From a1f0796fccfafee19b2fe06155efe746da2e9654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 15 Dec 2021 19:22:36 +0100 Subject: feat: Add support for import assertions and JSON modules (#12866) This commit adds proper support for import assertions and JSON modules. Implementation of "core/modules.rs" was changed to account for multiple possible module types, instead of always assuming that the code is an "ES module". In effect "ModuleMap" now has knowledge about each modules' type (stored via "ModuleType" enum). Module loading pipeline now stores information about expected module type for each request and validates that expected type matches discovered module type based on file's "MediaType". Relevant tests were added to "core/modules.rs" and integration tests, additionally multiple WPT tests were enabled. There are still some rough edges in the implementation and not all WPT were enabled, due to: a) unclear BOM handling in source code by "FileFetcher" b) design limitation of Deno's "FileFetcher" that doesn't download the same module multiple times in a single run Co-authored-by: Kitson Kelly --- cli/tests/testdata/import_assertions/data.json | 6 ++++++ cli/tests/testdata/import_assertions/dynamic_error.out | 5 +++++ cli/tests/testdata/import_assertions/dynamic_error.ts | 3 +++ cli/tests/testdata/import_assertions/dynamic_import.out | 2 ++ cli/tests/testdata/import_assertions/dynamic_import.ts | 3 +++ cli/tests/testdata/import_assertions/static_error.out | 5 +++++ cli/tests/testdata/import_assertions/static_error.ts | 3 +++ cli/tests/testdata/import_assertions/static_export.out | 2 ++ cli/tests/testdata/import_assertions/static_export.ts | 3 +++ cli/tests/testdata/import_assertions/static_import.out | 2 ++ cli/tests/testdata/import_assertions/static_import.ts | 3 +++ cli/tests/testdata/import_assertions/static_reexport.ts | 1 + cli/tests/testdata/import_assertions/type_check.out | 5 +++++ cli/tests/testdata/import_assertions/type_check.ts | 3 +++ 14 files changed, 46 insertions(+) create mode 100644 cli/tests/testdata/import_assertions/data.json create mode 100644 cli/tests/testdata/import_assertions/dynamic_error.out create mode 100644 cli/tests/testdata/import_assertions/dynamic_error.ts create mode 100644 cli/tests/testdata/import_assertions/dynamic_import.out create mode 100644 cli/tests/testdata/import_assertions/dynamic_import.ts create mode 100644 cli/tests/testdata/import_assertions/static_error.out create mode 100644 cli/tests/testdata/import_assertions/static_error.ts create mode 100644 cli/tests/testdata/import_assertions/static_export.out create mode 100644 cli/tests/testdata/import_assertions/static_export.ts create mode 100644 cli/tests/testdata/import_assertions/static_import.out create mode 100644 cli/tests/testdata/import_assertions/static_import.ts create mode 100644 cli/tests/testdata/import_assertions/static_reexport.ts create mode 100644 cli/tests/testdata/import_assertions/type_check.out create mode 100644 cli/tests/testdata/import_assertions/type_check.ts (limited to 'cli/tests/testdata/import_assertions') diff --git a/cli/tests/testdata/import_assertions/data.json b/cli/tests/testdata/import_assertions/data.json new file mode 100644 index 000000000..37b3ee1e0 --- /dev/null +++ b/cli/tests/testdata/import_assertions/data.json @@ -0,0 +1,6 @@ +{ + "a": "b", + "c": { + "d": 10 + } +} diff --git a/cli/tests/testdata/import_assertions/dynamic_error.out b/cli/tests/testdata/import_assertions/dynamic_error.out new file mode 100644 index 000000000..1ad40889b --- /dev/null +++ b/cli/tests/testdata/import_assertions/dynamic_error.out @@ -0,0 +1,5 @@ +[WILDCARD] +error: Uncaught (in promise) TypeError: Expected a "JavaScript" module but loaded a "JSON" module. +const data = await import("./data.json"); + ^ + at async [WILDCARD]dynamic_error.ts:1:14 diff --git a/cli/tests/testdata/import_assertions/dynamic_error.ts b/cli/tests/testdata/import_assertions/dynamic_error.ts new file mode 100644 index 000000000..2d9c6757f --- /dev/null +++ b/cli/tests/testdata/import_assertions/dynamic_error.ts @@ -0,0 +1,3 @@ +const data = await import("./data.json"); + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/dynamic_import.out b/cli/tests/testdata/import_assertions/dynamic_import.out new file mode 100644 index 000000000..3280e0f53 --- /dev/null +++ b/cli/tests/testdata/import_assertions/dynamic_import.out @@ -0,0 +1,2 @@ +[WILDCARD] +Module { default: { a: "b", c: { d: 10 } } } diff --git a/cli/tests/testdata/import_assertions/dynamic_import.ts b/cli/tests/testdata/import_assertions/dynamic_import.ts new file mode 100644 index 000000000..d6983852c --- /dev/null +++ b/cli/tests/testdata/import_assertions/dynamic_import.ts @@ -0,0 +1,3 @@ +const data = await import("./data.json", { assert: { type: "json" } }); + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/static_error.out b/cli/tests/testdata/import_assertions/static_error.out new file mode 100644 index 000000000..8524079de --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_error.out @@ -0,0 +1,5 @@ +[WILDCARD] +error: An unsupported media type was attempted to be imported as a module. + Specifier: [WILDCARD]data.json + MediaType: Json + at [WILDCARD]static_error.ts:1:18 diff --git a/cli/tests/testdata/import_assertions/static_error.ts b/cli/tests/testdata/import_assertions/static_error.ts new file mode 100644 index 000000000..0bc3a93f8 --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_error.ts @@ -0,0 +1,3 @@ +import data from "./data.json"; + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/static_export.out b/cli/tests/testdata/import_assertions/static_export.out new file mode 100644 index 000000000..42fbc066c --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_export.out @@ -0,0 +1,2 @@ +[WILDCARD] +{ a: "b", c: { d: 10 } } diff --git a/cli/tests/testdata/import_assertions/static_export.ts b/cli/tests/testdata/import_assertions/static_export.ts new file mode 100644 index 000000000..ac3ee694f --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_export.ts @@ -0,0 +1,3 @@ +import data from "./static_reexport.ts"; + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/static_import.out b/cli/tests/testdata/import_assertions/static_import.out new file mode 100644 index 000000000..42fbc066c --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_import.out @@ -0,0 +1,2 @@ +[WILDCARD] +{ a: "b", c: { d: 10 } } diff --git a/cli/tests/testdata/import_assertions/static_import.ts b/cli/tests/testdata/import_assertions/static_import.ts new file mode 100644 index 000000000..180ab75f2 --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_import.ts @@ -0,0 +1,3 @@ +import data from "./data.json" assert { type: "json" }; + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/static_reexport.ts b/cli/tests/testdata/import_assertions/static_reexport.ts new file mode 100644 index 000000000..81af428be --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_reexport.ts @@ -0,0 +1 @@ +export { default } from "./data.json" assert { type: "json" }; diff --git a/cli/tests/testdata/import_assertions/type_check.out b/cli/tests/testdata/import_assertions/type_check.out new file mode 100644 index 000000000..8e1387456 --- /dev/null +++ b/cli/tests/testdata/import_assertions/type_check.out @@ -0,0 +1,5 @@ +[WILDCARD] +error: TS2339 [ERROR]: Property 'foo' does not exist on type '{ a: string; c: { d: number; }; }'. +console.log(data.foo); + ~~~ + at [WILDCARD]type_check.ts:3:18 diff --git a/cli/tests/testdata/import_assertions/type_check.ts b/cli/tests/testdata/import_assertions/type_check.ts new file mode 100644 index 000000000..19adb3eae --- /dev/null +++ b/cli/tests/testdata/import_assertions/type_check.ts @@ -0,0 +1,3 @@ +import data from "./data.json" assert { type: "json" }; + +console.log(data.foo); -- cgit v1.2.3