From 50d98aee0e83bbf806d1f08c0b3eae785690da69 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 24 Jan 2019 12:40:52 -0800 Subject: [PATCH] Create getters for js sourcemap, dts and dts map text getters in prepend nodes --- src/compiler/emitter.ts | 19 +++-- src/compiler/factory.ts | 81 +++++++++++++++---- src/compiler/program.ts | 10 +-- src/compiler/transformers/declarations.ts | 2 +- src/compiler/transformers/ts.ts | 2 +- src/compiler/types.ts | 3 + src/testRunner/unittests/tsbuild.ts | 20 ++--- .../reference/api/tsserverlibrary.d.ts | 9 ++- tests/baselines/reference/api/typescript.d.ts | 9 ++- 9 files changed, 107 insertions(+), 48 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 395f6448890..8daedabf29a 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -38,17 +38,22 @@ namespace ts { } } + /*@internal*/ + export function getOutputPathsForBundle(options: CompilerOptions, forceDtsPaths: boolean): EmitFileNames { + const outPath = options.outFile || options.out!; + const jsFilePath = options.emitDeclarationOnly ? undefined : outPath; + const sourceMapFilePath = jsFilePath && getSourceMapFilePath(jsFilePath, options); + const declarationFilePath = (forceDtsPaths || getEmitDeclarations(options)) ? removeFileExtension(outPath) + Extension.Dts : undefined; + const declarationMapPath = declarationFilePath && getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined; + const bundleInfoPath = options.references && jsFilePath ? (removeFileExtension(jsFilePath) + infoExtension) : undefined; + return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, bundleInfoPath }; + } + /*@internal*/ export function getOutputPathsFor(sourceFile: SourceFile | Bundle, host: EmitHost, forceDtsPaths: boolean): EmitFileNames { const options = host.getCompilerOptions(); if (sourceFile.kind === SyntaxKind.Bundle) { - const outPath = options.outFile || options.out!; - const jsFilePath = options.emitDeclarationOnly ? undefined : outPath; - const sourceMapFilePath = jsFilePath && getSourceMapFilePath(jsFilePath, options); - const declarationFilePath = (forceDtsPaths || getEmitDeclarations(options)) ? removeFileExtension(outPath) + Extension.Dts : undefined; - const declarationMapPath = declarationFilePath && getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined; - const bundleInfoPath = options.references && jsFilePath ? (removeFileExtension(jsFilePath) + infoExtension) : undefined; - return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, bundleInfoPath }; + return getOutputPathsForBundle(options, forceDtsPaths); } else { const ownOutputFilePath = getOwnEmitOutputFilePath(sourceFile.fileName, host, getOutputExtension(sourceFile, options)); diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index ec19d2c906c..3707b725ed3 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -2630,41 +2630,88 @@ namespace ts { } export function createUnparsedSourceFile(text: string): UnparsedSource; + export function createUnparsedSourceFile(inputFile: InputFiles, type: "js" | "dts"): UnparsedSource; export function createUnparsedSourceFile(text: string, mapPath: string | undefined, map: string | undefined): UnparsedSource; - export function createUnparsedSourceFile(text: string, mapPath?: string, map?: string): UnparsedSource { + export function createUnparsedSourceFile(textOrInputFiles: string | InputFiles, mapPathOrType?: string | "js" | "dts", map?: string): UnparsedSource { const node = createNode(SyntaxKind.UnparsedSource); - node.text = text; - node.sourceMapPath = mapPath; - node.sourceMapText = map; + if (!isString(textOrInputFiles)) { + Debug.assert(mapPathOrType === "js" || mapPathOrType === "dts"); + node.fileName = mapPathOrType === "js" ? textOrInputFiles.javascriptPath : textOrInputFiles.declarationPath; + node.sourceMapPath = mapPathOrType === "js" ? textOrInputFiles.javascriptMapPath : textOrInputFiles.declarationMapPath; + Object.defineProperties(node, { + text: { get() { return mapPathOrType === "js" ? textOrInputFiles.javascriptText : textOrInputFiles.declarationText; } }, + sourceMapText: { get() { return mapPathOrType === "js" ? textOrInputFiles.javascriptMapText : textOrInputFiles.declarationMapText; } }, + }); + } + else { + node.text = textOrInputFiles; + node.sourceMapPath = mapPathOrType; + node.sourceMapText = map; + } return node; } export function createInputFiles( - javascript: string, - declaration: string + javascriptText: string, + declarationText: string ): InputFiles; export function createInputFiles( - javascript: string, - declaration: string, + readFileText: (path: string) => string | undefined, + javascriptPath: string, + javascriptMapPath: string | undefined, + declarationPath: string, + declarationMapPath: string | undefined, + ): InputFiles; + export function createInputFiles( + javascriptText: string, + declarationText: string, javascriptMapPath: string | undefined, javascriptMapText: string | undefined, declarationMapPath: string | undefined, declarationMapText: string | undefined ): InputFiles; export function createInputFiles( - javascript: string, - declaration: string, + javascriptTextOrReadFileText: string | ((path: string) => string | undefined), + declarationTextOrJavascriptPath: string, javascriptMapPath?: string, - javascriptMapText?: string, + javascriptMapTextOrDeclarationPath?: string, declarationMapPath?: string, declarationMapText?: string ): InputFiles { const node = createNode(SyntaxKind.InputFiles); - node.javascriptText = javascript; - node.javascriptMapPath = javascriptMapPath; - node.javascriptMapText = javascriptMapText; - node.declarationText = declaration; - node.declarationMapPath = declarationMapPath; - node.declarationMapText = declarationMapText; + if (!isString(javascriptTextOrReadFileText)) { + const cache = createMap(); + const textGetter = (path: string | undefined) => { + if (path === undefined) return undefined; + let value = cache.get(path); + if (value === undefined) { + value = javascriptTextOrReadFileText(path); + cache.set(path, value !== undefined ? value : false); + } + return value !== false ? value as string : undefined; + }; + const definedTextGetter = (path: string) => { + const result = textGetter(path); + return result !== undefined ? result : `/* Input file ${path} was missing */\r\n`; + }; + node.javascriptPath = declarationTextOrJavascriptPath; + node.javascriptMapPath = javascriptMapPath; + node.declarationPath = Debug.assertDefined(javascriptMapTextOrDeclarationPath); + node.declarationMapPath = declarationMapPath; + Object.defineProperties(node, { + javascriptText: { get() { return definedTextGetter(declarationTextOrJavascriptPath); } }, + javascriptMapText: { get() { return textGetter(javascriptMapPath); } }, + declarationText: { get() { return definedTextGetter(Debug.assertDefined(javascriptMapTextOrDeclarationPath)); } }, + declarationMapText: { get() { return textGetter(declarationMapPath); } } + }); + } + else { + node.javascriptText = javascriptTextOrReadFileText; + node.javascriptMapPath = javascriptMapPath; + node.javascriptMapText = javascriptMapTextOrDeclarationPath; + node.declarationText = declarationTextOrJavascriptPath; + node.declarationMapPath = declarationMapPath; + node.declarationMapText = declarationMapText; + } return node; } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 14042a89e94..dbc2e61fa42 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1456,14 +1456,8 @@ namespace ts { // Upstream project didn't have outFile set -- skip (error will have been issued earlier) if (!out) continue; - const dtsFilename = changeExtension(out, ".d.ts"); - const js = host.readFile(out) || `/* Input file ${out} was missing */\r\n`; - const jsMapPath = out + ".map"; // TODO: try to read sourceMappingUrl comment from the file - const jsMap = host.readFile(jsMapPath); - const dts = host.readFile(dtsFilename) || `/* Input file ${dtsFilename} was missing */\r\n`; - const dtsMapPath = dtsFilename + ".map"; - const dtsMap = host.readFile(dtsMapPath); - const node = createInputFiles(js, dts, jsMap && jsMapPath, jsMap, dtsMap && dtsMapPath, dtsMap); + const { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath } = getOutputPathsForBundle(resolvedRefOpts.options, /*forceDtsPaths*/ true); + const node = createInputFiles(path => host.readFile(path), jsFilePath!, sourceMapFilePath, declarationFilePath!, declarationMapPath); nodes.push(node); } } diff --git a/src/compiler/transformers/declarations.ts b/src/compiler/transformers/declarations.ts index d03c3b8d30b..42bb5d77931 100644 --- a/src/compiler/transformers/declarations.ts +++ b/src/compiler/transformers/declarations.ts @@ -207,7 +207,7 @@ namespace ts { } ), mapDefined(node.prepends, prepend => { if (prepend.kind === SyntaxKind.InputFiles) { - return createUnparsedSourceFile(prepend.declarationText, prepend.declarationMapPath, prepend.declarationMapText); + return createUnparsedSourceFile(prepend, "dts"); } })); bundle.syntheticFileReferences = []; diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index a22c6df8d06..7aebed59588 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -101,7 +101,7 @@ namespace ts { function transformBundle(node: Bundle) { return createBundle(node.sourceFiles.map(transformSourceFile), mapDefined(node.prepends, prepend => { if (prepend.kind === SyntaxKind.InputFiles) { - return createUnparsedSourceFile(prepend.javascriptText, prepend.javascriptMapPath, prepend.javascriptMapText); + return createUnparsedSourceFile(prepend, "js"); } return prepend; })); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d213e862299..c460b948347 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2761,9 +2761,11 @@ namespace ts { export interface InputFiles extends Node { kind: SyntaxKind.InputFiles; + javascriptPath?: string; javascriptText: string; javascriptMapPath?: string; javascriptMapText?: string; + declarationPath?: string; declarationText: string; declarationMapPath?: string; declarationMapText?: string; @@ -2771,6 +2773,7 @@ namespace ts { export interface UnparsedSource extends Node { kind: SyntaxKind.UnparsedSource; + fileName?: string; text: string; sourceMapPath?: string; sourceMapText?: string; diff --git a/src/testRunner/unittests/tsbuild.ts b/src/testRunner/unittests/tsbuild.ts index b8a58eb1bc0..be544f4eb1d 100644 --- a/src/testRunner/unittests/tsbuild.ts +++ b/src/testRunner/unittests/tsbuild.ts @@ -510,16 +510,16 @@ export const b = new A();`); expectedMap.set("/src/first/first_part3.ts", 1); // outputs - expectedMap.set("/src/first/bin/first-output.js", 2); - expectedMap.set("/src/first/bin/first-output.js.map", 2); - // 1 for reading source File, 2 for forEachEmittedFiles (verifying compiler Options and actual emit)when prepend array is created - expectedMap.set("/src/first/bin/first-output.d.ts", 3); - expectedMap.set("/src/first/bin/first-output.d.ts.map", 2); - expectedMap.set("/src/2/second-output.js", 2); - expectedMap.set("/src/2/second-output.js.map", 2); - // 1 for reading source File, 2 for forEachEmittedFiles (verifying compiler Options and actual emit)when prepend array is created - expectedMap.set("/src/2/second-output.d.ts", 3); - expectedMap.set("/src/2/second-output.d.ts.map", 2); + expectedMap.set("/src/first/bin/first-output.js", 1); + expectedMap.set("/src/first/bin/first-output.js.map", 1); + // 1 for reading source File, 1 for emit + expectedMap.set("/src/first/bin/first-output.d.ts", 2); + expectedMap.set("/src/first/bin/first-output.d.ts.map", 1); + expectedMap.set("/src/2/second-output.js", 1); + expectedMap.set("/src/2/second-output.js.map", 1); + // 1 for reading source File, 1 for emit + expectedMap.set("/src/2/second-output.d.ts", 2); + expectedMap.set("/src/2/second-output.d.ts.map", 1); assert.equal(actualReadFileMap.size, expectedMap.size, `Expected: ${JSON.stringify(arrayFrom(expectedMap.entries()))} \nActual: ${JSON.stringify(arrayFrom(actualReadFileMap.entries()))}`); actualReadFileMap.forEach((value, key) => { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 4353bbcd8fa..a384375b248 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1726,15 +1726,18 @@ declare namespace ts { } interface InputFiles extends Node { kind: SyntaxKind.InputFiles; + javascriptPath?: string; javascriptText: string; javascriptMapPath?: string; javascriptMapText?: string; + declarationPath?: string; declarationText: string; declarationMapPath?: string; declarationMapText?: string; } interface UnparsedSource extends Node { kind: SyntaxKind.UnparsedSource; + fileName?: string; text: string; sourceMapPath?: string; sourceMapText?: string; @@ -3979,9 +3982,11 @@ declare namespace ts { function updateCommaList(node: CommaListExpression, elements: ReadonlyArray): CommaListExpression; function createBundle(sourceFiles: ReadonlyArray, prepends?: ReadonlyArray): Bundle; function createUnparsedSourceFile(text: string): UnparsedSource; + function createUnparsedSourceFile(inputFile: InputFiles, type: "js" | "dts"): UnparsedSource; function createUnparsedSourceFile(text: string, mapPath: string | undefined, map: string | undefined): UnparsedSource; - function createInputFiles(javascript: string, declaration: string): InputFiles; - function createInputFiles(javascript: string, declaration: string, javascriptMapPath: string | undefined, javascriptMapText: string | undefined, declarationMapPath: string | undefined, declarationMapText: string | undefined): InputFiles; + function createInputFiles(javascriptText: string, declarationText: string): InputFiles; + function createInputFiles(readFileText: (path: string) => string | undefined, javascriptPath: string, javascriptMapPath: string | undefined, declarationPath: string, declarationMapPath: string | undefined): InputFiles; + function createInputFiles(javascriptText: string, declarationText: string, javascriptMapPath: string | undefined, javascriptMapText: string | undefined, declarationMapPath: string | undefined, declarationMapText: string | undefined): InputFiles; function updateBundle(node: Bundle, sourceFiles: ReadonlyArray, prepends?: ReadonlyArray): Bundle; function createImmediatelyInvokedFunctionExpression(statements: ReadonlyArray): CallExpression; function createImmediatelyInvokedFunctionExpression(statements: ReadonlyArray, param: ParameterDeclaration, paramValue: Expression): CallExpression; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index d8deb32c0ef..5ae49608353 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1726,15 +1726,18 @@ declare namespace ts { } interface InputFiles extends Node { kind: SyntaxKind.InputFiles; + javascriptPath?: string; javascriptText: string; javascriptMapPath?: string; javascriptMapText?: string; + declarationPath?: string; declarationText: string; declarationMapPath?: string; declarationMapText?: string; } interface UnparsedSource extends Node { kind: SyntaxKind.UnparsedSource; + fileName?: string; text: string; sourceMapPath?: string; sourceMapText?: string; @@ -3979,9 +3982,11 @@ declare namespace ts { function updateCommaList(node: CommaListExpression, elements: ReadonlyArray): CommaListExpression; function createBundle(sourceFiles: ReadonlyArray, prepends?: ReadonlyArray): Bundle; function createUnparsedSourceFile(text: string): UnparsedSource; + function createUnparsedSourceFile(inputFile: InputFiles, type: "js" | "dts"): UnparsedSource; function createUnparsedSourceFile(text: string, mapPath: string | undefined, map: string | undefined): UnparsedSource; - function createInputFiles(javascript: string, declaration: string): InputFiles; - function createInputFiles(javascript: string, declaration: string, javascriptMapPath: string | undefined, javascriptMapText: string | undefined, declarationMapPath: string | undefined, declarationMapText: string | undefined): InputFiles; + function createInputFiles(javascriptText: string, declarationText: string): InputFiles; + function createInputFiles(readFileText: (path: string) => string | undefined, javascriptPath: string, javascriptMapPath: string | undefined, declarationPath: string, declarationMapPath: string | undefined): InputFiles; + function createInputFiles(javascriptText: string, declarationText: string, javascriptMapPath: string | undefined, javascriptMapText: string | undefined, declarationMapPath: string | undefined, declarationMapText: string | undefined): InputFiles; function updateBundle(node: Bundle, sourceFiles: ReadonlyArray, prepends?: ReadonlyArray): Bundle; function createImmediatelyInvokedFunctionExpression(statements: ReadonlyArray): CallExpression; function createImmediatelyInvokedFunctionExpression(statements: ReadonlyArray, param: ParameterDeclaration, paramValue: Expression): CallExpression;