disallow acesssor generate in function like initializer

This commit is contained in:
王文璐
2018-05-17 10:18:20 +08:00
parent 159c808a0d
commit 755b443b6d
2 changed files with 35 additions and 1 deletions

View File

@@ -119,7 +119,12 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined {
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
const declaration = findAncestor(node.parent, isAcceptedDeclaration);
const declaration = <AcceptedDeclaration>findAncestor(node.parent, n => {
if (isFunctionLikeDeclaration(n)) {
return "quit";
}
return isAcceptedDeclaration(n);
});
// 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;

View File

@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts' />
//// class A {
//// /*a*/public/*b*/ /*c*/a/*d*/ = () => {
//// /*e*/return/*f*/ /*g*/1/*h*/;
//// }
//// /*i*/b/*j*/: /*k*/number/*l*/ = /*m*/1/*n*/
//// };
goTo.select("a", "b");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
goTo.select("c", "d");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
goTo.select("e", "f");
verify.not.refactorAvailable("Generate 'get' and 'set' accessors");
goTo.select("g", "h");
verify.not.refactorAvailable("Generate 'get' and 'set' accessors");
goTo.select("i", "j");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
goTo.select("k", "l");
verify.refactorAvailable("Generate 'get' and 'set' accessors");
goTo.select("m", "n");
verify.refactorAvailable("Generate 'get' and 'set' accessors");