Skip to content

Commit 8481bc1

Browse files
authored
Do not report errors when we fail to find a module symbol at an import specifier when invoked via API (microsoft#36742)
1 parent a772c26 commit 8481bc1

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Diff for: src/compiler/checker.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ namespace ts {
414414
},
415415
getSymbolAtLocation: node => {
416416
node = getParseTreeNode(node);
417-
return node ? getSymbolAtLocation(node) : undefined;
417+
// set ignoreErrors: true because any lookups invoked by the API shouldn't cause any new errors
418+
return node ? getSymbolAtLocation(node, /*ignoreErrors*/ true) : undefined;
418419
},
419420
getShorthandAssignmentValueSymbol: node => {
420421
node = getParseTreeNode(node);
@@ -34412,7 +34413,7 @@ namespace ts {
3441234413
return undefined;
3441334414
}
3441434415

34415-
function getSymbolAtLocation(node: Node): Symbol | undefined {
34416+
function getSymbolAtLocation(node: Node, ignoreErrors?: boolean): Symbol | undefined {
3441634417
if (node.kind === SyntaxKind.SourceFile) {
3441734418
return isExternalModule(<SourceFile>node) ? getMergedSymbol(node.symbol) : undefined;
3441834419
}
@@ -34496,7 +34497,7 @@ namespace ts {
3449634497
((isInJSFile(node) && isRequireCall(node.parent, /*checkArgumentIsStringLiteralLike*/ false)) || isImportCall(node.parent)) ||
3449734498
(isLiteralTypeNode(node.parent) && isLiteralImportTypeNode(node.parent.parent) && node.parent.parent.argument === node.parent)
3449834499
) {
34499-
return resolveExternalModuleName(node, <LiteralExpression>node);
34500+
return resolveExternalModuleName(node, <LiteralExpression>node, ignoreErrors);
3450034501
}
3450134502
if (isCallExpression(parent) && isBindableObjectDefinePropertyCall(parent) && parent.arguments[1] === node) {
3450234503
return getSymbolOfNode(parent);
@@ -34518,7 +34519,7 @@ namespace ts {
3451834519
case SyntaxKind.ClassKeyword:
3451934520
return getSymbolOfNode(node.parent);
3452034521
case SyntaxKind.ImportType:
34521-
return isLiteralImportTypeNode(node) ? getSymbolAtLocation(node.argument.literal) : undefined;
34522+
return isLiteralImportTypeNode(node) ? getSymbolAtLocation(node.argument.literal, ignoreErrors) : undefined;
3452234523

3452334524
case SyntaxKind.ExportKeyword:
3452434525
return isExportAssignment(node.parent) ? Debug.assertDefined(node.parent.symbol) : undefined;

Diff for: src/testRunner/unittests/programApi.ts

+15
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,19 @@ namespace ts {
178178
assert.isNotNaN(program.getIdentifierCount());
179179
});
180180
});
181+
182+
describe("unittests:: programApi:: Program.getDiagnosticsProducingTypeChecker / Program.getSemanticDiagnostics", () => {
183+
it("getSymbolAtLocation does not cause additional error to be added on module resolution failure", () => {
184+
const main = new documents.TextDocument("/main.ts", "import \"./module\";");
185+
const mod = new documents.TextDocument("/module.d.ts", "declare const foo: any;");
186+
187+
const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, { documents: [main, mod], cwd: "/" });
188+
const program = createProgram(["/main.ts"], {}, new fakes.CompilerHost(fs, { newLine: NewLineKind.LineFeed }));
189+
190+
const sourceFile = program.getSourceFile("main.ts")!;
191+
const typeChecker = program.getDiagnosticsProducingTypeChecker();
192+
typeChecker.getSymbolAtLocation((sourceFile.statements[0] as ImportDeclaration).moduleSpecifier);
193+
assert.isEmpty(program.getSemanticDiagnostics());
194+
});
195+
});
181196
}

0 commit comments

Comments
 (0)