From 30295325a7e7b8c3370a9ed44e7bd524724855fe Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 10 Nov 2016 11:10:19 -0800 Subject: [PATCH] synthesize complete import declaration for tslib --- src/compiler/program.ts | 9 +++++++-- src/harness/unittests/tsserverProjectSystem.ts | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 213e6a95ab173..b080f7297464c 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1140,9 +1140,14 @@ namespace ts { if (options.importHelpers && (options.isolatedModules || isExternalModuleFile) && !file.isDeclarationFile) { - const externalHelpersModuleReference = createNode(SyntaxKind.StringLiteral); + // synthesize 'import "tslib"' declaration + const externalHelpersModuleReference = createSynthesizedNode(SyntaxKind.StringLiteral); externalHelpersModuleReference.text = externalHelpersModuleNameText; - externalHelpersModuleReference.parent = file; + const importDecl = createSynthesizedNode(SyntaxKind.ImportDeclaration); + + importDecl.parent = file; + externalHelpersModuleReference.parent = importDecl; + imports = [externalHelpersModuleReference]; } diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 17b1dd3e585f7..7986137ca4344 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -2576,4 +2576,21 @@ namespace ts.projectSystem { assert.isTrue(diags.length === 0); }); }); + + describe("import helpers", () => { + it("should not crash in tsserver", () => { + const f1 = { + path: "/a/app.ts", + content: "export async function foo() { return 100; }" + }; + const tslib = { + path: "/a/node_modules/tslib/index.d.ts", + content: "" + }; + const host = createServerHost([f1, tslib]); + const service = createProjectService(host); + service.openExternalProject({ projectFileName: "p", rootFiles: [toExternalFile(f1.path)], options: { importHelpers: true } }); + service.checkNumberOfProjects({ externalProjects: 1 }); + }); + }); } \ No newline at end of file