Fix default behavior for transpileModule when fileName not provided (#32982)

This commit is contained in:
Ron Buckton 2019-08-19 18:13:39 -07:00 committed by GitHub
parent 6ca9d04b60
commit 1bf218291f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 4 deletions

View File

@ -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;

View File

@ -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 = <T>(a: T) => a`, {
noSetFileName: true
});
});
}

View File

@ -0,0 +1,2 @@
var fn = function (a) { return a; };
//# sourceMappingURL=module.js.map

View File

@ -0,0 +1,2 @@
var fn = function (a) { return a; };
//# sourceMappingURL=module.js.map