Skip to content

Commit

Permalink
🤖 Pick PR #60749 (Do not require import attribute on ...) into releas…
Browse files Browse the repository at this point in the history
…e-5.7 (#60750)

Co-authored-by: Andrew Branch <andrewbranch@users.noreply.github.com>
  • Loading branch information
typescript-bot and andrewbranch authored Dec 12, 2024
1 parent b82fd16 commit 21b02a1
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47660,7 +47660,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

if (moduleKind === ModuleKind.NodeNext && isOnlyImportableAsDefault(node.moduleSpecifier, resolvedModule) && !hasTypeJsonImportAttribute(node)) {
if (!importClause.isTypeOnly && moduleKind === ModuleKind.NodeNext && isOnlyImportableAsDefault(node.moduleSpecifier, resolvedModule) && !hasTypeJsonImportAttribute(node)) {
// Import attributes/assertions are not allowed in --module node16, so don't suggest adding one
error(node.moduleSpecifier, Diagnostics.Importing_a_JSON_file_into_an_ECMAScript_module_requires_a_type_Colon_json_import_attribute_when_module_is_set_to_0, ModuleKind[moduleKind]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
/loosey.cts(6,9): error TS2339: Property 'default' does not exist on type '{ version: number; }'.
/main.mts(5,36): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(6,52): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(8,10): error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'Node16'.
/main.mts(8,41): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(9,42): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(10,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
/main.mts(9,47): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(10,10): error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'Node16'.
/main.mts(10,41): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(11,42): error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
/main.mts(12,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.


==== /node_modules/not.json/package.json (0 errors) ====
Expand Down Expand Up @@ -42,7 +43,7 @@
"version": 1
}

==== /main.mts (6 errors) ====
==== /main.mts (7 errors) ====
import { oops } from "not.json"; // Ok
import moreOops from "actually-json"; // Error in nodenext
import typed from "actually-json/typed"; // Error in nodenext
Expand All @@ -54,6 +55,10 @@
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
import config2 from "./config.json"; // Error in nodenext, no attribute
import type config2Type from "./config.json"; // Ok, type-only
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2823: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.
import { version } from "./config.json" with { type: "json" }; // Error, named import
~~~~~~~
!!! error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'Node16'.
Expand Down
14 changes: 10 additions & 4 deletions tests/baselines/reference/nodeModulesJson(module=node16).symbols
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,24 @@ import { default as config1 } from "./config.json" with { type: "json" }; // Ok
import config2 from "./config.json"; // Error in nodenext, no attribute
>config2 : Symbol(config2, Decl(main.mts, 6, 6))

import type config2Type from "./config.json"; // Ok, type-only
>config2Type : Symbol(config2Type, Decl(main.mts, 7, 6))

import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
>config2Type2 : Symbol(config2Type2, Decl(main.mts, 8, 6))

import { version } from "./config.json" with { type: "json" }; // Error, named import
>version : Symbol(version, Decl(main.mts, 7, 8))
>version : Symbol(version, Decl(main.mts, 9, 8))

import * as config3 from "./config.json" with { type: "json" };
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))

config3.version; // Error
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))

config3.default; // Ok
>config3.default : Symbol("/config")
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
>default : Symbol("/config")

=== /loosey.cts ===
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/nodeModulesJson(module=node16).types
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ import config2 from "./config.json"; // Error in nodenext, no attribute
>config2 : { version: number; }
> : ^^^^^^^^^^^^^^^^^^^^

import type config2Type from "./config.json"; // Ok, type-only
>config2Type : any
> : ^^^

import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
>config2Type2 : any
> : ^^^
>type : any
> : ^^^

import { version } from "./config.json" with { type: "json" }; // Error, named import
>version : number
> : ^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
/main.mts(2,22): error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'NodeNext'.
/main.mts(3,19): error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'NodeNext'.
/main.mts(7,21): error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'NodeNext'.
/main.mts(8,10): error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'NodeNext'.
/main.mts(10,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.
/main.mts(9,47): error TS2857: Import attributes cannot be used with type-only imports or exports.
/main.mts(10,10): error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'NodeNext'.
/main.mts(12,9): error TS2339: Property 'version' does not exist on type '{ default: { version: number; }; }'.


==== /node_modules/not.json/package.json (0 errors) ====
Expand Down Expand Up @@ -41,7 +42,7 @@
"version": 1
}

==== /main.mts (5 errors) ====
==== /main.mts (6 errors) ====
import { oops } from "not.json"; // Ok
import moreOops from "actually-json"; // Error in nodenext
~~~~~~~~~~~~~~~
Expand All @@ -55,6 +56,10 @@
import config2 from "./config.json"; // Error in nodenext, no attribute
~~~~~~~~~~~~~~~
!!! error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'NodeNext'.
import type config2Type from "./config.json"; // Ok, type-only
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2857: Import attributes cannot be used with type-only imports or exports.
import { version } from "./config.json" with { type: "json" }; // Error, named import
~~~~~~~
!!! error TS1544: Named imports from a JSON file into an ECMAScript module are not allowed when 'module' is set to 'NodeNext'.
Expand Down
14 changes: 10 additions & 4 deletions tests/baselines/reference/nodeModulesJson(module=nodenext).symbols
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,24 @@ import { default as config1 } from "./config.json" with { type: "json" }; // Ok
import config2 from "./config.json"; // Error in nodenext, no attribute
>config2 : Symbol(config2, Decl(main.mts, 6, 6))

import type config2Type from "./config.json"; // Ok, type-only
>config2Type : Symbol(config2Type, Decl(main.mts, 7, 6))

import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
>config2Type2 : Symbol(config2Type2, Decl(main.mts, 8, 6))

import { version } from "./config.json" with { type: "json" }; // Error, named import
>version : Symbol(version, Decl(main.mts, 7, 8))
>version : Symbol(version, Decl(main.mts, 9, 8))

import * as config3 from "./config.json" with { type: "json" };
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))

config3.version; // Error
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))

config3.default; // Ok
>config3.default : Symbol("/config")
>config3 : Symbol(config3, Decl(main.mts, 8, 6))
>config3 : Symbol(config3, Decl(main.mts, 10, 6))
>default : Symbol("/config")

=== /loosey.cts ===
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/nodeModulesJson(module=nodenext).types
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ import config2 from "./config.json"; // Error in nodenext, no attribute
>config2 : { version: number; }
> : ^^^^^^^^^^^^^^^^^^^^

import type config2Type from "./config.json"; // Ok, type-only
>config2Type : any
> : ^^^

import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
>config2Type2 : any
> : ^^^
>type : any
> : ^^^

import { version } from "./config.json" with { type: "json" }; // Error, named import
>version : number
> : ^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/conformance/node/nodeModulesJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import typed from "actually-json/typed"; // Error in nodenext
import config from "./config.json" with { type: "json" }; // Ok
import { default as config1 } from "./config.json" with { type: "json" }; // Ok
import config2 from "./config.json"; // Error in nodenext, no attribute
import type config2Type from "./config.json"; // Ok, type-only
import type config2Type2 from "./config.json" with { type: "json" }; // Error, import attributes not allowed on type-only imports
import { version } from "./config.json" with { type: "json" }; // Error, named import
import * as config3 from "./config.json" with { type: "json" };
config3.version; // Error
Expand Down

0 comments on commit 21b02a1

Please sign in to comment.