8000 Check source map files are being overwritten · fleurdeswift/TypeScript@34706df · GitHub
[go: up one dir, main page]

Skip to content

Commit 34706df

Browse files
committed
Check source map files are being overwritten
1 parent 16becda commit 34706df

21 files changed

+495
-14
lines changed

src/compiler/emitter.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
376376
return true;
377377
}
378378

379-
function emitJavaScript(jsFilePath: string, root?: SourceFile) {
379+
function emitJavaScript(jsFilePath: string, sourceMapFilePath: string, root?: SourceFile) {
380380
let writer = createTextWriter(newLine);
381381
let { write, writeTextOfNode, writeLine, increaseIndent, decreaseIndent } = writer;
382382

@@ -872,24 +872,23 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
872872
if (compilerOptions.inlineSourceMap) {
873873
// Encode the sourceMap into the sourceMap url
874874
let base64SourceMapText = convertToBase64(sourceMapText);
875-
sourceMapUrl = `//# sourceMappingURL=data:application/json;base64,${base64SourceMapText}`;
875+
sourceMapData.jsSourceMappingURL = `data:application/json;base64,${base64SourceMapText}`;
876876
}
877877
else {
878878
// Write source map file
879879
writeFile(host, diagnostics, sourceMapData.sourceMapFilePath, sourceMapText, /*writeByteOrderMark*/ false);
880-
sourceMapUrl = `//# sourceMappingURL=${sourceMapData.jsSourceMappingURL}`;
881880
}
881+
sourceMapUrl = `//# sourceMappingURL=${sourceMapData.jsSourceMappingURL}`;
882882

883883
// Write sourcemap url to the js file and write the js file
884884
writeJavaScriptFile(emitOutput + sourceMapUrl, writeByteOrderMark);
885885
}
886886

887887
// Initialize source map data
888-
let sourceMapJsFile = getBaseFileName(normalizeSlashes(jsFilePath));
889888
sourceMapData = {
890-
sourceMapFilePath: jsFilePath + ".map",
891-
jsSourceMappingURL: sourceMapJsFile + ".map",
892-
sourceMapFile: sourceMapJsFile,
889+
sourceMapFilePath: sourceMapFilePath,
890+
jsSourceMappingURL: !compilerOptions.inlineSourceMap ? getBaseFileName(normalizeSlashes(sourceMapFilePath)) : undefined,
891+
sourceMapFile: getBaseFileName(normalizeSlashes(jsFilePath)),
893892
sourceMapSourceRoot: compilerOptions.sourceRoot || "",
894893
sourceMapSources: [],
895894
inputSourceFileNames: [],
@@ -7596,9 +7595,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
75967595
}
75977596
}
75987597

