Skip to content

Commit

Permalink
Fix default behavior for transpileModule when fileName not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Aug 19, 2019
1 parent 2cde3b7 commit c4c6df9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/services/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions src/testRunner/unittests/services/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ts {

interface TranspileTestSettings {
options?: TranspileOptions;
noSetFileName?: boolean;
}

function transpilesCorrectly(name: string, input: string, testSettings: TranspileTestSettings) {
Expand Down Expand Up @@ -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
}];

Expand Down Expand Up @@ -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
});
});
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c4c6df9

Please sign in to comment.