diff --git a/src/services/refactors/generateGetAccessorAndSetAccessor.ts b/src/services/refactors/generateGetAccessorAndSetAccessor.ts index d68a052eca9..92d87b1fcd0 100644 --- a/src/services/refactors/generateGetAccessorAndSetAccessor.ts +++ b/src/services/refactors/generateGetAccessorAndSetAccessor.ts @@ -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 = 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); diff --git a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess35.ts b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess35.ts index f1b306fa4b9..35e707b49d0 100644 --- a/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess35.ts +++ b/tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess35.ts @@ -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"); \ No newline at end of file +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");