Skip to content

Commit 23a7e2f

Browse files
committed
Report errors about correctness of the json file
1 parent 4257c2f commit 23a7e2f

File tree

7 files changed

+166
-9
lines changed

7 files changed

+166
-9
lines changed

src/compiler/commandLineParser.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,19 +1049,21 @@ namespace ts {
10491049
* Convert the json syntax tree into the json value
10501050
*/
10511051
export function convertToObject(sourceFile: JsonSourceFile, errors: Push<Diagnostic>): any {
1052-
return convertToObjectWorker(sourceFile, errors, /*knownRootOptions*/ undefined, /*jsonConversionNotifier*/ undefined);
1052+
return convertToObjectWorker(sourceFile, errors, /*returnValue*/ true, /*knownRootOptions*/ undefined, /*jsonConversionNotifier*/ undefined);
10531053
}
10541054

10551055
/**
1056-
* Convert the json syntax tree into the json value
1056+
* Convert the json syntax tree into the json value and report errors
10571057
*/
1058-
function convertToObjectWorker(
1058+
/*@internal*/
1059+
export function convertToObjectWorker(
10591060
sourceFile: JsonSourceFile,
10601061
errors: Push<Diagnostic>,
1062+
returnValue: boolean,
10611063
knownRootOptions: CommandLineOption | undefined,
10621064
jsonConversionNotifier: JsonConversionNotifier | undefined): any {
10631065
if (!sourceFile.statements.length) {
1064-
return {};
1066+
return returnValue ? {} : undefined;
10651067
}
10661068

10671069
return convertPropertyValueToJson(sourceFile.statements[0].expression, knownRootOptions);
@@ -1076,7 +1078,7 @@ namespace ts {
10761078
extraKeyDiagnosticMessage: DiagnosticMessage | undefined,
10771079
parentOption: string | undefined
10781080
): any {
1079-
const result: any = {};
1081+
const result: any = returnValue ? {} : undefined;
10801082
for (const element of node.properties) {
10811083
if (element.kind !== SyntaxKind.PropertyAssignment) {
10821084
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, element, Diagnostics.Property_assignment_expected));
@@ -1097,7 +1099,9 @@ namespace ts {
10971099
}
10981100
const value = convertPropertyValueToJson(element.initializer, option);
10991101
if (typeof keyText !== "undefined") {
1100-
result[keyText] = value;
1102+
if (returnValue) {
1103+
result[keyText] = value;
1104+
}
11011105
// Notify key value set, if user asked for it
11021106
if (jsonConversionNotifier &&
11031107
// Current callbacks are only on known parent option or if we are setting values in the root
@@ -1128,8 +1132,8 @@ namespace ts {
11281132
function convertArrayLiteralExpressionToJson(
11291133
elements: NodeArray<Expression>,
11301134
elementOption: CommandLineOption | undefined
1131-
): any[] {
1132-
return elements.map(element => convertPropertyValueToJson(element, elementOption));
1135+
): any[] | void {
1136+
return (returnValue ? elements.map : elements.forEach).call(elements, (element: Expression) => convertPropertyValueToJson(element, elementOption));
11331137
}
11341138

11351139
function convertPropertyValueToJson(valueExpression: Expression, option: CommandLineOption): any {
@@ -1693,7 +1697,7 @@ namespace ts {
16931697
}
16941698
}
16951699
};
1696-
const json = convertToObjectWorker(sourceFile, errors, getTsconfigRootOptionsMap(), optionsIterator);
1700+
const json = convertToObjectWorker(sourceFile, errors, /*returnValue*/ true, getTsconfigRootOptionsMap(), optionsIterator);
16971701
if (!typeAcquisition) {
16981702
if (typingOptionstypeAcquisition) {
16991703
typeAcquisition = (typingOptionstypeAcquisition.enableAutoDiscovery !== undefined) ?

src/compiler/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ namespace ts {
662662
scriptKind = ensureScriptKind(fileName, scriptKind);
663663
if (scriptKind === ScriptKind.JSON) {
664664
const result = parseJsonText(fileName, sourceText, languageVersion, syntaxCursor, setParentNodes);
665+
convertToObjectWorker(result, result.parseDiagnostics, /*returnValue*/ false, /*knownRootOptions*/ undefined, /*jsonConversionNotifier*/ undefined);
665666
result.typeReferenceDirectives = emptyArray;
666667
result.amdDependencies = emptyArray;
667668
return result;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/compiler/b.json(2,5): error TS1327: String literal with double quotes expected.
2+
3+
4+
==== tests/cases/compiler/file1.ts (0 errors) ====
5+
import b1 = require('./b.json');
6+
let x = b1.a;
7+
import b2 = require('./b.json');
8+
if (x) {
9+
let b = b2.b;
10+
x = (b1.b === b);
11+
}
12+
13+
==== tests/cases/compiler/b.json (1 errors) ====
14+
{
15+
'a': true,
16+
~~~
17+
!!! error TS1327: String literal with double quotes expected.
18+
"b": "hello"
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithErrors.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+
//// [b.json]
19+
{
20+
'a': true,
21+
"b": "hello"
22+
}
23+
//// [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+
}
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @module: commonjs
2+
// @outdir: out/
3+
// @allowJs: true
4+
5+
// @Filename: file1.ts
6+
import b1 = require('./b.json');
7+
let x = b1.a;
8+
import b2 = require('./b.json');
9+
if (x) {
10+
let b = b2.b;
11+
x = (b1.b === b);
12+
}
13+
14+
// @Filename: b.json
15+
{
16+
'a': true,
17+
"b": "hello"
18+
}

0 commit comments

Comments
 (0)