fix(29908): Declare static method/property quickfix can add st… (#36854)

This commit is contained in:
Alexander T 2020-02-18 21:27:35 +02:00 committed by GitHub
parent 7cc4a8df94
commit bc12123115
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 5 deletions

View File

@ -129,23 +129,26 @@ namespace ts.codefix {
const { symbol } = leftExpressionType;
if (!symbol || !symbol.declarations) return undefined;
const isClass = find(symbol.declarations, isClassLike);
const classDeclaration = find(symbol.declarations, isClassLike);
// Don't suggest adding private identifiers to anything other than a class.
if (!isClass && isPrivateIdentifier(token)) {
if (!classDeclaration && isPrivateIdentifier(token)) {
return undefined;
}
// Prefer to change the class instead of the interface if they are merged
const classOrInterface = isClass || find(symbol.declarations, isInterfaceDeclaration);
const classOrInterface = classDeclaration || find(symbol.declarations, isInterfaceDeclaration);
if (classOrInterface && !program.isSourceFileFromExternalLibrary(classOrInterface.getSourceFile())) {
const makeStatic = ((leftExpressionType as TypeReference).target || leftExpressionType) !== checker.getDeclaredTypeOfSymbol(symbol);
// Static private identifier properties are not supported yet.
if (makeStatic && isPrivateIdentifier(token)) return undefined;
if (makeStatic && (isPrivateIdentifier(token) || isInterfaceDeclaration(classOrInterface))) {
return undefined;
}
const declSourceFile = classOrInterface.getSourceFile();
const inJs = isSourceFileJS(declSourceFile);
const call = tryCast(parent.parent, isCallExpression);
return { kind: InfoKind.ClassOrInterface, token, parentDeclaration: classOrInterface, makeStatic, declSourceFile, inJs, call };
}
const enumDeclaration = find(symbol.declarations, isEnumDeclaration);
if (enumDeclaration && !isPrivateIdentifier(token) && !program.isSourceFileFromExternalLibrary(enumDeclaration.getSourceFile())) {
return { kind: InfoKind.Enum, token, parentDeclaration: enumDeclaration };

View File

@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />
////interface Foo {}
////class Foo {}
////Foo.test();
verify.codeFix({
description: ignoreInterpolations(ts.Diagnostics.Declare_static_method_0),
index: 0,
newFileContent:
`interface Foo {}
class Foo {
static test() {
throw new Error("Method not implemented.");
}
}
Foo.test();`
});

View File

@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />
////interface Foo {}
////class Foo {}
////Foo.test;
verify.codeFix({
description: ignoreInterpolations(ts.Diagnostics.Declare_static_property_0),
index: 0,
newFileContent:
`interface Foo {}
class Foo {
static test: any;
}
Foo.test;`
});

View File

@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />
////interface Foo {}
////namespace Foo {
//// export function bar() { }
////}
////Foo.test();
verify.not.codeFixAvailable();