From c4c6df968ed9008e51975d2e0773d36f70abe341 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 19 Aug 2019 15:40:15 -0700 Subject: [PATCH] Fix default behavior for transpileModule when fileName not provided --- src/services/transpile.ts | 2 +- src/testRunner/unittests/services/transpile.ts | 15 ++++++++++++--- .../transpile/Infer correct file extension.js | 2 ++ .../Infer correct file extension.oldTranspile.js | 2 ++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/transpile/Infer correct file extension.js create mode 100644 tests/baselines/reference/transpile/Infer correct file extension.oldTranspile.js diff --git a/src/services/transpile.ts b/src/services/transpile.ts index bdc27ac5e22cf..51d1582c564c9 100644 --- a/src/services/transpile.ts +++ b/src/services/transpile.ts @@ -66,7 +66,7 @@ namespace ts { options.noResolve = true; // if jsx is specified then treat file as .tsx - const inputFileName = transpileOptions.fileName || (options.jsx ? "module.tsx" : "module.ts"); + const inputFileName = transpileOptions.fileName || (transpileOptions.compilerOptions && transpileOptions.compilerOptions.jsx ? "module.tsx" : "module.ts"); const sourceFile = createSourceFile(inputFileName, input, options.target!); // TODO: GH#18217 if (transpileOptions.moduleName) { sourceFile.moduleName = transpileOptions.moduleName; diff --git a/src/testRunner/unittests/services/transpile.ts b/src/testRunner/unittests/services/transpile.ts index 6e8f3bcac9c6d..a86f4bdfc9e60 100644 --- a/src/testRunner/unittests/services/transpile.ts +++ b/src/testRunner/unittests/services/transpile.ts @@ -3,6 +3,7 @@ namespace ts { interface TranspileTestSettings { options?: TranspileOptions; + noSetFileName?: boolean; } function transpilesCorrectly(name: string, input: string, testSettings: TranspileTestSettings) { @@ -30,15 +31,19 @@ namespace ts { transpileOptions.compilerOptions.sourceMap = true; - if (!transpileOptions.fileName) { - transpileOptions.fileName = transpileOptions.compilerOptions.jsx ? "file.tsx" : "file.ts"; + let unitName = transpileOptions.fileName; + if (!unitName) { + unitName = transpileOptions.compilerOptions.jsx ? "file.tsx" : "file.ts"; + if (!testSettings.noSetFileName) { + transpileOptions.fileName = unitName; + } } transpileOptions.reportDiagnostics = true; justName = "transpile/" + name.replace(/[^a-z0-9\-. ]/ig, "") + (transpileOptions.compilerOptions.jsx ? Extension.Tsx : Extension.Ts); toBeCompiled = [{ - unitName: transpileOptions.fileName, + unitName, content: input }]; @@ -456,5 +461,9 @@ var x = 0;`, { transpilesCorrectly("Supports 'as const' arrays", `([] as const).forEach(k => console.log(k));`, { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); + + transpilesCorrectly("Infer correct file extension", `const fn = (a: T) => a`, { + noSetFileName: true + }); }); } diff --git a/tests/baselines/reference/transpile/Infer correct file extension.js b/tests/baselines/reference/transpile/Infer correct file extension.js new file mode 100644 index 0000000000000..76701b98d64fc --- /dev/null +++ b/tests/baselines/reference/transpile/Infer correct file extension.js @@ -0,0 +1,2 @@ +var fn = function (a) { return a; }; +//# sourceMappingURL=module.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Infer correct file extension.oldTranspile.js b/tests/baselines/reference/transpile/Infer correct file extension.oldTranspile.js new file mode 100644 index 0000000000000..76701b98d64fc --- /dev/null +++ b/tests/baselines/reference/transpile/Infer correct file extension.oldTranspile.js @@ -0,0 +1,2 @@ +var fn = function (a) { return a; }; +//# sourceMappingURL=module.js.map \ No newline at end of file