Skip to content

Commit 45c06cf

Browse files
committed
only allow refactor if selected span overlaps name declaration
1 parent 755b443 commit 45c06cf

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/services/refactors/generateGetAccessorAndSetAccessor.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
2121
}
2222

2323
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
24-
const { file, startPosition } = context;
25-
if (!getConvertibleFieldAtPosition(file, startPosition)) return undefined;
24+
const { file } = context;
25+
if (!getConvertibleFieldAtPosition(context, file)) return undefined;
2626

2727
return [{
2828
name: actionName,
@@ -37,9 +37,9 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
3737
}
3838

3939
function getEditsForAction(context: RefactorContext, _actionName: string): RefactorEditInfo | undefined {
40-
const { file, startPosition } = context;
40+
const { file } = context;
4141

42-
const fieldInfo = getConvertibleFieldAtPosition(file, startPosition);
42+
const fieldInfo = getConvertibleFieldAtPosition(context, file);
4343
if (!fieldInfo) return undefined;
4444

4545
const isJS = isSourceFileJavaScript(file);
@@ -117,17 +117,15 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
117117
return name.charCodeAt(0) === CharacterCodes._;
118118
}
119119

120-
function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined {
120+
function getConvertibleFieldAtPosition(context: RefactorContext, file: SourceFile): Info | undefined {
121+
const { startPosition, endPosition } = context;
122+
121123
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
122-
const declaration = <AcceptedDeclaration>findAncestor(node.parent, n => {
123-
if (isFunctionLikeDeclaration(n)) {
124-
return "quit";
125-
}
126-
return isAcceptedDeclaration(n);
127-
});
124+
const declaration = findAncestor(node.parent, isAcceptedDeclaration);
128125
// make sure declaration have AccessibilityModifier or Static Modifier or Readonly Modifier
129126
const meaning = ModifierFlags.AccessibilityModifier | ModifierFlags.Static | ModifierFlags.Readonly;
130-
if (!declaration || !isConvertableName(declaration.name) || (getModifierFlags(declaration) | meaning) !== meaning) return undefined;
127+
if (!declaration || !rangeOverlapsWithStartEnd(declaration.name, startPosition, endPosition)
128+
|| !isConvertableName(declaration.name) || (getModifierFlags(declaration) | meaning) !== meaning) return undefined;
131129

132130
const name = declaration.name.text;
133131
const startWithUnderscore = startsWithUnderscore(name);

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess35.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,45 @@
55
//// /*e*/return/*f*/ /*g*/1/*h*/;
66
//// }
77
//// /*i*/b/*j*/: /*k*/number/*l*/ = /*m*/1/*n*/
8+
//// /*o*/public /*p*/ c: number = 1; /*q*/
9+
//// /*r*/d = 1
10+
//// /*s*/public e/*t*/ = /*u*/ 1
11+
//// f = 1/*v*/ /*w*/
12+
//// g = 1/*x*/
813
//// };
914

1015
goTo.select("a", "b");
11-
verify.refactorAvailable("Generate 'get' and 'set' accessors");
16+
verify.not.refactorAvailable();
1217

1318
goTo.select("c", "d");
1419
verify.refactorAvailable("Generate 'get' and 'set' accessors");
1520

1621
goTo.select("e", "f");
17-
verify.not.refactorAvailable("Generate 'get' and 'set' accessors");
22+
verify.not.refactorAvailable();
1823

1924
goTo.select("g", "h");
20-
verify.not.refactorAvailable("Generate 'get' and 'set' accessors");
25+
verify.not.refactorAvailable();
2126

2227
goTo.select("i", "j");
23-
verify.refactorAvailable("Generate 'get' and 'set' accessors");
28+
verify.not.refactorAvailable();
2429

2530
goTo.select("k", "l");
26-
verify.refactorAvailable("Generate 'get' and 'set' accessors");
31+
verify.not.refactorAvailable();
2732

2833
goTo.select("m", "n");
29-
verify.refactorAvailable("Generate 'get' and 'set' accessors");
34+
verify.not.refactorAvailable();
35+
36+
goTo.select("o", "p");
37+
verify.not.refactorAvailable();
38+
39+
goTo.select("q", "r");
40+
verify.not.refactorAvailable();
41+
42+
goTo.select("s", "t");
43+
verify.refactorAvailable("Generate 'get' and 'set' accessors");
44+
45+
goTo.select("u", "v");
46+
verify.not.refactorAvailable();
47+
48+
goTo.select("w", "x");
49+
verify.refactorAvailable("Generate 'get' and 'set' accessors");

0 commit comments

Comments
 (0)