7599-
function emitFile({ jsFilePath, declarationFilePath}: { jsFilePath: string, declarationFilePath: string }, sourceFile?: SourceFile) {
7600-
if (!host.isEmitBlocked(jsFilePath)) {
7601-
emitJavaScript(jsFilePath, sourceFile);
7598+
function emitFile({ jsFilePath, sourceMapFilePath, declarationFilePath}: { jsFilePath: string, sourceMapFilePath: string, declarationFilePath: string }, sourceFile?: SourceFile) {
7599+
// Make sure not to write js File and source map file if any of them cannot be written
7600+
if (!host.isEmitBlocked(jsFilePath) && (!sourceMapFilePath || !host.isEmitBlocked(sourceMapFilePath))) {
7601+
emitJavaScript(jsFilePath, sourceMapFilePath, sourceFile);
76027602
}
76037603

76047604
if (compilerOptions.declaration) {

src/compiler/program.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,11 +1312,14 @@ namespace ts {
13121312

13131313
// Build map of files seen
13141314
for (let file of files) {
1315-
let { jsFilePath, declarationFilePath } = getEmitFileNames(file, emitHost);
1315+
let { jsFilePath, sourceMapFilePath, declarationFilePath } = getEmitFileNames(file, emitHost);
13161316
if (jsFilePath) {
13171317
let filesEmittingJsFilePath = lookUp(emitFilesSeen, jsFilePath);
13181318
if (!filesEmittingJsFilePath) {
13191319
emitFilesSeen[jsFilePath] = [file];
1320+
if (sourceMapFilePath) {
1321+
emitFilesSeen[sourceMapFilePath] = [file];
1322+
}
13201323
if (options.declaration) {
13211324
emitFilesSeen[declarationFilePath] = [file];
13221325
}

src/compiler/utilities.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,7 @@ namespace ts {
18411841
sourceFile.languageVariant === LanguageVariant.JSX && options.jsx === JsxEmit.Preserve ? ".jsx" : ".js");
18421842
return {
18431843
jsFilePath,
1844+
sourceMapFilePath: getSourceMapFilePath(jsFilePath, options),
18441845
declarationFilePath: getDeclarationEmitFilePath(jsFilePath, options)
18451846
};
18461847
}
@@ -1850,18 +1851,25 @@ namespace ts {
18501851
}
18511852
return {
18521853
jsFilePath: undefined,
1854+
sourceMapFilePath: undefined,
18531855
declarationFilePath: undefined
18541856
};
18551857
}
18561858

18571859
export function getBundledEmitFileNames(options: CompilerOptions) {
18581860
let jsFilePath = options.outFile || options.out;
1861+
18591862
return {
18601863
jsFilePath,
1864+
sourceMapFilePath: getSourceMapFilePath(jsFilePath, options),
18611865
declarationFilePath: getDeclarationEmitFilePath(jsFilePath, options)
18621866
};
18631867
}
18641868

1869+
function getSourceMapFilePath(jsFilePath: string, options: CompilerOptions) {
1870+
return options.sourceMap ? jsFilePath + ".map" : undefined;
1871+
}
1872+
18651873
function getDeclarationEmitFilePath(jsFilePath: string, options: CompilerOptions) {
18661874
return options.declaration ? removeFileExtension(jsFilePath, getExtensionsToRemoveForEmitPath(options)) + ".d.ts" : undefined;
18671875
}

tests/baselines/reference/inlineSourceMap.sourcemap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
===================================================================
22
JsFile: inlineSourceMap.js
3-
mapUrl: inlineSourceMap.js.map
3+
mapUrl: data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lU291cmNlTWFwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiaW5saW5lU291cmNlTWFwLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLElBQUksQ0FBQyxHQUFHLENBQUMsQ0FBQztBQUNWLE9BQU8sQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDLENBQUMifQ==
44
sourceRoot:
55
sources: inlineSourceMap.ts
66
===================================================================

tests/baselines/reference/inlineSourceMap2.sourcemap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
===================================================================
22
JsFile: outfile.js
3-
mapUrl: file:///folder/outfile.js.map
3+
mapUrl: data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3V0ZmlsZS5qcyIsInNvdXJjZVJvb3QiOiJmaWxlOi8vL2ZvbGRlci8iLCJzb3VyY2VzIjpbImlubGluZVNvdXJjZU1hcDIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsdUJBQXVCO0FBRXZCLElBQUksQ0FBQyxHQUFHLENBQUMsQ0FBQztBQUNWLE9BQU8sQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDLENBQUMifQ==
44
sourceRoot: file:///folder/
55
sources: inlineSourceMap2.ts
66
===================================================================

tests/baselines/reference/inlineSources2.sourcemap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
===================================================================
22
JsFile: out.js
3-
mapUrl: out.js.map
3+
mapUrl: data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3V0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsidGVzdHMvY2FzZXMvY29tcGlsZXIvYS50cyIsInRlc3RzL2Nhc2VzL2NvbXBpbGVyL2IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsSUFBSSxDQUFDLEdBQUcsQ0FBQyxDQUFDO0FBQ1YsT0FBTyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUMsQ0FBQztBQ0ZmLElBQUksQ0FBQyxHQUFHLENBQUMsQ0FBQztBQUNWLE9BQU8sQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDLENBQUMiLCJzb3VyY2VzQ29udGVudCI6WyJcbnZhciBhID0gMDtcbmNvbnNvbGUubG9nKGEpO1xuIiwidmFyIGIgPSAwO1xuY29uc29sZS5sb2coYik7Il19
44
sourceRoot:
55
sources: tests/cases/compiler/a.ts,tests/cases/compiler/b.ts
66
sourcesContent: ["\nvar a = 0;\nconsole.log(a);\n","var b = 0;\nconsole.log(b);"]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error TS5055: Cannot write file 'tests/cases/compiler/b.js' which is one of the input files.
2+
error TS5055: Cannot write file 'tests/cases/compiler/b.js.map' which is one of the input files.
3+
4+
5+
!!! error TS5055: Cannot write file 'tests/cases/compiler/b.js' which is one of the input files.
6+
!!! error TS5055: Cannot write file 'tests/cases/compiler/b.js.map' which is one of the input files.
7+
==== tests/cases/compiler/a.ts (0 errors) ====
8+
9+
class c {
10+
}
11+
12+
==== tests/cases/compiler/b.js.map (0 errors) ====
13+
function foo() {
14+
}
15+
16+
==== tests/cases/compiler/b.js (0 errors) ====
17+
function bar() {
18+
}

tests/baselines/reference/jsFileCompilationWithMapFileAsJs.js

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

tests/baselines/reference/jsFileCompilationWithMapFileAsJs.js.map

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
===================================================================
2+
JsFile: a.js
3+
mapUrl: a.js.map
4+
sourceRoot:
5+
sources: a.ts
6+
===================================================================
7+
-------------------------------------------------------------------
8+
emittedFile:tests/cases/compiler/a.js
9+
sourceFile:a.ts
10+
-------------------------------------------------------------------
11+
>>>var c = (function () {
12+
1 >
13+
2 >^^^^^^^^^^^^^^^^^^^->
14+
1 >
15+
>
16+
1 >Emitted(1, 1) Source(2, 1) + SourceIndex(0)
17+
---
18+
>>> 97AE function c() {
19+
1->^^^^
20+
2 > ^^->
21+
1->
22+
1->Emitted(2, 5) Source(2, 1) + SourceIndex(0) name (c)
23+
---
24+
>>> }
25+
1->^^^^
26+
2 > ^
27+
3 > ^^^^^^^^^->
28+
1->class c {
29+
>
30+
2 > }
31+
1->Emitted(3, 5) Source(3, 1) + SourceIndex(0) name (c.constructor)
32+
2 >Emitted(3, 6) Source(3, 2) + SourceIndex(0) name (c.constructor)
33+
---
34+
>>> return c;
35+
1->^^^^
36+
2 > ^^^^^^^^
37+
1->
38+
2 > }
39+
1->Emitted(4, 5) Source(3, 1) + SourceIndex(0) name (c)
40+
2 >Emitted(4, 13) Source(3, 2) + SourceIndex(0) name (c)
41+
---
42+
>>>})();
43+
1 >
44+
2 >^
45+
3 >
46+
4 > ^^^^
47+
5 > ^^^^^^^^^^^^^^^^^^^^^^^->
48+
1 >
49+
2 >}
50+
3 >
51+
4 > class c {
52+
> }
53+
1 >Emitted(5, 1) Source(3, 1) + SourceIndex(0) name (c)
54+
2 >Emitted(5, 2) Source(3, 2) + SourceIndex(0) name (c)
55+
3 >Emitted(5, 2) Source(2, 1) + SourceIndex(0)
56+
4 >Emitted(5, 6) Source(3, 2) + SourceIndex(0)
57+
---
58+
>>>//# sourceMappingURL=a.js.map===================================================================
59+
JsFile: b.js.js
60+
mapUrl: b.js.js.map
61+
sourceRoot:
62+
sources: b.js.map
63+
===================================================================
64+
-------------------------------------------------------------------
65+
emittedFile:tests/cases/compiler/b.js.js
66+
sourceFile:b.js.map
67+
-------------------------------------------------------------------
68+
>>>function foo() {
69+
1 >
70+
2 >^^->
71+
1 >
72+
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
73+
---
74+
>>>}
75+
1->
76+
2 >^
77+
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
78+
1->function foo() {
79+
>
80+
2 >}
81+
1->Emitted(2, 1) Source(2, 1) + SourceIndex(0) name (foo)
82+
2 >Emitted(2, 2) Source(2, 2) + SourceIndex(0) name (foo)
83+
---
84+
>>>//# sourceMappingURL=b.js.js.map

0 commit comments

Comments
 (0)
0