Skip to content

Commit 825c8e7

Browse files
authored
Merge pull request #24656 from Microsoft/jsonSourceMaps
[release-2.9] Disable source maps and declaration emit for the json module
2 parents a56fe76 + c619875 commit 825c8e7

15 files changed

+453
-11
lines changed

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace ts {
5050
}
5151
else {
5252
const jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, getOutputExtension(sourceFile, options));
53-
const sourceMapFilePath = getSourceMapFilePath(jsFilePath, options);
53+
const sourceMapFilePath = isJsonSourceFile(sourceFile) ? undefined : getSourceMapFilePath(jsFilePath, options);
5454
// For legacy reasons (ie, we have baselines capturing the behavior), js files don't report a .d.ts output path - this would only matter if `declaration` and `allowJs` were both on, which is currently an error
5555
const isJs = isSourceFileJavaScript(sourceFile);
5656
const declarationFilePath = ((forceDtsPaths || options.declaration) && !isJs) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined;

src/compiler/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ namespace ts {
717717
initializeState(sourceText, languageVersion, syntaxCursor, ScriptKind.JSON);
718718
// Set source file so that errors will be reported with this file name
719719
sourceFile = createSourceFile(fileName, ScriptTarget.ES2015, ScriptKind.JSON, /*isDeclaration*/ false);
720+
sourceFile.flags = contextFlags;
720721

721722
// Prime the scanner.
722723
nextToken();

src/compiler/sourcemap.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ namespace ts {
125125
* @param sourceFileOrBundle The input source file or bundle for the program.
126126
*/
127127
function initialize(filePath: string, sourceMapFilePath: string, sourceFileOrBundle: SourceFile | Bundle, outputSourceMapDataList?: SourceMapData[]) {
128-
if (disabled) {
128+
if (disabled || fileExtensionIs(filePath, Extension.Json)) {
129129
return;
130130
}
131131

@@ -270,7 +270,7 @@ namespace ts {
270270
* @param pos The position.
271271
*/
272272
function emitPos(pos: number) {
273-
if (disabled || positionIsSynthesized(pos)) {
273+
if (disabled || positionIsSynthesized(pos) || isJsonSourceMapSource(currentSource)) {
274274
return;
275275
}
276276

@@ -328,7 +328,7 @@ namespace ts {
328328
* @param emitCallback The callback used to emit the node.
329329
*/
330330
function emitNodeWithSourceMap(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) {
331-
if (disabled) {
331+
if (disabled || isInJsonFile(node)) {
332332
return emitCallback(hint, node);
333333
}
334334

@@ -381,7 +381,7 @@ namespace ts {
381381
* @param emitCallback The callback used to emit the token.
382382
*/
383383
function emitTokenWithSourceMap(node: Node, token: SyntaxKind, writer: (s: string) => void, tokenPos: number, emitCallback: (token: SyntaxKind, writer: (s: string) => void, tokenStartPos: number) => number) {
384-
if (disabled) {
384+
if (disabled || isInJsonFile(node)) {
385385
return emitCallback(token, writer, tokenPos);
386386
}
387387

@@ -404,6 +404,10 @@ namespace ts {
404404
return tokenPos;
405405
}
406406

407+
function isJsonSourceMapSource(sourceFile: SourceMapSource) {
408+
return fileExtensionIs(sourceFile.fileName, Extension.Json);
409+
}
410+
407411
/**
408412
* Set the current source file.
409413
*
@@ -417,6 +421,10 @@ namespace ts {
417421
currentSource = sourceFile;
418422
currentSourceText = currentSource.text;
419423

424+
if (isJsonSourceMapSource(sourceFile)) {
425+
return;
426+
}
427+
420428
// Add the file to tsFilePaths
421429
// If sourceroot option: Use the relative path corresponding to the common directory path
422430
// otherwise source locations relative to map file location
@@ -446,7 +454,7 @@ namespace ts {
446454
* Gets the text for the source map.
447455
*/
448456
function getText() {
449-
if (disabled) {
457+
if (disabled || isJsonSourceMapSource(currentSource)) {
450458
return;
451459
}
452460

@@ -467,7 +475,7 @@ namespace ts {
467475
* Gets the SourceMappingURL for the source map.
468476
*/
469477
function getSourceMappingURL() {
470-
if (disabled) {
478+
if (disabled || isJsonSourceMapSource(currentSource)) {
471479
return;
472480
}
473481

@@ -519,4 +527,4 @@ namespace ts {
519527

520528
return encodedStr;
521529
}
522-
}
530+
}

src/harness/compiler.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ namespace compiler {
183183

184184
public getSourceMapRecord(): string | undefined {
185185
if (this.result.sourceMaps && this.result.sourceMaps.length > 0) {
186-
return Harness.SourceMapRecorder.getSourceMapRecord(this.result.sourceMaps, this.program, Array.from(this.js.values()), Array.from(this.dts.values()));
186+
return Harness.SourceMapRecorder.getSourceMapRecord(this.result.sourceMaps, this.program, Array.from(this.js.values()).filter(d => !ts.fileExtensionIs(d.file, ts.Extension.Json)), Array.from(this.dts.values()));
187187
}
188188
}
189189

@@ -216,6 +216,16 @@ namespace compiler {
216216
}
217217
return vpath.changeExtension(path, ext);
218218
}
219+
220+
public getNumberOfJsFiles() {
221+
let count = this.js.size;
222+
this.js.forEach(document => {
223+
if (ts.fileExtensionIs(document.file, ts.Extension.Json)) {
224+
count--;
225+
}
226+
});
227+
return count;
228+
}
219229
}
220230

221231
export function compileFiles(host: fakes.CompilerHost, rootFiles: string[] | undefined, compilerOptions: ts.CompilerOptions): CompilationResult {

src/harness/harness.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ namespace Harness {
12721272
throw new Error("Only declaration files should be generated when emitDeclarationOnly:true");
12731273
}
12741274
}
1275-
else if (result.dts.size !== result.js.size) {
1275+
else if (result.dts.size !== result.getNumberOfJsFiles()) {
12761276
throw new Error("There were no errors and declFiles generated did not match number of js files generated");
12771277
}
12781278
}
@@ -1639,7 +1639,7 @@ namespace Harness {
16391639
return;
16401640
}
16411641
else if (options.sourceMap || declMaps) {
1642-
if (result.maps.size !== (result.js.size * (declMaps && options.sourceMap ? 2 : 1))) {
1642+
if (result.maps.size !== (result.getNumberOfJsFiles() * (declMaps && options.sourceMap ? 2 : 1))) {
16431643
throw new Error("Number of sourcemap files should be same as js files.");
16441644
}
16451645

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithDeclaration.ts] ////
2+
3+
//// [file1.ts]
4+
import b1 = require('./b.json');
5+
let x = b1.a;
6+
import b2 = require('./b.json');
7+
if (x) {
8+
let b = b2.b;
9+
x = (b1.b === b);
10+
}
11+
12+
//// [b.json]
13+
{
14+
"a": true,
15+
"b": "hello"
16+
}
17+
18+
//// [out/b.json]
19+
{
20+
"a": true,
21+
"b": "hello"
22+
}
23+
//// [out/file1.js]
24+
"use strict";
25+
exports.__esModule = true;
26+
var b1 = require("./b.json");
27+
var x = b1.a;
28+
var b2 = require("./b.json");
29+
if (x) {
30+
var b = b2.b;
31+
x = (b1.b === b);
32+
}
33+
34+
35+
//// [out/file1.d.ts]
36+
export {};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import b1 = require('./b.json');
3+
>b1 : Symbol(b1, Decl(file1.ts, 0, 0))
4+
5+
let x = b1.a;
6+
>x : Symbol(x, Decl(file1.ts, 1, 3))
7+
>b1.a : Symbol("a", Decl(b.json, 0, 1))
8+
>b1 : Symbol(b1, Decl(file1.ts, 0, 0))
9+
>a : Symbol("a", Decl(b.json, 0, 1))
10+
11+
import b2 = require('./b.json');
12+
>b2 : Symbol(b2, Decl(file1.ts, 1, 13))
13+
14+
if (x) {
15+
>x : Symbol(x, Decl(file1.ts, 1, 3))
16+
17+
let b = b2.b;
18+
>b : Symbol(b, Decl(file1.ts, 4, 7))
19+
>b2.b : Symbol("b", Decl(b.json, 1, 14))
20+
>b2 : Symbol(b2, Decl(file1.ts, 1, 13))
21+
>b : Symbol("b", Decl(b.json, 1, 14))
22+
23+
x = (b1.b === b);
24+
>x : Symbol(x, Decl(file1.ts, 1, 3))
25+
>b1.b : Symbol("b", Decl(b.json, 1, 14))
26+
>b1 : Symbol(b1, Decl(file1.ts, 0, 0))
27+
>b : Symbol("b", Decl(b.json, 1, 14))
28+
>b : Symbol(b, Decl(file1.ts, 4, 7))
29+
}
30+
31+
=== tests/cases/compiler/b.json ===
32+
{
33+
"a": true,
34+
>"a" : Symbol("a", Decl(b.json, 0, 1))
35+
36+
"b": "hello"
37+
>"b" : Symbol("b", Decl(b.json, 1, 14))
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import b1 = require('./b.json');
3+
>b1 : { "a": boolean; "b": string; }
4+
5+
let x = b1.a;
6+
>x : boolean
7+
>b1.a : boolean
8+
>b1 : { "a": boolean; "b": string; }
9+
>a : boolean
10+
11+
import b2 = require('./b.json');
12+
>b2 : { "a": boolean; "b": string; }
13+
14+
if (x) {
15+
>x : boolean
16+
17+
let b = b2.b;
18+
>b : string
19+
>b2.b : string
20+
>b2 : { "a": boolean; "b": string; }
21+
>b : string
22+
23+
x = (b1.b === b);
24+
>x = (b1.b === b) : boolean
25+
>x : boolean
26+
>(b1.b === b) : boolean
27+
>b1.b === b : boolean
28+
>b1.b : string
29+
>b1 : { "a": boolean; "b": string; }
30+
>b : string
31+
>b : string
32+
}
33+
34+
=== tests/cases/compiler/b.json ===
35+
{
36+
>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; }
37+
38+
"a": true,
39+
>"a" : boolean
40+
>true : true
41+
42+
"b": "hello"
43+
>"b" : string
44+
>"hello" : "hello"
45+
}

tests/baselines/reference/requireOfJsonFileWithSourceMap.js

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

tests/baselines/reference/requireOfJsonFileWithSourceMap.js.map

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

0 commit comments

Comments
 (0)