Skip to content

Commit 9050d0f

Browse files
committed
update external files api
1 parent 6bc5ef3 commit 9050d0f

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 5 additions & 10 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,12 +113,7 @@ 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-
121-
function getInfo(tokenSourceFile: SourceFile, tokenPos: number, checker: TypeChecker): Info | undefined {
116+
function getInfo(tokenSourceFile: SourceFile, tokenPos: number, checker: TypeChecker, program: Program): Info | undefined {
122117
// The identifier of the missing property. eg:
123118
// this.missing = 1;
124119
// ^^^^^^^
@@ -136,15 +131,15 @@ namespace ts.codefix {
136131

137132
// Prefer to change the class instead of the interface if they are merged
138133
const classOrInterface = find(symbol.declarations, isClassLike) || find(symbol.declarations, isInterfaceDeclaration);
139-
if (classOrInterface && !isInNodeModulesDeclarationFile(classOrInterface)) {
134+
if (classOrInterface && !program.isSourceFileFromExternalLibrary(classOrInterface.getSourceFile())) {
140135
const makeStatic = ((leftExpressionType as TypeReference).target || leftExpressionType) !== checker.getDeclaredTypeOfSymbol(symbol);
141136
const declSourceFile = classOrInterface.getSourceFile();
142137
const inJs = isSourceFileJS(declSourceFile);
143138
const call = tryCast(parent.parent, isCallExpression);
144139
return { kind: InfoKind.ClassOrInterface, token, parentDeclaration: classOrInterface, makeStatic, declSourceFile, inJs, call };
145140
}
146141
const enumDeclaration = find(symbol.declarations, isEnumDeclaration);
147-
if (enumDeclaration && !isInNodeModulesDeclarationFile(enumDeclaration)) {
142+
if (enumDeclaration && !program.isSourceFileFromExternalLibrary(enumDeclaration.getSourceFile())) {
148143
return { kind: InfoKind.Enum, token, parentDeclaration: enumDeclaration };
149144
}
150145
return undefined;
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
/// <reference path="fourslash.ts" />
22

3-
// @Filename: /node_modules/foo/declarations.d.ts
3+
// @noImplicitReferences: true
4+
// @traceResolution: true
5+
6+
// @Filename: /node_modules/foo/types.d.ts
47
//// interface Response {}
58

6-
// @Filename: foo.ts
7-
//// import '/node_modules/foo/declarations.d.ts'
9+
// @Filename: /node_modules/foo/package.json
10+
//// { "types": "types.d.ts" }
11+
12+
// @Filename: /foo.ts
13+
//// import { Response } from 'foo'
814
//// declare const resp: Response
915
//// resp.test()
1016

11-
goTo.file('foo.ts')
17+
goTo.file('/foo.ts')
1218

1319
verify.not.codeFixAvailable()

0 commit comments

Comments
 (0)