Skip to content

Commit 1b63714

Browse files
TypeScript Botrbuckton
TypeScript Bot
authored andcommitted
Cherry-pick PR #32982 into release-3.6 (#32985)
Component commits: c4c6df9 Fix default behavior for transpileModule when fileName not provided
1 parent 03055d2 commit 1b63714

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

src/services/transpile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace ts {
6666
options.noResolve = true;
6767

6868
// if jsx is specified then treat file as .tsx
69-
const inputFileName = transpileOptions.fileName || (options.jsx ? "module.tsx" : "module.ts");
69+
const inputFileName = transpileOptions.fileName || (transpileOptions.compilerOptions && transpileOptions.compilerOptions.jsx ? "module.tsx" : "module.ts");
7070
const sourceFile = createSourceFile(inputFileName, input, options.target!); // TODO: GH#18217
7171
if (transpileOptions.moduleName) {
7272
sourceFile.moduleName = transpileOptions.moduleName;

src/testRunner/unittests/services/transpile.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace ts {
33

44
interface TranspileTestSettings {
55
options?: TranspileOptions;
6+
noSetFileName?: boolean;
67
}
78

89
function transpilesCorrectly(name: string, input: string, testSettings: TranspileTestSettings) {
@@ -30,15 +31,19 @@ namespace ts {
3031

3132
transpileOptions.compilerOptions.sourceMap = true;
3233

33-
if (!transpileOptions.fileName) {
34-
transpileOptions.fileName = transpileOptions.compilerOptions.jsx ? "file.tsx" : "file.ts";
34+
let unitName = transpileOptions.fileName;
35+
if (!unitName) {
36+
unitName = transpileOptions.compilerOptions.jsx ? "file.tsx" : "file.ts";
37+
if (!testSettings.noSetFileName) {
38+
transpileOptions.fileName = unitName;
39+
}
3540
}
3641

3742
transpileOptions.reportDiagnostics = true;
3843

3944
justName = "transpile/" + name.replace(/[^a-z0-9\-. ]/ig, "") + (transpileOptions.compilerOptions.jsx ? Extension.Tsx : Extension.Ts);
4045
toBeCompiled = [{
41-
unitName: transpileOptions.fileName,
46+
unitName,
4247
content: input
4348
}];
4449

@@ -456,5 +461,9 @@ var x = 0;`, {
456461
transpilesCorrectly("Supports 'as const' arrays", `([] as const).forEach(k => console.log(k));`, {
457462
options: { compilerOptions: { module: ModuleKind.CommonJS } }
458463
});
464+
465+
transpilesCorrectly("Infer correct file extension", `const fn = <T>(a: T) => a`, {
466+
noSetFileName: true
467+
});
459468
});
460469
}

tests/baselines/reference/transpile/Infer correct file extension.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/transpile/Infer correct file extension.oldTranspile.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)