Skip to content

Commit 3866a50

Browse files
committed
Report errors for module generation other than "commonjs" with --resolveJsonModule
Fixes #25517
1 parent a9771e3 commit 3866a50

File tree

45 files changed

+636
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+636
-0
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,6 +2864,10 @@
28642864
"category": "Error",
28652865
"code": 5070
28662866
},
2867+
"Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.": {
2868+
"category": "Error",
2869+
"code": 5071
2870+
},
28672871

28682872
"Generates a sourcemap for each corresponding '.d.ts' file.": {
28692873
"category": "Message",

src/compiler/program.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,10 @@ namespace ts {
25482548
if (getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeJs) {
25492549
createDiagnosticForOptionName(Diagnostics.Option_resolveJsonModule_cannot_be_specified_without_node_module_resolution_strategy, "resolveJsonModule");
25502550
}
2551+
// Any emit other than common js is error
2552+
else if (options.module !== undefined && options.module !== ModuleKind.CommonJS) {
2553+
createDiagnosticForOptionName(Diagnostics.Option_resolveJsonModule_can_only_be_specified_when_module_code_generation_is_commonjs, "resolveJsonModule", "module");
2554+
}
25512555
}
25522556

25532557
// there has to be common source directory if user specified --outdir || --sourceRoot
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error TS5070: Option '--resolveJsonModule' cannot be specified without 'node' module resolution strategy.
2+
tests/cases/compiler/file1.ts(1,1): error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
3+
tests/cases/compiler/file1.ts(1,20): error TS2307: Cannot find module './b.json'.
4+
5+
6+
!!! error TS5070: Option '--resolveJsonModule' cannot be specified without 'node' module resolution strategy.
7+
==== tests/cases/compiler/file1.ts (2 errors) ====
8+
import * as b from './b.json';
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
!!! error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
11+
~~~~~~~~~~
12+
!!! error TS2307: Cannot find module './b.json'.
13+
14+
==== tests/cases/compiler/b.json (0 errors) ====
15+
{
16+
"a": true,
17+
"b": "hello"
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithModuleEmitNone.ts] ////
2+
3+
//// [file1.ts]
4+
import * as b from './b.json';
5+
6+
//// [b.json]
7+
{
8+
"a": true,
9+
"b": "hello"
10+
}
11+
12+
//// [out/file1.js]
13+
"use strict";
14+
exports.__esModule = true;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : Symbol(b, Decl(file1.ts, 0, 6))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : any
4+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithModuleEmitUndefined.ts] ////
2+
3+
//// [file1.ts]
4+
import * as b from './b.json';
5+
6+
//// [b.json]
7+
{
8+
"a": true,
9+
"b": "hello"
10+
}
11+
12+
//// [out/b.json]
13+
{
14+
"a": true,
15+
"b": "hello"
16+
}
17+
//// [out/file1.js]
18+
"use strict";
19+
exports.__esModule = true;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : Symbol(b, Decl(file1.ts, 0, 6))
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
"a": true,
8+
>"a" : Symbol("a", Decl(b.json, 0, 1))
9+
10+
"b": "hello"
11+
>"b" : Symbol("b", Decl(b.json, 1, 14))
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : { "a": boolean; "b": string; }
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; }
8+
9+
"a": true,
10+
>"a" : boolean
11+
>true : true
12+
13+
"b": "hello"
14+
>"b" : string
15+
>"hello" : "hello"
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
2+
3+
4+
!!! error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
5+
==== tests/cases/compiler/file1.ts (0 errors) ====
6+
import * as b from './b.json';
7+
8+
==== tests/cases/compiler/b.json (0 errors) ====
9+
{
10+
"a": true,
11+
"b": "hello"
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithModuleNodeResolutionEmitAmd.ts] ////
2+
3+
//// [file1.ts]
4+
import * as b from './b.json';
5+
6+
//// [b.json]
7+
{
8+
"a": true,
9+
"b": "hello"
10+
}
11+
12+
//// [out/b.json]
13+
{
14+
"a": true,
15+
"b": "hello"
16+
}
17+
//// [out/file1.js]
18+
define(["require", "exports"], function (require, exports) {
19+
"use strict";
20+
exports.__esModule = true;
21+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : Symbol(b, Decl(file1.ts, 0, 6))
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
"a": true,
8+
>"a" : Symbol("a", Decl(b.json, 0, 1))
9+
10+
"b": "hello"
11+
>"b" : Symbol("b", Decl(b.json, 1, 14))
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : { "a": boolean; "b": string; }
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; }
8+
9+
"a": true,
10+
>"a" : boolean
11+
>true : true
12+
13+
"b": "hello"
14+
>"b" : string
15+
>"hello" : "hello"
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
2+
3+
4+
!!! error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
5+
==== tests/cases/compiler/file1.ts (0 errors) ====
6+
import * as b from './b.json';
7+
8+
==== tests/cases/compiler/b.json (0 errors) ====
9+
{
10+
"a": true,
11+
"b": "hello"
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithModuleNodeResolutionEmitEs2015.ts] ////
2+
3+
//// [file1.ts]
4+
import * as b from './b.json';
5+
6+
//// [b.json]
7+
{
8+
"a": true,
9+
"b": "hello"
10+
}
11+
12+
//// [out/b.json]
13+
{
14+
"a": true,
15+
"b": "hello"
16+
}
17+
//// [out/file1.js]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : Symbol(b, Decl(file1.ts, 0, 6))
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
"a": true,
8+
>"a" : Symbol("a", Decl(b.json, 0, 1))
9+
10+
"b": "hello"
11+
>"b" : Symbol("b", Decl(b.json, 1, 14))
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : { "a": boolean; "b": string; }
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; }
8+
9+
"a": true,
10+
>"a" : boolean
11+
>true : true
12+
13+
"b": "hello"
14+
>"b" : string
15+
>"hello" : "hello"
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
2+
3+
4+
!!! error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
5+
==== tests/cases/compiler/file1.ts (0 errors) ====
6+
import * as b from './b.json';
7+
8+
==== tests/cases/compiler/b.json (0 errors) ====
9+
{
10+
"a": true,
11+
"b": "hello"
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithModuleNodeResolutionEmitEsNext.ts] ////
2+
3+
//// [file1.ts]
4+
import * as b from './b.json';
5+
6+
//// [b.json]
7+
{
8+
"a": true,
9+
"b": "hello"
10+
}
11+
12+
//// [out/b.json]
13+
{
14+
"a": true,
15+
"b": "hello"
16+
}
17+
//// [out/file1.js]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : Symbol(b, Decl(file1.ts, 0, 6))
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
"a": true,
8+
>"a" : Symbol("a", Decl(b.json, 0, 1))
9+
10+
"b": "hello"
11+
>"b" : Symbol("b", Decl(b.json, 1, 14))
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : { "a": boolean; "b": string; }
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; }
8+
9+
"a": true,
10+
>"a" : boolean
11+
>true : true
12+
13+
"b": "hello"
14+
>"b" : string
15+
>"hello" : "hello"
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
2+
tests/cases/compiler/file1.ts(1,1): error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
3+
4+
5+
!!! error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
6+
==== tests/cases/compiler/file1.ts (1 errors) ====
7+
import * as b from './b.json';
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
!!! error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
10+
11+
==== tests/cases/compiler/b.json (0 errors) ====
12+
{
13+
"a": true,
14+
"b": "hello"
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithModuleNodeResolutionEmitNone.ts] ////
2+
3+
//// [file1.ts]
4+
import * as b from './b.json';
5+
6+
//// [b.json]
7+
{
8+
"a": true,
9+
"b": "hello"
10+
}
11+
12+
//// [out/b.json]
13+
{
14+
"a": true,
15+
"b": "hello"
16+
}
17+
//// [out/file1.js]
18+
"use strict";
19+
exports.__esModule = true;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : Symbol(b, Decl(file1.ts, 0, 6))
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
"a": true,
8+
>"a" : Symbol("a", Decl(b.json, 0, 1))
9+
10+
"b": "hello"
11+
>"b" : Symbol("b", Decl(b.json, 1, 14))
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as b from './b.json';
3+
>b : { "a": boolean; "b": string; }
4+
5+
=== tests/cases/compiler/b.json ===
6+
{
7+
>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; }
8+
9+
"a": true,
10+
>"a" : boolean
11+
>true : true
12+
13+
"b": "hello"
14+
>"b" : string
15+
>"hello" : "hello"
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
2+
3+
4+
!!! error TS5071: Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs'.
5+
==== tests/cases/compiler/file1.ts (0 errors) ====
6+
import * as b from './b.json';
7+
8+
==== tests/cases/compiler/b.json (0 errors) ====
9+
{
10+
"a": true,
11+
"b": "hello"
12+
}

0 commit comments

Comments
 (0)