Skip to content

Commit 755b443

Browse files
committed
disallow acesssor generate in function like initializer
1 parent 159c808 commit 755b443

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/services/refactors/generateGetAccessorAndSetAccessor.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
119119

120120
function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined {
121121
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
122-
const declaration = findAncestor(node.parent, isAcceptedDeclaration);
122+
const declaration = <AcceptedDeclaration>findAncestor(node.parent, n => {
123+
if (isFunctionLikeDeclaration(n)) {
124+
return "quit";
125+
}
126+
return isAcceptedDeclaration(n);
127+
});
123128
// make sure declaration have AccessibilityModifier or Static Modifier or Readonly Modifier
124129
const meaning = ModifierFlags.AccessibilityModifier | ModifierFlags.Static | ModifierFlags.Readonly;
125130
if (!declaration || !isConvertableName(declaration.name) || (getModifierFlags(declaration) | meaning) !== meaning) return undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
//// };
9+
10+
goTo.select("a", "b");
11+
verify.refactorAvailable("Generate 'get' and 'set' accessors");
12+
13+
goTo.select("c", "d");
14+
verify.refactorAvailable("Generate 'get' and 'set' accessors");
15+
16+
goTo.select("e", "f");
17+
verify.not.refactorAvailable("Generate 'get' and 'set' accessors");
18+
19+
goTo.select("g", "h");
20+
verify.not.refactorAvailable("Generate 'get' and 'set' accessors");
21+
22+
goTo.select("i", "j");
23+
verify.refactorAvailable("Generate 'get' and 'set' accessors");
24+
25+
goTo.select("k", "l");
26+
verify.refactorAvailable("Generate 'get' and 'set' accessors");
27+
28+
goTo.select("m", "n");
29+
verify.refactorAvailable("Generate 'get' and 'set' accessors");

0 commit comments

Comments
 (0)