Skip to content

Commit 8c2ed97

Browse files
authored
Merge pull request #24187 from Kingwl/disallow-in-function-like-initializer
disallow acesssor generate in function like initializer
2 parents 66d6e5e + 45c06cf commit 8c2ed97

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/services/refactors/generateGetAccessorAndSetAccessor.ts

Lines changed: 9 additions & 6 deletions
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,12 +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);
122124
const declaration = findAncestor(node.parent, isAcceptedDeclaration);
123125
// make sure declaration have AccessibilityModifier or Static Modifier or Readonly Modifier
124126
const meaning = ModifierFlags.AccessibilityModifier | ModifierFlags.Static | ModifierFlags.Readonly;
125-
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;
126129

127130
const name = declaration.name.text;
128131
const startWithUnderscore = startsWithUnderscore(name);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// class A {
4+
//// /*a*/public/*b*/ /*c*/a/*d*/ = () => {
5+
//// /*e*/return/*f*/ /*g*/1/*h*/;
6+
//// }
7+
//// /*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*/
13+
//// };
14+
15+
goTo.select("a", "b");
16+
verify.not.refactorAvailable();
17+
18+
goTo.select("c", "d");
19+
verify.refactorAvailable("Generate 'get' and 'set' accessors");
20+
21+
goTo.select("e", "f");
22+
verify.not.refactorAvailable();
23+
24+
goTo.select("g", "h");
25+
verify.not.refactorAvailable();
26+
27+
goTo.select("i", "j");
28+
verify.not.refactorAvailable();
29+
30+
goTo.select("k", "l");
31+
verify.not.refactorAvailable();
32+
33+
goTo.select("m", "n");
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)