Skip to content

--moduleResolution bundler: Allow import assignment in ambient contexts #51971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42778,7 +42778,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Import equals declaration is deprecated in es6 or above
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);
}
else if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler) {
else if (!(node.flags & NodeFlags.Ambient) && getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems okay - I might try to reorder this to make it consistent with the above check

(also, you might want to just hoist the node.flags up a level.

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);
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/bundlerSyntaxRestrictions.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ error TS2468: Cannot find global value 'Promise'.
==== /node_modules/@types/node/index.d.ts (0 errors) ====
declare var require: (...args: any[]) => any;

==== /ambient.d.ts (0 errors) ====
declare module "fs" {
export function readFileSync(path: string, encoding?: string): string;
}
declare module "path" {
import fs = require("fs"); // ok
namespace path {
export const sep: string;
}
export = path; // ok
}

==== /mainJs.js (1 errors) ====
import {} from "./a";
import("./a");
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/bundlerSyntaxRestrictions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
//// [index.d.ts]
declare var require: (...args: any[]) => any;

//// [ambient.d.ts]
declare module "fs" {
export function readFileSync(path: string, encoding?: string): string;
}
declare module "path" {
import fs = require("fs"); // ok
namespace path {
export const sep: string;
}
export = path; // ok
}

//// [mainJs.js]
import {} from "./a";
import("./a");
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/bundlerSyntaxRestrictions.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ declare var require: (...args: any[]) => any;
>require : Symbol(require, Decl(index.d.ts, 0, 11))
>args : Symbol(args, Decl(index.d.ts, 0, 22))

=== /ambient.d.ts ===
declare module "fs" {
>"fs" : Symbol("fs", Decl(ambient.d.ts, 0, 0))

export function readFileSync(path: string, encoding?: string): string;
>readFileSync : Symbol(readFileSync, Decl(ambient.d.ts, 0, 21))
>path : Symbol(path, Decl(ambient.d.ts, 1, 33))
>encoding : Symbol(encoding, Decl(ambient.d.ts, 1, 46))
}
declare module "path" {
>"path" : Symbol("path", Decl(ambient.d.ts, 2, 1))

import fs = require("fs"); // ok
>fs : Symbol(fs, Decl(ambient.d.ts, 3, 23))

namespace path {
>path : Symbol(path, Decl(ambient.d.ts, 4, 30))

export const sep: string;
>sep : Symbol(sep, Decl(ambient.d.ts, 6, 20))
}
export = path; // ok
>path : Symbol(path, Decl(ambient.d.ts, 4, 30))
}

=== /mainJs.js ===
import {} from "./a";
import("./a");
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/bundlerSyntaxRestrictions.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ declare var require: (...args: any[]) => any;
>require : (...args: any[]) => any
>args : any[]

=== /ambient.d.ts ===
declare module "fs" {
>"fs" : typeof import("fs")

export function readFileSync(path: string, encoding?: string): string;
>readFileSync : (path: string, encoding?: string) => string
>path : string
>encoding : string
}
declare module "path" {
>"path" : typeof import("path")

import fs = require("fs"); // ok
>fs : typeof fs

namespace path {
>path : typeof path

export const sep: string;
>sep : string
}
export = path; // ok
>path : typeof path
}

=== /mainJs.js ===
import {} from "./a";
import("./a");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
// @Filename: /node_modules/@types/node/index.d.ts
declare var require: (...args: any[]) => any;

// @Filename: /ambient.d.ts
declare module "fs" {
export function readFileSync(path: string, encoding?: string): string;
}
declare module "path" {
import fs = require("fs"); // ok
namespace path {
export const sep: string;
}
export = path; // ok
}

// @Filename: /mainJs.js
import {} from "./a";
import("./a");
Expand Down