From 3595489796f6d7fd92ebd73a61bec48e2518c980 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Wed, 5 Mar 2025 12:21:38 +0000 Subject: [PATCH] `ExportNamedDeclaration` always has `attributes` property --- acorn/src/statement.js | 4 + test/run.js | 1 + test/tests-export-named.js | 215 +++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 test/tests-export-named.js diff --git a/acorn/src/statement.js b/acorn/src/statement.js index 2b37d0e5a..d64581f8e 100644 --- a/acorn/src/statement.js +++ b/acorn/src/statement.js @@ -883,6 +883,8 @@ pp.parseExport = function(node, exports) { this.checkExport(exports, node.declaration.id, node.declaration.id.start) node.specifiers = [] node.source = null + if (this.options.ecmaVersion >= 16) + node.attributes = [] } else { // export { x, y as z } [from '...'] node.declaration = null node.specifiers = this.parseExportSpecifiers(exports) @@ -904,6 +906,8 @@ pp.parseExport = function(node, exports) { } node.source = null + if (this.options.ecmaVersion >= 16) + node.attributes = [] } this.semicolon() } diff --git a/test/run.js b/test/run.js index 77ec4551e..ccac94063 100644 --- a/test/run.js +++ b/test/run.js @@ -20,6 +20,7 @@ require("./tests-optional-catch-binding.js"); require("./tests-bigint.js"); require("./tests-dynamic-import.js"); + require("./tests-export-named.js"); require("./tests-export-all-as-ns-from-source.js"); require("./tests-import-meta.js"); require("./tests-nullish-coalescing.js"); diff --git a/test/tests-export-named.js b/test/tests-export-named.js new file mode 100644 index 000000000..e2571e019 --- /dev/null +++ b/test/tests-export-named.js @@ -0,0 +1,215 @@ +// Tests for `ExportNamedDeclaration` + +if (typeof exports !== "undefined") { + var driver = require("./driver.js"); + var test = driver.test, testFail = driver.testFail; +} + +//------------------------------------------------------------------------------ +// export {x} from "source" +//------------------------------------------------------------------------------ + +test("export {x} from \"source\"", { + "type": "Program", + "start": 0, + "end": 24, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 24, + "declaration": null, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 8, + "end": 9, + "local": { + "type": "Identifier", + "start": 8, + "end": 9, + "name": "x" + }, + "exported": { + "type": "Identifier", + "start": 8, + "end": 9, + "name": "x" + } + } + ], + "source": { + "type": "Literal", + "start": 16, + "end": 24, + "value": "source", + "raw": "\"source\"" + }, + "attributes": [] + } + ], + "sourceType": "module" +}, { sourceType: "module", ecmaVersion: 16 }) + +test("export {x as y} from \"source\"", { + "type": "Program", + "start": 0, + "end": 29, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 29, + "declaration": null, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 8, + "end": 14, + "local": { + "type": "Identifier", + "start": 8, + "end": 9, + "name": "x" + }, + "exported": { + "type": "Identifier", + "start": 13, + "end": 14, + "name": "y" + } + } + ], + "source": { + "type": "Literal", + "start": 21, + "end": 29, + "value": "source", + "raw": "\"source\"" + }, + "attributes": [] + } + ], + "sourceType": "module" +}, { sourceType: "module", ecmaVersion: 16 }) + +//------------------------------------------------------------------------------ +// export {x} +//------------------------------------------------------------------------------ + +test("let x; export {x};", { + "type": "Program", + "start": 0, + "end": 18, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 6, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 5, + "id": { + "type": "Identifier", + "start": 4, + "end": 5, + "name": "x" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "ExportNamedDeclaration", + "start": 7, + "end": 18, + "declaration": null, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 15, + "end": 16, + "local": { + "type": "Identifier", + "start": 15, + "end": 16, + "name": "x" + }, + "exported": { + "type": "Identifier", + "start": 15, + "end": 16, + "name": "x" + } + } + ], + "source": null, + "attributes": [] + } + ], + "sourceType": "module" +}, { sourceType: "module", ecmaVersion: 16 }) + +test("let x; export {x as y};", { + "type": "Program", + "start": 0, + "end": 23, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 6, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 5, + "id": { + "type": "Identifier", + "start": 4, + "end": 5, + "name": "x" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "ExportNamedDeclaration", + "start": 7, + "end": 23, + "declaration": null, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 15, + "end": 21, + "local": { + "type": "Identifier", + "start": 15, + "end": 16, + "name": "x" + }, + "exported": { + "type": "Identifier", + "start": 20, + "end": 21, + "name": "y" + } + } + ], + "source": null, + "attributes": [] + } + ], + "sourceType": "module" +}, {sourceType: "module", ecmaVersion: 16}) + +testFail("export {x} from \"source\"", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 }) +testFail("export {x as y} from \"source\"", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 }) +testFail("export {x}; let x;", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 }) +testFail("export {x as y}; let x;", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 })