mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
avoid add missing member in declaration file
This commit is contained in:
parent
3a2f6a3ed1
commit
bf42f5bd1c
@ -113,6 +113,11 @@ namespace ts.codefix {
|
||||
}
|
||||
type Info = EnumInfo | ClassOrInterfaceInfo;
|
||||
|
||||
function isInNodeModulesDeclarationFile(node: Node) {
|
||||
const sourceFile = getSourceFileOfNode(node);
|
||||
return sourceFile.isDeclarationFile && startsWith(sourceFile.resolvedPath, "node_modules/") || sourceFile.resolvedPath.indexOf("/node_modules/") !== -1;
|
||||
}
|
||||
|
||||
function getInfo(tokenSourceFile: SourceFile, tokenPos: number, checker: TypeChecker): Info | undefined {
|
||||
// The identifier of the missing property. eg:
|
||||
// this.missing = 1;
|
||||
@ -131,7 +136,7 @@ namespace ts.codefix {
|
||||
|
||||
// Prefer to change the class instead of the interface if they are merged
|
||||
const classOrInterface = find(symbol.declarations, isClassLike) || find(symbol.declarations, isInterfaceDeclaration);
|
||||
if (classOrInterface) {
|
||||
if (classOrInterface && !isInNodeModulesDeclarationFile(classOrInterface)) {
|
||||
const makeStatic = ((leftExpressionType as TypeReference).target || leftExpressionType) !== checker.getDeclaredTypeOfSymbol(symbol);
|
||||
const declSourceFile = classOrInterface.getSourceFile();
|
||||
const inJs = isSourceFileJS(declSourceFile);
|
||||
@ -139,7 +144,7 @@ namespace ts.codefix {
|
||||
return { kind: InfoKind.ClassOrInterface, token, parentDeclaration: classOrInterface, makeStatic, declSourceFile, inJs, call };
|
||||
}
|
||||
const enumDeclaration = find(symbol.declarations, isEnumDeclaration);
|
||||
if (enumDeclaration) {
|
||||
if (enumDeclaration && !isInNodeModulesDeclarationFile(enumDeclaration)) {
|
||||
return { kind: InfoKind.Enum, token, parentDeclaration: enumDeclaration };
|
||||
}
|
||||
return undefined;
|
||||
|
||||
17
tests/cases/fourslash/addMemberInDeclarationFile.ts
Normal file
17
tests/cases/fourslash/addMemberInDeclarationFile.ts
Normal file
@ -0,0 +1,17 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @Filename: ./declarations.d.ts
|
||||
//// interface Response {}
|
||||
|
||||
// @Filename: foo.ts
|
||||
//// import './declarations.d.ts'
|
||||
//// declare const resp: Response
|
||||
//// resp.test()
|
||||
|
||||
goTo.file('foo.ts')
|
||||
|
||||
verify.codeFixAvailable([
|
||||
{ description: "Declare method 'test'" },
|
||||
{ description: "Declare property 'test'" },
|
||||
{ description: "Add index signature for property 'test'" }
|
||||
])
|
||||
@ -0,0 +1,13 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @Filename: /node_modules/foo/declarations.d.ts
|
||||
//// interface Response {}
|
||||
|
||||
// @Filename: foo.ts
|
||||
//// import '/node_modules/foo/declarations.d.ts'
|
||||
//// declare const resp: Response
|
||||
//// resp.test()
|
||||
|
||||
goTo.file('foo.ts')
|
||||
|
||||
verify.not.codeFixAvailable()
|
||||
Loading…
x
Reference in New Issue
Block a user