Skip to content

Commit e1fa3c5

Browse files
committed
Merge pull request microsoft#3982 from Microsoft/sourceMapsInTranspile
add transpileModule function that can return emitted source map
2 parents c5c7e25 + 500cada commit e1fa3c5

12 files changed

+165
-66
lines changed

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,6 @@ namespace ts {
509509
Option_noEmit_cannot_be_specified_with_option_out_or_outDir: { code: 5040, category: DiagnosticCategory.Error, key: "Option 'noEmit' cannot be specified with option 'out' or 'outDir'." },
510510
Option_noEmit_cannot_be_specified_with_option_declaration: { code: 5041, category: DiagnosticCategory.Error, key: "Option 'noEmit' cannot be specified with option 'declaration'." },
511511
Option_project_cannot_be_mixed_with_source_files_on_a_command_line: { code: 5042, category: DiagnosticCategory.Error, key: "Option 'project' cannot be mixed with source files on a command line." },
512-
Option_sourceMap_cannot_be_specified_with_option_isolatedModules: { code: 5043, category: DiagnosticCategory.Error, key: "Option 'sourceMap' cannot be specified with option 'isolatedModules'." },
513512
Option_declaration_cannot_be_specified_with_option_isolatedModules: { code: 5044, category: DiagnosticCategory.Error, key: "Option 'declaration' cannot be specified with option 'isolatedModules'." },
514513
Option_noEmitOnError_cannot_be_specified_with_option_isolatedModules: { code: 5045, category: DiagnosticCategory.Error, key: "Option 'noEmitOnError' cannot be specified with option 'isolatedModules'." },
515514
Option_out_cannot_be_specified_with_option_isolatedModules: { code: 5046, category: DiagnosticCategory.Error, key: "Option 'out' cannot be specified with option 'isolatedModules'." },

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,10 +2025,6 @@
20252025
"category": "Error",
20262026
"code": 5042
20272027
},
2028-
"Option 'sourceMap' cannot be specified with option 'isolatedModules'.": {
2029-
"category": "Error",
2030-
"code": 5043
2031-
},
20322028
"Option 'declaration' cannot be specified with option 'isolatedModules'.": {
20332029
"category": "Error",
20342030
"code": 5044

src/compiler/program.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,6 @@ namespace ts {
602602

603603
function verifyCompilerOptions() {
604604
if (options.isolatedModules) {
605-
if (options.sourceMap) {
606-
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_sourceMap_cannot_be_specified_with_option_isolatedModules));
607-
}
608-
609605
if (options.declaration) {
610606
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_declaration_cannot_be_specified_with_option_isolatedModules));
611607
}

