mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #5153 from Microsoft/transpileTsx
transpile text as tsx if jsx option is specified
This commit is contained in:
@@ -1850,8 +1850,8 @@ namespace ts {
|
||||
// so pass --noResolve to avoid reporting missing file errors.
|
||||
options.noResolve = true;
|
||||
|
||||
// Parse
|
||||
let inputFileName = transpileOptions.fileName || "module.ts";
|
||||
// if jsx is specified then treat file as .tsx
|
||||
let inputFileName = transpileOptions.fileName || (options.jsx ? "module.tsx" : "module.ts");
|
||||
let sourceFile = createSourceFile(inputFileName, input, options.target);
|
||||
if (transpileOptions.moduleName) {
|
||||
sourceFile.moduleName = transpileOptions.moduleName;
|
||||
|
||||
@@ -43,7 +43,7 @@ module ts {
|
||||
}
|
||||
|
||||
if (canUseOldTranspile) {
|
||||
let diagnostics: Diagnostic[] = [];
|
||||
let diagnostics: Diagnostic[] = [];
|
||||
let transpileResult = transpile(input, transpileOptions.compilerOptions, transpileOptions.fileName, diagnostics, transpileOptions.moduleName);
|
||||
checkDiagnostics(diagnostics, testSettings.expectedDiagnosticCodes);
|
||||
if (testSettings.expectedOutput) {
|
||||
@@ -57,10 +57,10 @@ module ts {
|
||||
}
|
||||
|
||||
if (!transpileOptions.fileName) {
|
||||
transpileOptions.fileName = "file.ts";
|
||||
transpileOptions.fileName = transpileOptions.compilerOptions.jsx ? "file.tsx" : "file.ts";
|
||||
}
|
||||
|
||||
transpileOptions.compilerOptions.sourceMap = true;
|
||||
transpileOptions.compilerOptions.sourceMap = true;
|
||||
let transpileModuleResultWithSourceMap = transpileModule(input, transpileOptions);
|
||||
assert.isTrue(transpileModuleResultWithSourceMap.sourceMapText !== undefined);
|
||||
|
||||
@@ -68,7 +68,7 @@ module ts {
|
||||
let expectedSourceMappingUrlLine = `//# sourceMappingURL=${expectedSourceMapFileName}`;
|
||||
|
||||
if (testSettings.expectedOutput !== undefined) {
|
||||
assert.equal(transpileModuleResultWithSourceMap.outputText, testSettings.expectedOutput + expectedSourceMappingUrlLine);
|
||||
assert.equal(transpileModuleResultWithSourceMap.outputText, testSettings.expectedOutput + expectedSourceMappingUrlLine);
|
||||
}
|
||||
else {
|
||||
// expected output is not set, just verify that output text has sourceMappingURL as a last line
|
||||
@@ -78,7 +78,7 @@ module ts {
|
||||
assert.equal(output, expectedSourceMappingUrlLine);
|
||||
}
|
||||
else {
|
||||
let suffix = getNewLineCharacter(transpileOptions.compilerOptions) + expectedSourceMappingUrlLine
|
||||
let suffix = getNewLineCharacter(transpileOptions.compilerOptions) + expectedSourceMappingUrlLine
|
||||
assert.isTrue(output.indexOf(suffix, output.length - suffix.length) !== -1);
|
||||
}
|
||||
}
|
||||
@@ -274,5 +274,14 @@ var x = 0;`,
|
||||
it("Supports backslashes in file name", () => {
|
||||
test("var x", { expectedOutput: "var x;\r\n", options: { fileName: "a\\b.ts" }});
|
||||
});
|
||||
|
||||
it("transpile file as 'tsx' if 'jsx' is specified", () => {
|
||||
let input = `var x = <div/>`;
|
||||
let output = `var x = React.createElement("div", null);\n`;
|
||||
test(input, {
|
||||
expectedOutput: output,
|
||||
options: { compilerOptions: { jsx: JsxEmit.React, newLine: NewLineKind.LineFeed } }
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user