Skip to content

Commit 3ecb601

Browse files
authored
synthesize complete import declaration for tslib (#12151)
1 parent c87bce1 commit 3ecb601

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/compiler/program.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,9 +1143,14 @@ namespace ts {
11431143
if (options.importHelpers
11441144
&& (options.isolatedModules || isExternalModuleFile)
11451145
&& !file.isDeclarationFile) {
1146-
const externalHelpersModuleReference = <StringLiteral>createNode(SyntaxKind.StringLiteral);
1146+
// synthesize 'import "tslib"' declaration
1147+
const externalHelpersModuleReference = <StringLiteral>createSynthesizedNode(SyntaxKind.StringLiteral);
11471148
externalHelpersModuleReference.text = externalHelpersModuleNameText;
1148-
externalHelpersModuleReference.parent = file;
1149+
const importDecl = createSynthesizedNode(SyntaxKind.ImportDeclaration);
1150+
1151+
importDecl.parent = file;
1152+
externalHelpersModuleReference.parent = importDecl;
1153+
11491154
imports = [externalHelpersModuleReference];
11501155
}
11511156

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,4 +2576,21 @@ namespace ts.projectSystem {
25762576
assert.isTrue(diags.length === 0);
25772577
});
25782578
});
2579+
2580+
describe("import helpers", () => {
2581+
it("should not crash in tsserver", () => {
2582+
const f1 = {
2583+
path: "/a/app.ts",
2584+
content: "export async function foo() { return 100; }"
2585+
};
2586+
const tslib = {
2587+
path: "/a/node_modules/tslib/index.d.ts",
2588+
content: ""
2589+
};
2590+
const host = createServerHost([f1, tslib]);
2591+
const service = createProjectService(host);
2592+
service.openExternalProject({ projectFileName: "p", rootFiles: [toExternalFile(f1.path)], options: { importHelpers: true } });
2593+
service.checkNumberOfProjects({ externalProjects: 1 });
2594+
});
2595+
});
25792596
}

0 commit comments

Comments
 (0)