mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 19:16:17 -06:00
fix(29908): Declare static method/property quickfix can add st… (#36854)
This commit is contained in:
parent
7cc4a8df94
commit
bc12123115
@ -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 };
|
||||
|
||||
18
tests/cases/fourslash/codeFixAddMissingMember14.ts
Normal file
18
tests/cases/fourslash/codeFixAddMissingMember14.ts
Normal 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();`
|
||||
});
|
||||
16
tests/cases/fourslash/codeFixAddMissingMember15.ts
Normal file
16
tests/cases/fourslash/codeFixAddMissingMember15.ts
Normal 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;`
|
||||
});
|
||||
9
tests/cases/fourslash/codeFixAddMissingMember16.ts
Normal file
9
tests/cases/fourslash/codeFixAddMissingMember16.ts
Normal file
@ -0,0 +1,9 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////interface Foo {}
|
||||
////namespace Foo {
|
||||
//// export function bar() { }
|
||||
////}
|
||||
////Foo.test();
|
||||
|
||||
verify.not.codeFixAvailable();
|
||||
Loading…
x
Reference in New Issue
Block a user