only allow refactor if selected span overlaps name declaration

This commit is contained in:
王文璐
2018-05-18 10:07:45 +08:00
parent 755b443b6d
commit 45c06cfd11
2 changed files with 36 additions and 18 deletions

View File

@@ -21,8 +21,8 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
}
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
const { file, startPosition } = context;
if (!getConvertibleFieldAtPosition(file, startPosition)) return undefined;
const { file } = context;
if (!getConvertibleFieldAtPosition(context, file)) return undefined;
return [{
name: actionName,
@@ -37,9 +37,9 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
}
function getEditsForAction(context: RefactorContext, _actionName: string): RefactorEditInfo | undefined {
const { file, startPosition } = context;
const { file } = context;
const fieldInfo = getConvertibleFieldAtPosition(file, startPosition);
const fieldInfo = getConvertibleFieldAtPosition(context, file);
if (!fieldInfo) return undefined;
const isJS = isSourceFileJavaScript(file);
@@ -117,17 +117,15 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
return name.charCodeAt(0) === CharacterCodes._;
}
function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined {
function getConvertibleFieldAtPosition(context: RefactorContext, file: SourceFile): Info | undefined {
const { startPosition, endPosition } = context;
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
const declaration = <AcceptedDeclaration>findAncestor(node.parent, n => {
if (isFunctionLikeDeclaration(n)) {
return "quit";
}
return isAcceptedDeclaration(n);
});
const declaration = findAncestor(node.parent, isAcceptedDeclaration);
// make sure declaration have AccessibilityModifier or Static Modifier or Readonly Modifier
const meaning = ModifierFlags.AccessibilityModifier | ModifierFlags.Static | ModifierFlags.Readonly;
if (!declaration || !isConvertableName(declaration.name) || (getModifierFlags(declaration) | meaning) !== meaning) return undefined;
if (!declaration || !rangeOverlapsWithStartEnd(declaration.name, startPosition, endPosition)
|| !isConvertableName(declaration.name) || (getModifierFlags(declaration) | meaning) !== meaning) return undefined;
const name = declaration.name.text;
const startWithUnderscore = startsWithUnderscore(name);

View File

@@ -5,25 +5,45 @@
//// /*e*/return/*f*/ /*g*/1/*h*/;
//// }
//// /*i*/b/*j*/: /*k*/number/*l*/ = /*m*/1/*n*/
//// /*o*/public /*p*/ c: number = 1; /*q*/
//// /*r*/d = 1
//// /*s*/public e/*t*/ = /*u*/ 1
//// f = 1/*v*/ /*w*/
//// g = 1/*x*/
//// };
goTo.select("a", "b");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
verify.not.refactorAvailable();
goTo.select("c", "d");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
goTo.select("e", "f");
verify.not.refactorAvailable("Generate 'get' and 'set' accessors");
verify.not.refactorAvailable();
goTo.select("g", "h");
verify.not.refactorAvailable("Generate 'get' and 'set' accessors");
verify.not.refactorAvailable();
goTo.select("i", "j");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
verify.not.refactorAvailable();
goTo.select("k", "l");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
verify.not.refactorAvailable();
goTo.select("m", "n");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
verify.not.refactorAvailable();
goTo.select("o", "p");
verify.not.refactorAvailable();
goTo.select("q", "r");
verify.not.refactorAvailable();
goTo.select("s", "t");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
goTo.select("u", "v");
verify.not.refactorAvailable();
goTo.select("w", "x");
verify.refactorAvailable("Generate 'get' and 'set' accessors");