Skip to content

Commit bf42f5b

Browse files
committed
avoid add missing member in declaration file
1 parent 3a2f6a3 commit bf42f5b

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ namespace ts.codefix {
113113
}
114114
type Info = EnumInfo | ClassOrInterfaceInfo;
115115

116+
function isInNodeModulesDeclarationFile(node: Node) {
117+
const sourceFile = getSourceFileOfNode(node);
118+
return sourceFile.isDeclarationFile && startsWith(sourceFile.resolvedPath, "node_modules/") || sourceFile.resolvedPath.indexOf("/node_modules/") !== -1;
119+
}
120+
116121
function getInfo(tokenSourceFile: SourceFile, tokenPos: number, checker: TypeChecker): Info | undefined {
117122
// The identifier of the missing property. eg:
118123
// this.missing = 1;
@@ -131,15 +136,15 @@ namespace ts.codefix {
131136

132137
// Prefer to change the class instead of the interface if they are merged
133138
const classOrInterface = find(symbol.declarations, isClassLike) || find(symbol.declarations, isInterfaceDeclaration);
134-
if (classOrInterface) {
139+
if (classOrInterface && !isInNodeModulesDeclarationFile(classOrInterface)) {
135140
const makeStatic = ((leftExpressionType as TypeReference).target || leftExpressionType) !== checker.getDeclaredTypeOfSymbol(symbol);
136141
const declSourceFile = classOrInterface.getSourceFile();
137142
const inJs = isSourceFileJS(declSourceFile);
138143
const call = tryCast(parent.parent, isCallExpression);
139144
return { kind: InfoKind.ClassOrInterface, token, parentDeclaration: classOrInterface, makeStatic, declSourceFile, inJs, call };
140145
}
141146
const enumDeclaration = find(symbol.declarations, isEnumDeclaration);
142-
if (enumDeclaration) {
147+
if (enumDeclaration && !isInNodeModulesDeclarationFile(enumDeclaration)) {
143148
return { kind: InfoKind.Enum, token, parentDeclaration: enumDeclaration };
144149
}
145150
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /node_modules/foo/declarations.d.ts
4+
//// interface Response {}
5+
6+
// @Filename: foo.ts
7+
//// import '/node_modules/foo/declarations.d.ts'
8+
//// declare const resp: Response
9+
//// resp.test()
10+
11+
goTo.file('foo.ts')
12+
13+
verify.not.codeFixAvailable()

0 commit comments

Comments
 (0)