From 2afc85e6f7de621de6e5131e953986218cfa81d0 Mon Sep 17 00:00:00 2001 From: developjik Date: Fri, 27 Nov 2026 05:18:41 +0900 Subject: [PATCH] throw when a code block imports a name the module does not export A static import of a JavaScript code block is rewritten to a dynamic import that is destructured, so a named or default import that the module does not export bound undefined instead of failing like a static import. The module namespace is now checked for each imported name before it is destructured, and a SyntaxError like the one the browser reports for a static import is thrown. Fixes #1659 --- src/javascript/transpile.ts | 68 ++++++- test/input/imports/static-mixed-import.js | 3 + test/input/imports/static-namespace-import.js | 4 + test/input/imports/static-shadowed-import.js | 5 + test/javascript/transpile-test.ts | 55 ++++++ test/output/build/data-loaders/index.html | 4 +- test/output/build/fetches/foo.html | 5 +- test/output/build/fetches/top.html | 7 +- test/output/build/imports/foo/foo.html | 9 +- test/output/build/jsr/index.html | 5 +- test/output/build/typescript/index.html | 4 +- test/output/imports/alias-import.js | 4 +- .../output/imports/local-fetch-from-import.js | 4 +- test/output/imports/static-all-import.js | 5 +- test/output/imports/static-hoisted-import.js | 4 +- test/output/imports/static-import.js | 4 +- test/output/imports/static-mixed-import.js | 10 ++ .../imports/static-mixed-import.js.json | 140 +++++++++++++++ .../output/imports/static-namespace-import.js | 8 + .../imports/static-namespace-import.js.json | 134 ++++++++++++++ test/output/imports/static-npm-import.js | 4 +- test/output/imports/static-shadowed-import.js | 9 + .../imports/static-shadowed-import.js.json | 169 ++++++++++++++++++ .../imports/transitive-static-import.js | 4 +- test/output/static-import-noent.js | 4 +- 25 files changed, 652 insertions(+), 20 deletions(-) create mode 100644 test/input/imports/static-mixed-import.js create mode 100644 test/input/imports/static-namespace-import.js create mode 100644 test/input/imports/static-shadowed-import.js create mode 100644 test/output/imports/static-mixed-import.js create mode 100644 test/output/imports/static-mixed-import.js.json create mode 100644 test/output/imports/static-namespace-import.js create mode 100644 test/output/imports/static-namespace-import.js.json create mode 100644 test/output/imports/static-shadowed-import.js create mode 100644 test/output/imports/static-shadowed-import.js.json diff --git a/src/javascript/transpile.ts b/src/javascript/transpile.ts index d68ebbb44..aa5e020f8 100644 --- a/src/javascript/transpile.ts +++ b/src/javascript/transpile.ts @@ -201,16 +201,68 @@ function rewriteImportDeclarations( const specifiers: string[] = []; const imports: string[] = []; + const sources: string[] = []; for (const node of declarations) { output.delete(node.start, node.end + +(output.input[node.end] === "\n")); specifiers.push(rewriteImportSpecifiers(node)); - imports.push(`import(${annotatePath(resolve(getStringLiteralValue(node.source as StringLiteral)))})`); + const source = getStringLiteralValue(node.source as StringLiteral); + sources.push(source); + imports.push(`import(${annotatePath(resolve(source))})`); } - if (declarations.length > 1) { - output.insertLeft(0, `const [${specifiers.join(", ")}] = await Promise.all([${imports.join(", ")}]);\n`); - } else if (declarations.length === 1) { - output.insertLeft(0, `const ${specifiers[0]} = await ${imports[0]};\n`); + if (declarations.length === 0) return; + // A named or default import that the module does not export must fail like a + // static import does, instead of silently binding undefined, so the module + // namespaces are inspected before they are destructured. + const names = declarations.map(findImportedNames); + const checked = names.some((names) => names.length > 0); + const modules = checked ? declarations.map((_, i) => unusedName(body, declarations.length > 1 ? `$module${i}` : "$module")) : specifiers; // prettier-ignore + let code = + declarations.length > 1 + ? `const [${modules.join(", ")}] = await Promise.all([${imports.join(", ")}]);\n` + : `const ${modules[0]} = await ${imports[0]};\n`; + if (checked) { + for (let i = 0; i < declarations.length; ++i) { + for (const name of names[i]) { + const message = `The requested module ${quote(sources[i])} does not provide an export named ${quote(name)}`; + code += `if (!(${JSON.stringify(name)} in ${modules[i]})) throw new SyntaxError(${JSON.stringify(message)});\n`; + } + } + code += + declarations.length > 1 + ? `const [${specifiers.join(", ")}] = [${modules.join(", ")}];\n` + : `const ${specifiers[0]} = ${modules[0]};\n`; } + output.insertLeft(0, code); +} + +/** Returns the names that the default and named specifiers of the specified import declaration import. */ +function findImportedNames(node: ImportDeclaration): string[] { + const names = new Set(); + for (const specifier of node.specifiers) { + if (isDefaultSpecifier(specifier)) { + names.add("default"); + } else if (!isNamespaceSpecifier(specifier)) { + names.add(getImportedValue(specifier)); + } + } + return Array.from(names); +} + +/** Returns the specified name, suffixed as needed so that it is not used as an identifier in the specified node. */ +function unusedName(node: Node, name: string): string { + const names = new Set(); + simple(node, { + Identifier(node) { + names.add(node.name); + } + }); + while (names.has(name)) name += "_"; + return name; +} + +/** Returns the specified string as a single-quoted string literal, as browsers quote module and export names in this message. */ +function quote(string: string): string { + return !/['"\\\n\r]/.test(string) ? `'${string}'` : JSON.stringify(string); } function rewriteImportSpecifiers(node: ImportDeclaration): string { @@ -235,6 +287,12 @@ function getImportedName(node: ImportSpecifier): string { return node.imported.type === "Identifier" ? node.imported.name : node.imported.raw!; } +function getImportedValue(node: ImportSpecifier): string { + return node.imported.type === "Identifier" + ? node.imported.name + : getStringLiteralValue(node.imported as StringLiteral); +} + function isDefaultSpecifier( node: ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ): node is ImportDefaultSpecifier { diff --git a/test/input/imports/static-mixed-import.js b/test/input/imports/static-mixed-import.js new file mode 100644 index 000000000..c7f35f155 --- /dev/null +++ b/test/input/imports/static-mixed-import.js @@ -0,0 +1,3 @@ +import d, {"a-b" as ab, foo} from "./bar.js"; + +d(ab(foo)); diff --git a/test/input/imports/static-namespace-import.js b/test/input/imports/static-namespace-import.js new file mode 100644 index 000000000..27f6321ff --- /dev/null +++ b/test/input/imports/static-namespace-import.js @@ -0,0 +1,4 @@ +import * as bar from "./bar.js"; +import {foo} from "./other/foo.js"; + +bar.foo(foo); diff --git a/test/input/imports/static-shadowed-import.js b/test/input/imports/static-shadowed-import.js new file mode 100644 index 000000000..2fb9f7986 --- /dev/null +++ b/test/input/imports/static-shadowed-import.js @@ -0,0 +1,5 @@ +import * as bar from "./bar.js"; +import {foo} from "./other/foo.js"; + +const $module0 = foo; +$module0 + $module1 + bar; diff --git a/test/javascript/transpile-test.ts b/test/javascript/transpile-test.ts index 4f3a9d3bd..77029d96f 100644 --- a/test/javascript/transpile-test.ts +++ b/test/javascript/transpile-test.ts @@ -2,6 +2,8 @@ import assert from "node:assert"; import {readdirSync, statSync} from "node:fs"; import {mkdir, readFile, unlink, writeFile} from "node:fs/promises"; import {basename, join, resolve} from "node:path/posix"; +import {pathToFileURL} from "node:url"; +import {temporaryDirectoryTask} from "tempy"; import {isEnoent} from "../../src/error.js"; import {parseJavaScript} from "../../src/javascript/parse.js"; import type {TranspileModuleOptions} from "../../src/javascript/transpile.js"; @@ -95,6 +97,59 @@ describe("transpileJavaScript(input, options)", () => { }); }); +describe("the static imports of a JavaScript code block", () => { + // Runs the specified code block against a module at ./bar.js, and returns the result of the block. + async function run( + input: string, + module: string + ): Promise<{status: "fulfilled"; value: any} | {status: "rejected"; error: any}> { + const node = parseJavaScript(input, {path: "block.js"}); + const code = transpileJavaScript(node, {id: "0", path: "block.js", resolveImport: mockResolveImport}); + return temporaryDirectoryTask(async (root) => { + await mkdir(join(root, "_import")); + await writeFile(join(root, "package.json"), '{"type":"module"}'); + await writeFile(join(root, "_import", "bar.js"), module); + await writeFile( + join(root, "block.js"), + `const outcomes = [];\nconst define = ({body}) => outcomes.push(body().then((value) => ({status: "fulfilled", value}), (error) => ({status: "rejected", error})));\n${code}\nexport {outcomes};\n` // prettier-ignore + ); + const {outcomes} = await import(pathToFileURL(join(root, "block.js")).href); + return outcomes[0]; + }); + } + + it("throws a SyntaxError for a named import that the module does not export", async () => { + const outcome = await run('import {doesNotExist} from "./bar.js";\ndoesNotExist();', "export const foo = 1;\n"); // prettier-ignore + assert.strictEqual(outcome.status, "rejected"); + assert.ok(outcome.error instanceof SyntaxError); + assert.strictEqual(outcome.error.message, "The requested module './bar.js' does not provide an export named 'doesNotExist'"); // prettier-ignore + }); + it("throws a SyntaxError for a default import that the module does not export", async () => { + const outcome = await run('import foo from "./bar.js";\nfoo();', "export const foo = 1;\n"); + assert.strictEqual(outcome.status, "rejected"); + assert.ok(outcome.error instanceof SyntaxError); + assert.strictEqual( + outcome.error.message, + "The requested module './bar.js' does not provide an export named 'default'" + ); + }); + it("does not throw for an export whose value is undefined", async () => { + const outcome = await run('import {foo} from "./bar.js";\nif (foo !== undefined) throw new Error("foo should be undefined");', "export let foo;\n"); // prettier-ignore + assert.strictEqual(outcome.status, "fulfilled"); + assert.deepStrictEqual(outcome.value, {foo: undefined}); + }); + it("does not throw for a namespace import", async () => { + const outcome = await run('import * as bar from "./bar.js";\nbar.foo();', "export const foo = () => 123;\n"); // prettier-ignore + assert.strictEqual(outcome.status, "fulfilled"); + assert.strictEqual(typeof outcome.value.bar.foo, "function"); + }); + it("checks each import of a block", async () => { + const outcome = await run('import {foo} from "./bar.js";\nimport {doesNotExist} from "./bar.js";\nfoo();', "export const foo = 1;\n"); // prettier-ignore + assert.strictEqual(outcome.status, "rejected"); + assert.strictEqual(outcome.error.message, "The requested module './bar.js' does not provide an export named 'doesNotExist'"); // prettier-ignore + }); +}); + async function testFile(target: string, path: string): Promise { const input = `import {FileAttachment} from "observablehq:stdlib";\nFileAttachment(${JSON.stringify(target)})`; const output = await transpileModule(input, {root: "src", path}); diff --git a/test/output/build/data-loaders/index.html b/test/output/build/data-loaders/index.html index 400ef6388..9ce4879ad 100644 --- a/test/output/build/data-loaders/index.html +++ b/test/output/build/data-loaders/index.html @@ -23,7 +23,9 @@ registerFile("./test.txt", {"name":"./test.txt","mimeType":"text/plain","path":"./_file/test.f2ca1bb6.txt","lastModified":/* ts */1706742000000,"size":5}); define({id: "05e74070", inputs: ["display"], outputs: ["test"], body: async (display) => { -const {test} = await import("./_import/import-test.3349a02d.js"); +const $module = await import("./_import/import-test.3349a02d.js"); +if (!("test" in $module)) throw new SyntaxError("The requested module './import-test.js' does not provide an export named 'test'"); +const {test} = $module; display(await test); return {test}; diff --git a/test/output/build/fetches/foo.html b/test/output/build/fetches/foo.html index e4e54385f..af239815d 100644 --- a/test/output/build/fetches/foo.html +++ b/test/output/build/fetches/foo.html @@ -23,7 +23,10 @@ registerFile("./foo/foo-data.json", {"name":"./foo/foo-data.json","mimeType":"application/json","path":"./_file/foo/foo-data.67358ed8.json","lastModified":/* ts */1706742000000,"size":10}); define({id: "47a695da", inputs: ["display"], outputs: ["fooJsonData","fooCsvData"], body: async (display) => { -const {fooJsonData, fooCsvData} = await import("./_import/foo/foo.0cc12e18.js"); +const $module = await import("./_import/foo/foo.0cc12e18.js"); +if (!("fooJsonData" in $module)) throw new SyntaxError("The requested module '/foo/foo.js' does not provide an export named 'fooJsonData'"); +if (!("fooCsvData" in $module)) throw new SyntaxError("The requested module '/foo/foo.js' does not provide an export named 'fooCsvData'"); +const {fooJsonData, fooCsvData} = $module; display(fooJsonData); display(fooCsvData); diff --git a/test/output/build/fetches/top.html b/test/output/build/fetches/top.html index c9db53efa..bcab1d7c7 100644 --- a/test/output/build/fetches/top.html +++ b/test/output/build/fetches/top.html @@ -26,7 +26,12 @@ registerFile("./top-data.json", {"name":"./top-data.json","mimeType":"application/json","path":"./_file/top-data.67358ed8.json","lastModified":/* ts */1706742000000,"size":10}); define({id: "cb908c08", inputs: ["display"], outputs: ["fooCsvData","fooJsonData","topCsvData","topJsonData"], body: async (display) => { -const {fooCsvData, fooJsonData, topCsvData, topJsonData} = await import("./_import/top.bacf54cc.js"); +const $module = await import("./_import/top.bacf54cc.js"); +if (!("fooCsvData" in $module)) throw new SyntaxError("The requested module '/top.js' does not provide an export named 'fooCsvData'"); +if (!("fooJsonData" in $module)) throw new SyntaxError("The requested module '/top.js' does not provide an export named 'fooJsonData'"); +if (!("topCsvData" in $module)) throw new SyntaxError("The requested module '/top.js' does not provide an export named 'topCsvData'"); +if (!("topJsonData" in $module)) throw new SyntaxError("The requested module '/top.js' does not provide an export named 'topJsonData'"); +const {fooCsvData, fooJsonData, topCsvData, topJsonData} = $module; display(fooJsonData); display(fooCsvData); diff --git a/test/output/build/imports/foo/foo.html b/test/output/build/imports/foo/foo.html index 2cae2575d..671453553 100644 --- a/test/output/build/imports/foo/foo.html +++ b/test/output/build/imports/foo/foo.html @@ -29,7 +29,10 @@ registerFile("./hello.txt", {"name":"./hello.txt","mimeType":"text/plain","path":"../_file/foo/hello.5891b5b5.txt","lastModified":/* ts */1706742000000,"size":6}); define({id: "261e010e", inputs: ["display","FileAttachment"], outputs: ["d3","bar","top"], body: async (display,FileAttachment) => { -const [d3, {bar}, {top}] = await Promise.all([import("../_npm/d3@7.8.5/cd372fb8.js"), import("../_import/bar/bar.4460ccc2.js"), import("../_import/top.160847a6.js")]); +const [$module0, $module1, $module2] = await Promise.all([import("../_npm/d3@7.8.5/cd372fb8.js"), import("../_import/bar/bar.4460ccc2.js"), import("../_import/top.160847a6.js")]); +if (!("bar" in $module1)) throw new SyntaxError("The requested module '../bar/bar.js' does not provide an export named 'bar'"); +if (!("top" in $module2)) throw new SyntaxError("The requested module '/top.js' does not provide an export named 'top'"); +const [d3, {bar}, {top}] = [$module0, $module1, $module2]; display(bar); display(top); @@ -38,7 +41,9 @@ }}); define({id: "ec24b17a", inputs: ["display"], outputs: ["foobar"], body: async (display) => { -const {foobar} = await import("./foo%20bar.js"); +const $module = await import("./foo%20bar.js"); +if (!("foobar" in $module)) throw new SyntaxError("The requested module './foo%20bar.js' does not provide an export named 'foobar'"); +const {foobar} = $module; display(foobar); return {foobar}; }}); diff --git a/test/output/build/jsr/index.html b/test/output/build/jsr/index.html index 3d6e5617b..8b8adf43f 100644 --- a/test/output/build/jsr/index.html +++ b/test/output/build/jsr/index.html @@ -25,7 +25,10 @@ import {define} from "./_observablehq/client.00000001.js"; define({id: "0e3025ad", inputs: ["display"], outputs: ["randomIntegerBetween","randomSeeded","prng"], body: async (display) => { -const {randomIntegerBetween, randomSeeded} = await import("./_jsr/@std/random@0.1.0/mod.e50195f4.js"); +const $module = await import("./_jsr/@std/random@0.1.0/mod.e50195f4.js"); +if (!("randomIntegerBetween" in $module)) throw new SyntaxError("The requested module 'jsr:@std/random' does not provide an export named 'randomIntegerBetween'"); +if (!("randomSeeded" in $module)) throw new SyntaxError("The requested module 'jsr:@std/random' does not provide an export named 'randomSeeded'"); +const {randomIntegerBetween, randomSeeded} = $module; const prng = randomSeeded(1n); diff --git a/test/output/build/typescript/index.html b/test/output/build/typescript/index.html index 6365f5ee7..01cb11942 100644 --- a/test/output/build/typescript/index.html +++ b/test/output/build/typescript/index.html @@ -35,7 +35,9 @@ }}); define({id: "5a5e524f", outputs: ["sum"], body: async () => { -const {sum} = await import("./_import/sum.fd55756b.js"); +const $module = await import("./_import/sum.fd55756b.js"); +if (!("sum" in $module)) throw new SyntaxError("The requested module './sum.js' does not provide an export named 'sum'"); +const {sum} = $module; return {sum}; }}); diff --git a/test/output/imports/alias-import.js b/test/output/imports/alias-import.js index 825af323d..20e2460aa 100644 --- a/test/output/imports/alias-import.js +++ b/test/output/imports/alias-import.js @@ -1,5 +1,7 @@ define({id: "0", inputs: ["display"], outputs: ["bar"], body: async (display) => { -const {foo: bar} = await import("./_import/bar.js"); +const $module = await import("./_import/bar.js"); +if (!("foo" in $module)) throw new SyntaxError("The requested module './bar.js' does not provide an export named 'foo'"); +const {foo: bar} = $module; display(bar); return {bar}; diff --git a/test/output/imports/local-fetch-from-import.js b/test/output/imports/local-fetch-from-import.js index 96b63d240..dba0ae2d0 100644 --- a/test/output/imports/local-fetch-from-import.js +++ b/test/output/imports/local-fetch-from-import.js @@ -1,5 +1,7 @@ define({id: "0", inputs: ["display"], outputs: ["data"], body: async (display) => { -const {data} = await import("./_import/baz.js"); +const $module = await import("./_import/baz.js"); +if (!("data" in $module)) throw new SyntaxError("The requested module './baz.js' does not provide an export named 'data'"); +const {data} = $module; display(data); return {data}; diff --git a/test/output/imports/static-all-import.js b/test/output/imports/static-all-import.js index 54b21ac24..0a7e3ec64 100644 --- a/test/output/imports/static-all-import.js +++ b/test/output/imports/static-all-import.js @@ -1,5 +1,8 @@ define({id: "0", outputs: ["foo","bar"], body: async () => { -const [{foo}, {bar}] = await Promise.all([import("./_import/foo.js"), import("./_import/bar.js")]); +const [$module0, $module1] = await Promise.all([import("./_import/foo.js"), import("./_import/bar.js")]); +if (!("foo" in $module0)) throw new SyntaxError("The requested module './foo.js' does not provide an export named 'foo'"); +if (!("bar" in $module1)) throw new SyntaxError("The requested module './bar.js' does not provide an export named 'bar'"); +const [{foo}, {bar}] = [$module0, $module1]; foo() + bar(); return {foo,bar}; diff --git a/test/output/imports/static-hoisted-import.js b/test/output/imports/static-hoisted-import.js index 8b1321418..4d2d2b083 100644 --- a/test/output/imports/static-hoisted-import.js +++ b/test/output/imports/static-hoisted-import.js @@ -1,5 +1,7 @@ define({id: "0", outputs: ["confetti"], body: async () => { -const {default: confetti} = await import("https://cdn.jsdelivr.net/npm/canvas-confetti"); +const $module = await import("https://cdn.jsdelivr.net/npm/canvas-confetti"); +if (!("default" in $module)) throw new SyntaxError("The requested module 'npm:canvas-confetti' does not provide an export named 'default'"); +const {default: confetti} = $module; confetti(); diff --git a/test/output/imports/static-import.js b/test/output/imports/static-import.js index aded43886..5dabb09a9 100644 --- a/test/output/imports/static-import.js +++ b/test/output/imports/static-import.js @@ -1,5 +1,7 @@ define({id: "0", inputs: ["display"], outputs: ["foo"], body: async (display) => { -const {foo} = await import("./_import/bar.js"); +const $module = await import("./_import/bar.js"); +if (!("foo" in $module)) throw new SyntaxError("The requested module './bar.js' does not provide an export named 'foo'"); +const {foo} = $module; display(foo); return {foo}; diff --git a/test/output/imports/static-mixed-import.js b/test/output/imports/static-mixed-import.js new file mode 100644 index 000000000..a3a7d8350 --- /dev/null +++ b/test/output/imports/static-mixed-import.js @@ -0,0 +1,10 @@ +define({id: "0", outputs: ["d","ab","foo"], body: async () => { +const $module = await import("./_import/bar.js"); +if (!("default" in $module)) throw new SyntaxError("The requested module './bar.js' does not provide an export named 'default'"); +if (!("a-b" in $module)) throw new SyntaxError("The requested module './bar.js' does not provide an export named 'a-b'"); +if (!("foo" in $module)) throw new SyntaxError("The requested module './bar.js' does not provide an export named 'foo'"); +const {default: d, "a-b": ab, foo} = $module; + +d(ab(foo)); +return {d,ab,foo}; +}}); diff --git a/test/output/imports/static-mixed-import.js.json b/test/output/imports/static-mixed-import.js.json new file mode 100644 index 000000000..bc0c8d7c7 --- /dev/null +++ b/test/output/imports/static-mixed-import.js.json @@ -0,0 +1,140 @@ +{ + "body": { + "type": "Program", + "start": 0, + "end": 59, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 45, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 7, + "end": 8, + "local": { + "type": "Identifier", + "start": 7, + "end": 8, + "name": "d" + } + }, + { + "type": "ImportSpecifier", + "start": 11, + "end": 22, + "imported": { + "type": "Literal", + "start": 11, + "end": 16, + "value": "a-b", + "raw": "\"a-b\"" + }, + "local": { + "type": "Identifier", + "start": 20, + "end": 22, + "name": "ab" + } + }, + { + "type": "ImportSpecifier", + "start": 24, + "end": 27, + "imported": { + "type": "Identifier", + "start": 24, + "end": 27, + "name": "foo" + }, + "local": { + "type": "Identifier", + "start": 24, + "end": 27, + "name": "foo" + } + } + ], + "source": { + "type": "Literal", + "start": 34, + "end": 44, + "value": "./bar.js", + "raw": "\"./bar.js\"" + } + }, + { + "type": "ExpressionStatement", + "start": 47, + "end": 58, + "expression": { + "type": "CallExpression", + "start": 47, + "end": 57, + "callee": { + "type": "Identifier", + "start": 47, + "end": 48, + "name": "d" + }, + "arguments": [ + { + "type": "CallExpression", + "start": 49, + "end": 56, + "callee": { + "type": "Identifier", + "start": 49, + "end": 51, + "name": "ab" + }, + "arguments": [ + { + "type": "Identifier", + "start": 52, + "end": 55, + "name": "foo" + } + ], + "optional": false + } + ], + "optional": false + } + } + ], + "sourceType": "module" + }, + "declarations": [ + { + "type": "Identifier", + "start": 7, + "end": 8, + "name": "d" + }, + { + "type": "Identifier", + "start": 20, + "end": 22, + "name": "ab" + }, + { + "type": "Identifier", + "start": 24, + "end": 27, + "name": "foo" + } + ], + "references": [], + "files": [], + "imports": [ + { + "name": "./bar.js", + "type": "local", + "method": "static" + } + ], + "expression": false, + "async": false +} \ No newline at end of file diff --git a/test/output/imports/static-namespace-import.js b/test/output/imports/static-namespace-import.js new file mode 100644 index 000000000..1b0d2bfb9 --- /dev/null +++ b/test/output/imports/static-namespace-import.js @@ -0,0 +1,8 @@ +define({id: "0", outputs: ["bar","foo"], body: async () => { +const [$module0, $module1] = await Promise.all([import("./_import/bar.js"), import("./_import/other/foo.js")]); +if (!("foo" in $module1)) throw new SyntaxError("The requested module './other/foo.js' does not provide an export named 'foo'"); +const [bar, {foo}] = [$module0, $module1]; + +bar.foo(foo); +return {bar,foo}; +}}); diff --git a/test/output/imports/static-namespace-import.js.json b/test/output/imports/static-namespace-import.js.json new file mode 100644 index 000000000..b5367ebec --- /dev/null +++ b/test/output/imports/static-namespace-import.js.json @@ -0,0 +1,134 @@ +{ + "body": { + "type": "Program", + "start": 0, + "end": 84, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 32, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start": 7, + "end": 15, + "local": { + "type": "Identifier", + "start": 12, + "end": 15, + "name": "bar" + } + } + ], + "source": { + "type": "Literal", + "start": 21, + "end": 31, + "value": "./bar.js", + "raw": "\"./bar.js\"" + } + }, + { + "type": "ImportDeclaration", + "start": 33, + "end": 68, + "specifiers": [ + { + "type": "ImportSpecifier", + "start": 41, + "end": 44, + "imported": { + "type": "Identifier", + "start": 41, + "end": 44, + "name": "foo" + }, + "local": { + "type": "Identifier", + "start": 41, + "end": 44, + "name": "foo" + } + } + ], + "source": { + "type": "Literal", + "start": 51, + "end": 67, + "value": "./other/foo.js", + "raw": "\"./other/foo.js\"" + } + }, + { + "type": "ExpressionStatement", + "start": 70, + "end": 83, + "expression": { + "type": "CallExpression", + "start": 70, + "end": 82, + "callee": { + "type": "MemberExpression", + "start": 70, + "end": 77, + "object": { + "type": "Identifier", + "start": 70, + "end": 73, + "name": "bar" + }, + "property": { + "type": "Identifier", + "start": 74, + "end": 77, + "name": "foo" + }, + "computed": false, + "optional": false + }, + "arguments": [ + { + "type": "Identifier", + "start": 78, + "end": 81, + "name": "foo" + } + ], + "optional": false + } + } + ], + "sourceType": "module" + }, + "declarations": [ + { + "type": "Identifier", + "start": 12, + "end": 15, + "name": "bar" + }, + { + "type": "Identifier", + "start": 41, + "end": 44, + "name": "foo" + } + ], + "references": [], + "files": [], + "imports": [ + { + "name": "./bar.js", + "type": "local", + "method": "static" + }, + { + "name": "./other/foo.js", + "type": "local", + "method": "static" + } + ], + "expression": false, + "async": false +} \ No newline at end of file diff --git a/test/output/imports/static-npm-import.js b/test/output/imports/static-npm-import.js index 23e383cd9..c402bd933 100644 --- a/test/output/imports/static-npm-import.js +++ b/test/output/imports/static-npm-import.js @@ -1,5 +1,7 @@ define({id: "0", outputs: ["confetti"], body: async () => { -const {default: confetti} = await import("https://cdn.jsdelivr.net/npm/canvas-confetti"); +const $module = await import("https://cdn.jsdelivr.net/npm/canvas-confetti"); +if (!("default" in $module)) throw new SyntaxError("The requested module 'npm:canvas-confetti' does not provide an export named 'default'"); +const {default: confetti} = $module; confetti(); return {confetti}; diff --git a/test/output/imports/static-shadowed-import.js b/test/output/imports/static-shadowed-import.js new file mode 100644 index 000000000..4a7c9e12b --- /dev/null +++ b/test/output/imports/static-shadowed-import.js @@ -0,0 +1,9 @@ +define({id: "0", inputs: ["$module1"], outputs: ["bar","foo","$module0"], body: async ($module1) => { +const [$module0_, $module1_] = await Promise.all([import("./_import/bar.js"), import("./_import/other/foo.js")]); +if (!("foo" in $module1_)) throw new SyntaxError("The requested module './other/foo.js' does not provide an export named 'foo'"); +const [bar, {foo}] = [$module0_, $module1_]; + +const $module0 = foo; +$module0 + $module1 + bar; +return {bar,foo,$module0}; +}}); diff --git a/test/output/imports/static-shadowed-import.js.json b/test/output/imports/static-shadowed-import.js.json new file mode 100644 index 000000000..2ce7cfe26 --- /dev/null +++ b/test/output/imports/static-shadowed-import.js.json @@ -0,0 +1,169 @@ +{ + "body": { + "type": "Program", + "start": 0, + "end": 119, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 32, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start": 7, + "end": 15, + "local": { + "type": "Identifier", + "start": 12, + "end": 15, + "name": "bar" + } + } + ], + "source": { + "type": "Literal", + "start": 21, + "end": 31, + "value": "./bar.js", + "raw": "\"./bar.js\"" + } + }, + { + "type": "ImportDeclaration", + "start": 33, + "end": 68, + "specifiers": [ + { + "type": "ImportSpecifier", + "start": 41, + "end": 44, + "imported": { + "type": "Identifier", + "start": 41, + "end": 44, + "name": "foo" + }, + "local": { + "type": "Identifier", + "start": 41, + "end": 44, + "name": "foo" + } + } + ], + "source": { + "type": "Literal", + "start": 51, + "end": 67, + "value": "./other/foo.js", + "raw": "\"./other/foo.js\"" + } + }, + { + "type": "VariableDeclaration", + "start": 70, + "end": 91, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 76, + "end": 90, + "id": { + "type": "Identifier", + "start": 76, + "end": 84, + "name": "$module0" + }, + "init": { + "type": "Identifier", + "start": 87, + "end": 90, + "name": "foo" + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "start": 92, + "end": 118, + "expression": { + "type": "BinaryExpression", + "start": 92, + "end": 117, + "left": { + "type": "BinaryExpression", + "start": 92, + "end": 111, + "left": { + "type": "Identifier", + "start": 92, + "end": 100, + "name": "$module0" + }, + "operator": "+", + "right": { + "type": "Identifier", + "start": 103, + "end": 111, + "name": "$module1" + } + }, + "operator": "+", + "right": { + "type": "Identifier", + "start": 114, + "end": 117, + "name": "bar" + } + } + } + ], + "sourceType": "module" + }, + "declarations": [ + { + "type": "Identifier", + "start": 12, + "end": 15, + "name": "bar" + }, + { + "type": "Identifier", + "start": 41, + "end": 44, + "name": "foo" + }, + { + "type": "Identifier", + "start": 76, + "end": 84, + "name": "$module0" + } + ], + "references": [ + { + "type": "Identifier", + "start": 103, + "end": 111, + "name": "$module1" + } + ], + "files": [], + "imports": [ + { + "name": "./bar.js", + "type": "local", + "method": "static" + }, + { + "name": "./other/foo.js", + "type": "local", + "method": "static" + } + ], + "expression": false, + "async": false +} \ No newline at end of file diff --git a/test/output/imports/transitive-static-import.js b/test/output/imports/transitive-static-import.js index 43f8c7cf3..94c040cf9 100644 --- a/test/output/imports/transitive-static-import.js +++ b/test/output/imports/transitive-static-import.js @@ -1,5 +1,7 @@ define({id: "0", outputs: ["foo"], body: async () => { -const {foo} = await import("./_import/other/foo.js"); +const $module = await import("./_import/other/foo.js"); +if (!("foo" in $module)) throw new SyntaxError("The requested module './other/foo.js' does not provide an export named 'foo'"); +const {foo} = $module; return {foo}; }}); diff --git a/test/output/static-import-noent.js b/test/output/static-import-noent.js index 88878f0e3..5cffd3343 100644 --- a/test/output/static-import-noent.js +++ b/test/output/static-import-noent.js @@ -1,5 +1,7 @@ define({id: "0", inputs: ["display"], outputs: ["foo"], body: async (display) => { -const {foo} = await import("./_import/noent.js"); +const $module = await import("./_import/noent.js"); +if (!("foo" in $module)) throw new SyntaxError("The requested module './noent.js' does not provide an export named 'foo'"); +const {foo} = $module; display(foo); return {foo};