Skip to content

Commit 0d95d04

Browse files
committed
Merge pull request #5153 from Microsoft/transpileTsx
transpile text as tsx if jsx option is specified
2 parents 738b26f + fb1d2cf commit 0d95d04

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/services/services.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1850,8 +1850,8 @@ namespace ts {
18501850
// so pass --noResolve to avoid reporting missing file errors.
18511851
options.noResolve = true;
18521852

1853-
// Parse
1854-
let inputFileName = transpileOptions.fileName || "module.ts";
1853+
// if jsx is specified then treat file as .tsx
1854+
let inputFileName = transpileOptions.fileName || (options.jsx ? "module.tsx" : "module.ts");
18551855
let sourceFile = createSourceFile(inputFileName, input, options.target);
18561856
if (transpileOptions.moduleName) {
18571857
sourceFile.moduleName = transpileOptions.moduleName;

tests/cases/unittests/transpile.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module ts {
4343
}
4444

4545
if (canUseOldTranspile) {
46-
let diagnostics: Diagnostic[] = [];
46+
let diagnostics: Diagnostic[] = [];
4747
let transpileResult = transpile(input, transpileOptions.compilerOptions, transpileOptions.fileName, diagnostics, transpileOptions.moduleName);
4848
checkDiagnostics(diagnostics, testSettings.expectedDiagnosticCodes);
4949
if (testSettings.expectedOutput) {
@@ -57,18 +57,18 @@ module ts {
5757
}
5858

5959
if (!transpileOptions.fileName) {
60-
transpileOptions.fileName = "file.ts";
60+
transpileOptions.fileName = transpileOptions.compilerOptions.jsx ? "file.tsx" : "file.ts";
6161
}
6262

63-
transpileOptions.compilerOptions.sourceMap = true;
63+
transpileOptions.compilerOptions.sourceMap = true;
6464
let transpileModuleResultWithSourceMap = transpileModule(input, transpileOptions);
6565
assert.isTrue(transpileModuleResultWithSourceMap.sourceMapText !== undefined);
6666

6767
let expectedSourceMapFileName = removeFileExtension(getBaseFileName(normalizeSlashes(transpileOptions.fileName))) + ".js.map";
6868
let expectedSourceMappingUrlLine = `//# sourceMappingURL=${expectedSourceMapFileName}`;
6969

7070
if (testSettings.expectedOutput !== undefined) {
71-
assert.equal(transpileModuleResultWithSourceMap.outputText, testSettings.expectedOutput + expectedSourceMappingUrlLine);
71+
assert.equal(transpileModuleResultWithSourceMap.outputText, testSettings.expectedOutput + expectedSourceMappingUrlLine);
7272
}
7373
else {
7474
// expected output is not set, just verify that output text has sourceMappingURL as a last line
@@ -78,7 +78,7 @@ module ts {
7878
assert.equal(output, expectedSourceMappingUrlLine);
7979
}
8080
else {
81-
let suffix = getNewLineCharacter(transpileOptions.compilerOptions) + expectedSourceMappingUrlLine
81+
let suffix = getNewLineCharacter(transpileOptions.compilerOptions) + expectedSourceMappingUrlLine
8282
assert.isTrue(output.indexOf(suffix, output.length - suffix.length) !== -1);
8383
}
8484
}
@@ -274,5 +274,14 @@ var x = 0;`,
274274
it("Supports backslashes in file name", () => {
275275
test("var x", { expectedOutput: "var x;\r\n", options: { fileName: "a\\b.ts" }});
276276
});
277+
278+
it("transpile file as 'tsx' if 'jsx' is specified", () => {
279+
let input = `var x = <div/>`;
280+
let output = `var x = React.createElement("div", null);\n`;
281+
test(input, {
282+
expectedOutput: output,
283+
options: { compilerOptions: { jsx: JsxEmit.React, newLine: NewLineKind.LineFeed } }
284+
})
285+
});
277286
});
278287
}

0 commit comments

Comments
 (0)