Skip to content

Commit bc12123

Browse files
authored
fix(29908): Declare static method/property quickfix can add st… (microsoft#36854)
1 parent 7cc4a8d commit bc12123

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,26 @@ namespace ts.codefix {
129129
const { symbol } = leftExpressionType;
130130
if (!symbol || !symbol.declarations) return undefined;
131131

132-
const isClass = find(symbol.declarations, isClassLike);
132+
const classDeclaration = find(symbol.declarations, isClassLike);
133133
// Don't suggest adding private identifiers to anything other than a class.
134-
if (!isClass && isPrivateIdentifier(token)) {
134+
if (!classDeclaration && isPrivateIdentifier(token)) {
135135
return undefined;
136136
}
137137

138138
// Prefer to change the class instead of the interface if they are merged
139-
const classOrInterface = isClass || find(symbol.declarations, isInterfaceDeclaration);
139+
const classOrInterface = classDeclaration || find(symbol.declarations, isInterfaceDeclaration);
140140
if (classOrInterface && !program.isSourceFileFromExternalLibrary(classOrInterface.getSourceFile())) {
141141
const makeStatic = ((leftExpressionType as TypeReference).target || leftExpressionType) !== checker.getDeclaredTypeOfSymbol(symbol);
142-
// Static private identifier properties are not supported yet.
143-
if (makeStatic && isPrivateIdentifier(token)) return undefined;
142+
if (makeStatic && (isPrivateIdentifier(token) || isInterfaceDeclaration(classOrInterface))) {
143+
return undefined;
144+
}
145+
144146
const declSourceFile = classOrInterface.getSourceFile();
145147
const inJs = isSourceFileJS(declSourceFile);
146148
const call = tryCast(parent.parent, isCallExpression);
147149
return { kind: InfoKind.ClassOrInterface, token, parentDeclaration: classOrInterface, makeStatic, declSourceFile, inJs, call };
148150
}
151+
149152
const enumDeclaration = find(symbol.declarations, isEnumDeclaration);
150153
if (enumDeclaration && !isPrivateIdentifier(token) && !program.isSourceFileFromExternalLibrary(enumDeclaration.getSourceFile())) {
151154
return { kind: InfoKind.Enum, token, parentDeclaration: enumDeclaration };
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////interface Foo {}
4+
////class Foo {}
5+
////Foo.test();
6+
7+
verify.codeFix({
8+
description: ignoreInterpolations(ts.Diagnostics.Declare_static_method_0),
9+
index: 0,
10+
newFileContent:
11+
`interface Foo {}
12+
class Foo {
13+
static test() {
14+
throw new Error("Method not implemented.");
15+
}
16+
}
17+
Foo.test();`
18+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////interface Foo {}
4+
////class Foo {}
5+
////Foo.test;
6+
7+
verify.codeFix({
8+
description: ignoreInterpolations(ts.Diagnostics.Declare_static_property_0),
9+
index: 0,
10+
newFileContent:
11+
`interface Foo {}
12+
class Foo {
13+
static test: any;
14+
}
15+
Foo.test;`
16+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////interface Foo {}
4+
////namespace Foo {
5+
//// export function bar() { }
6+
////}
7+
////Foo.test();
8+
9+
verify.not.codeFixAvailable();

0 commit comments

Comments
 (0)