Skip to content

Commit 714d734

Browse files
authored
--moduleResoltuion bunlder: Allow import assignment in ambient contexts (#51971)
1 parent 5951ee9 commit 714d734

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42778,7 +42778,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4277842778
// Import equals declaration is deprecated in es6 or above
4277942779
grammarErrorOnNode(node, Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead);
4278042780
}
42781-
else if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler) {
42781+
else if (!(node.flags & NodeFlags.Ambient) && getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler) {
4278242782
grammarErrorOnNode(node, Diagnostics.Import_assignment_is_not_allowed_when_moduleResolution_is_set_to_bundler_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead);
4278342783
}
4278442784
}

tests/baselines/reference/bundlerSyntaxRestrictions.errors.txt

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ error TS2468: Cannot find global value 'Promise'.
88
==== /node_modules/@types/node/index.d.ts (0 errors) ====
99
declare var require: (...args: any[]) => any;
1010

11+
==== /ambient.d.ts (0 errors) ====
12+
declare module "fs" {
13+
export function readFileSync(path: string, encoding?: string): string;
14+
}
15+
declare module "path" {
16+
import fs = require("fs"); // ok
17+
namespace path {
18+
export const sep: string;
19+
}
20+
export = path; // ok
21+
}
22+
1123
==== /mainJs.js (1 errors) ====
1224
import {} from "./a";
1325
import("./a");

tests/baselines/reference/bundlerSyntaxRestrictions.js

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
//// [index.d.ts]
44
declare var require: (...args: any[]) => any;
55

6+
//// [ambient.d.ts]
7+
declare module "fs" {
8+
export function readFileSync(path: string, encoding?: string): string;
9+
}
10+
declare module "path" {
11+
import fs = require("fs"); // ok
12+
namespace path {
13+
export const sep: string;
14+
}
15+
export = path; // ok
16+
}
17+
618
//// [mainJs.js]
719
import {} from "./a";
820
import("./a");

tests/baselines/reference/bundlerSyntaxRestrictions.symbols

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ declare var require: (...args: any[]) => any;
33
>require : Symbol(require, Decl(index.d.ts, 0, 11))
44
>args : Symbol(args, Decl(index.d.ts, 0, 22))
55

6+
=== /ambient.d.ts ===
7+
declare module "fs" {
8+
>"fs" : Symbol("fs", Decl(ambient.d.ts, 0, 0))
9+
10+
export function readFileSync(path: string, encoding?: string): string;
11+
>readFileSync : Symbol(readFileSync, Decl(ambient.d.ts, 0, 21))
12+
>path : Symbol(path, Decl(ambient.d.ts, 1, 33))
13+
>encoding : Symbol(encoding, Decl(ambient.d.ts, 1, 46))
14+
}
15+
declare module "path" {
16+
>"path" : Symbol("path", Decl(ambient.d.ts, 2, 1))
17+
18+
import fs = require("fs"); // ok
19+
>fs : Symbol(fs, Decl(ambient.d.ts, 3, 23))
20+
21+
namespace path {
22+
>path : Symbol(path, Decl(ambient.d.ts, 4, 30))
23+
24+
export const sep: string;
25+
>sep : Symbol(sep, Decl(ambient.d.ts, 6, 20))
26+
}
27+
export = path; // ok
28+
>path : Symbol(path, Decl(ambient.d.ts, 4, 30))
29+
}
30+
631
=== /mainJs.js ===
732
import {} from "./a";
833
import("./a");

tests/baselines/reference/bundlerSyntaxRestrictions.types

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ declare var require: (...args: any[]) => any;
33
>require : (...args: any[]) => any
44
>args : any[]
55

6+
=== /ambient.d.ts ===
7+
declare module "fs" {
8+
>"fs" : typeof import("fs")
9+
10+
export function readFileSync(path: string, encoding?: string): string;
11+
>readFileSync : (path: string, encoding?: string) => string
12+
>path : string
13+
>encoding : string
14+
}
15+
declare module "path" {
16+
>"path" : typeof import("path")
17+
18+
import fs = require("fs"); // ok
19+
>fs : typeof fs
20+
21+
namespace path {
22+
>path : typeof path
23+
24+
export const sep: string;
25+
>sep : string
26+
}
27+
export = path; // ok
28+
>path : typeof path
29+
}
30+
631
=== /mainJs.js ===
732
import {} from "./a";
833
import("./a");

tests/cases/conformance/moduleResolution/bundler/bundlerSyntaxRestrictions.ts

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
// @Filename: /node_modules/@types/node/index.d.ts
77
declare var require: (...args: any[]) => any;
88

9+
// @Filename: /ambient.d.ts
10+
declare module "fs" {
11+
export function readFileSync(path: string, encoding?: string): string;
12+
}
13+
declare module "path" {
14+
import fs = require("fs"); // ok
15+
namespace path {
16+
export const sep: string;
17+
}
18+
export = path; // ok
19+
}
20+
921
// @Filename: /mainJs.js
1022
import {} from "./a";
1123
import("./a");

0 commit comments

Comments
 (0)