Skip to content

Commit 75af4f7

Browse files
committed
transpile text as tsx if jsx option is specified
1 parent 738b26f commit 75af4f7

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-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

+62-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,62 @@ 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 = `import * as React from 'react';\r\n` +
280+
`export default class Test extends React.Component<any, any> {\r\n` +
281+
` constructor(props: any) {\r\n` +
282+
` this.state = {\r\n` +
283+
` text : undefined\r\n` +
284+
` };\r\n` +
285+
` super();\r\n` +
286+
` }\r\n` +
287+
` handleClick(e) {\r\n` +
288+
` e.preventDefault();\r\n` +
289+
` this.setState({\r\n` +
290+
` text : 'just testing'\r\n` +
291+
` });\r\n` +
292+
` }\r\n` +
293+
` render() {\r\n` +
294+
` return (\r\n` +
295+
` <div>\r\n` +
296+
` <a href="#" onClick={this.handleClick}>\r\n` +
297+
` {'test'}\r\n` +
298+
` </a>\r\n` +
299+
` </div>\r\n` +
300+
` );\r\n` +
301+
` }\r\n` +
302+
`}`;
303+
let output = `var __extends = (this && this.__extends) || function (d, b) {\r\n` +
304+
` for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\r\n` +
305+
` function __() { this.constructor = d; }\r\n` +
306+
` d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n` +
307+
`};\r\n` +
308+
`var React = require('react');\r\n` +
309+
`var Test = (function (_super) {\r\n` +
310+
` __extends(Test, _super);\r\n` +
311+
` function Test(props) {\r\n` +
312+
` this.state = {\r\n` +
313+
` text: undefined\r\n` +
314+
` };\r\n` +
315+
` _super.call(this);\r\n` +
316+
` }\r\n` +
317+
` Test.prototype.handleClick = function (e) {\r\n` +
318+
` e.preventDefault();\r\n` +
319+
` this.setState({\r\n` +
320+
` text: 'just testing'\r\n` +
321+
` });\r\n` +
322+
` };\r\n` +
323+
` Test.prototype.render = function () {\r\n` +
324+
` return (React.createElement("div", null, React.createElement("a", {"href": "#", "onClick": this.handleClick}, 'test')));\r\n` +
325+
` };\r\n` +
326+
` return Test;\r\n` +
327+
`})(React.Component);\r\n` +
328+
`exports["default"] = Test;\r\n`;
329+
test(input, {
330+
expectedOutput: output,
331+
options: { compilerOptions: { jsx: JsxEmit.React, newLine: NewLineKind.CarriageReturnLineFeed } }
332+
})
333+
});
277334
});
278335
}

0 commit comments

Comments
 (0)