src/services/services.ts

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,18 +1760,31 @@ namespace ts {
17601760
sourceFile.version = version;
17611761
sourceFile.scriptSnapshot = scriptSnapshot;
17621762
}
1763-
1763+
1764+
export interface TranspileOptions {
1765+
compilerOptions?: CompilerOptions;
1766+
fileName?: string;
1767+
reportDiagnostics?: boolean;
1768+
moduleName?: string;
1769+
}
1770+
1771+
export interface TranspileOutput {
1772+
outputText: string;
1773+
diagnostics?: Diagnostic[];
1774+
sourceMapText?: string;
1775+
}
1776+
17641777
/*
17651778
* This function will compile source text from 'input' argument using specified compiler options.
17661779
* If not options are provided - it will use a set of default compiler options.
1767-
* Extra compiler options that will unconditionally be used bu this function are:
1780+
* Extra compiler options that will unconditionally be used by this function are:
17681781
* - isolatedModules = true
17691782
* - allowNonTsExtensions = true
17701783
* - noLib = true
17711784
* - noResolve = true
1772-
*/
1773-
export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[], moduleName?: string): string {
1774-
let options = compilerOptions ? clone(compilerOptions) : getDefaultCompilerOptions();
1785+
*/
1786+
export function transpileModule(input: string, transpileOptions?: TranspileOptions): TranspileOutput {
1787+
let options = transpileOptions.compilerOptions ? clone(transpileOptions.compilerOptions) : getDefaultCompilerOptions();
17751788

17761789
options.isolatedModules = true;
17771790

@@ -1787,23 +1800,30 @@ namespace ts {
17871800
options.noResolve = true;
17881801

17891802
// Parse
1790-
let inputFileName = fileName || "module.ts";
1803+
let inputFileName = transpileOptions.fileName || "module.ts";
17911804
let sourceFile = createSourceFile(inputFileName, input, options.target);
1792-
if (moduleName) {
1793-
sourceFile.moduleName = moduleName;
1805+
if (transpileOptions.moduleName) {
1806+
sourceFile.moduleName = transpileOptions.moduleName;
17941807
}
17951808

17961809
let newLine = getNewLineCharacter(options);
17971810

17981811
// Output
17991812
let outputText: string;
1813+
let sourceMapText: string;
18001814

18011815
// Create a compilerHost object to allow the compiler to read and write files
18021816
let compilerHost: CompilerHost = {
18031817
getSourceFile: (fileName, target) => fileName === inputFileName ? sourceFile : undefined,
18041818
writeFile: (name, text, writeByteOrderMark) => {
1805-
Debug.assert(outputText === undefined, "Unexpected multiple outputs for the file: " + name);
1806-
outputText = text;
1819+
if (fileExtensionIs(name, ".map")) {
1820+
Debug.assert(sourceMapText === undefined, `Unexpected multiple source map outputs for the file '${name}'`);
1821+
sourceMapText = text;
1822+
}
1823+
else {
1824+
Debug.assert(outputText === undefined, "Unexpected multiple outputs for the file: " + name);
1825+
outputText = text;
1826+
}
18071827
},
18081828
getDefaultLibFileName: () => "lib.d.ts",
18091829
useCaseSensitiveFileNames: () => false,
@@ -1813,16 +1833,29 @@ namespace ts {
18131833
};
18141834

18151835
let program = createProgram([inputFileName], options, compilerHost);
1816-
1817-
addRange(/*to*/ diagnostics, /*from*/ program.getSyntacticDiagnostics(sourceFile));
1818-
addRange(/*to*/ diagnostics, /*from*/ program.getOptionsDiagnostics());
1819-
1836+
1837+
let diagnostics: Diagnostic[];
1838+
if (transpileOptions.reportDiagnostics) {
1839+
diagnostics = [];
1840+
addRange(/*to*/ diagnostics, /*from*/ program.getSyntacticDiagnostics(sourceFile));
1841+
addRange(/*to*/ diagnostics, /*from*/ program.getOptionsDiagnostics());
1842+
}
18201843
// Emit
18211844
program.emit();
18221845

18231846
Debug.assert(outputText !== undefined, "Output generation failed");
18241847

1825-
return outputText;
1848+
return { outputText, diagnostics, sourceMapText };
1849+
}
1850+
1851+
/*
1852+
* This is a shortcut function for transpileModule - it accepts transpileOptions as parameters and returns only outputText part of the result.
1853+
*/
1854+
export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[], moduleName?: string): string {
1855+
let output = transpileModule(input, { compilerOptions, fileName, reportDiagnostics: !!diagnostics, moduleName });
1856+
// addRange correctly handles cases when wither 'from' or 'to' argument is missing
1857+
addRange(diagnostics, output.diagnostics);
1858+
return output.outputText;
18261859
}
18271860

18281861
export function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean): SourceFile {

tests/baselines/reference/isolatedModulesSourceMap.errors.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/baselines/reference/isolatedModulesSourceMap.js

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

tests/baselines/reference/isolatedModulesSourceMap.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/isolatedModulesSourceMap.sourcemap.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@ sources: isolatedModulesSourceMap.ts
88
emittedFile:tests/cases/compiler/isolatedModulesSourceMap.js
99
sourceFile:isolatedModulesSourceMap.ts
1010
-------------------------------------------------------------------
11-
>>>export var x;
11+
>>>export var x = 1;
1212
1 >
1313
2 >^^^^^^^^^^^
1414
3 > ^
15-
4 > ^
16-
5 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
15+
4 > ^^^
16+
5 > ^
17+
6 > ^
18+
7 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1719
1 >
1820
>
1921
2 >export var
2022
3 > x
21-
4 > ;
23+
4 > =
24+
5 > 1
25+
6 > ;
2226
1 >Emitted(1, 1) Source(2, 1) + SourceIndex(0)
2327
2 >Emitted(1, 12) Source(2, 12) + SourceIndex(0)
2428
3 >Emitted(1, 13) Source(2, 13) + SourceIndex(0)
25-
4 >Emitted(1, 14) Source(2, 14) + SourceIndex(0)
29+
4 >Emitted(1, 16) Source(2, 16) + SourceIndex(0)
30+
5 >Emitted(1, 17) Source(2, 17) + SourceIndex(0)
31+
6 >Emitted(1, 18) Source(2, 18) + SourceIndex(0)
2632
---
2733
>>>//# sourceMappingURL=isolatedModulesSourceMap.js.map
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/isolatedModulesSourceMap.ts ===
2+
3+
export var x = 1;
4+
>x : Symbol(x, Decl(isolatedModulesSourceMap.ts, 1, 10))
5+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/isolatedModulesSourceMap.ts ===
2+
3+
export var x = 1;
4+
>x : number
5+
>1 : number
6+

0 commit comments

Comments
 (0)