Skip to content

Commit 17cedda

Browse files
authored
Merge pull request #29493 from Kingwl/avoid-quickfix-for-declaration-file
avoid add missing member in declaration file
2 parents bb5eb02 + 9050d0f commit 17cedda

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ts.codefix {
1212
registerCodeFix({
1313
errorCodes,
1414
getCodeActions(context) {
15-
const info = getInfo(context.sourceFile, context.span.start, context.program.getTypeChecker());
15+
const info = getInfo(context.sourceFile, context.span.start, context.program.getTypeChecker(), context.program);
1616
if (!info) return undefined;
1717

1818
if (info.kind === InfoKind.Enum) {
@@ -37,7 +37,7 @@ namespace ts.codefix {
3737

3838
return createCombinedCodeActions(textChanges.ChangeTracker.with(context, changes => {
3939
eachDiagnostic(context, errorCodes, diag => {
40-
const info = getInfo(diag.file, diag.start, checker);
40+
const info = getInfo(diag.file, diag.start, checker, context.program);
4141
if (!info || !addToSeen(seen, getNodeId(info.parentDeclaration) + "#" + info.token.text)) {
4242
return;
4343
}
@@ -113,7 +113,7 @@ namespace ts.codefix {
113113
}
114114
type Info = EnumInfo | ClassOrInterfaceInfo;
115115

116-
function getInfo(tokenSourceFile: SourceFile, tokenPos: number, checker: TypeChecker): Info | undefined {
116+
function getInfo(tokenSourceFile: SourceFile, tokenPos: number, checker: TypeChecker, program: Program): Info | undefined {
117117
// The identifier of the missing property. eg:
118118
// this.missing = 1;
119119
// ^^^^^^^
@@ -131,15 +131,15 @@ namespace ts.codefix {
131131

132132
// Prefer to change the class instead of the interface if they are merged
133133
const classOrInterface = find(symbol.declarations, isClassLike) || find(symbol.declarations, isInterfaceDeclaration);
134-
if (classOrInterface) {
134+
if (classOrInterface && !program.isSourceFileFromExternalLibrary(classOrInterface.getSourceFile())) {
135135
const makeStatic = ((leftExpressionType as TypeReference).target || leftExpressionType) !== checker.getDeclaredTypeOfSymbol(symbol);
136136
const declSourceFile = classOrInterface.getSourceFile();
137137
const inJs = isSourceFileJS(declSourceFile);
138138
const call = tryCast(parent.parent, isCallExpression);
139139
return { kind: InfoKind.ClassOrInterface, token, parentDeclaration: classOrInterface, makeStatic, declSourceFile, inJs, call };
140140
}
141141
const enumDeclaration = find(symbol.declarations, isEnumDeclaration);
142-
if (enumDeclaration) {
142+
if (enumDeclaration && !program.isSourceFileFromExternalLibrary(enumDeclaration.getSourceFile())) {
143143
return { kind: InfoKind.Enum, token, parentDeclaration: enumDeclaration };
144144
}
145145
return undefined;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: ./declarations.d.ts
4+
//// interface Response {}
5+
6+
// @Filename: foo.ts
7+
//// import './declarations.d.ts'
8+
//// declare const resp: Response
9+
//// resp.test()
10+
11+
goTo.file('foo.ts')
12+
13+
verify.codeFixAvailable([
14+
{ description: "Declare method 'test'" },
15+
{ description: "Declare property 'test'" },
16+
{ description: "Add index signature for property 'test'" }
17+
])
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @noImplicitReferences: true
4+
// @traceResolution: true
5+
6+
// @Filename: /node_modules/foo/types.d.ts
7+
//// interface Response {}
8+
9+
// @Filename: /node_modules/foo/package.json
10+
//// { "types": "types.d.ts" }
11+
12+
// @Filename: /foo.ts
13+
//// import { Response } from 'foo'
14+
//// declare const resp: Response
15+
//// resp.test()
16+
17+
goTo.file('/foo.ts')
18+
19+
verify.not.codeFixAvailable()

0 commit comments

Comments
 (0)