Skip to content

Commit 0daa290

Browse files
Make sure ExportNamedDeclaration nodes always have an attributes property
FIX: Properly add an empty `attributes` property to every form of `ExportNamedDeclaration`.
1 parent fef49dc commit 0daa290

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

acorn/src/statement.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ pp.parseExport = function(node, exports) {
883883
this.checkExport(exports, node.declaration.id, node.declaration.id.start)
884884
node.specifiers = []
885885
node.source = null
886+
if (this.options.ecmaVersion >= 16)
887+
node.attributes = []
886888
} else { // export { x, y as z } [from '...']
887889
node.declaration = null
888890
node.specifiers = this.parseExportSpecifiers(exports)
@@ -904,6 +906,8 @@ pp.parseExport = function(node, exports) {
904906
}
905907

906908
node.source = null
909+
if (this.options.ecmaVersion >= 16)
910+
node.attributes = []
907911
}
908912
this.semicolon()
909913
}

test/run.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
require("./tests-optional-catch-binding.js");
2121
require("./tests-bigint.js");
2222
require("./tests-dynamic-import.js");
23+
require("./tests-export-named.js");
2324
require("./tests-export-all-as-ns-from-source.js");
2425
require("./tests-import-meta.js");
2526
require("./tests-nullish-coalescing.js");

test/tests-export-named.js

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
// Tests for `ExportNamedDeclaration`
2+
3+
if (typeof exports !== "undefined") {
4+
var driver = require("./driver.js");
5+
var test = driver.test, testFail = driver.testFail;
6+
}
7+
8+
//------------------------------------------------------------------------------
9+
// export {x} from "source"
10+
//------------------------------------------------------------------------------
11+
12+
test("export {x} from \"source\"", {
13+
"type": "Program",
14+
"start": 0,
15+
"end": 24,
16+
"body": [
17+
{
18+
"type": "ExportNamedDeclaration",
19+
"start": 0,
20+
"end": 24,
21+
"declaration": null,
22+
"specifiers": [
23+
{
24+
"type": "ExportSpecifier",
25+
"start": 8,
26+
"end": 9,
27+
"local": {
28+
"type": "Identifier",
29+
"start": 8,
30+
"end": 9,
31+
"name": "x"
32+
},
33+
"exported": {
34+
"type": "Identifier",
35+
"start": 8,
36+
"end": 9,
37+
"name": "x"
38+
}
39+
}
40+
],
41+
"source": {
42+
"type": "Literal",
43+
"start": 16,
44+
"end": 24,
45+
"value": "source",
46+
"raw": "\"source\""
47+
},
48+
"attributes": []
49+
}
50+
],
51+
"sourceType": "module"
52+
}, { sourceType: "module", ecmaVersion: 16 })
53+
54+
test("export {x as y} from \"source\"", {
55+
"type": "Program",
56+
"start": 0,
57+
"end": 29,
58+
"body": [
59+
{
60+
"type": "ExportNamedDeclaration",
61+
"start": 0,
62+
"end": 29,
63+
"declaration": null,
64+
"specifiers": [
65+
{
66+
"type": "ExportSpecifier",
67+
"start": 8,
68+
"end": 14,
69+
"local": {
70+
"type": "Identifier",
71+
"start": 8,
72+
"end": 9,
73+
"name": "x"
74+
},
75+
"exported": {
76+
"type": "Identifier",
77+
"start": 13,
78+
"end": 14,
79+
"name": "y"
80+
}
81+
}
82+
],
83+
"source": {
84+
"type": "Literal",
85+
"start": 21,
86+
"end": 29,
87+
"value": "source",
88+
"raw": "\"source\""
89+
},
90+
"attributes": []
91+
}
92+
],
93+
"sourceType": "module"
94+
}, { sourceType: "module", ecmaVersion: 16 })
95+
96+
//------------------------------------------------------------------------------
97+
// export {x}
98+
//------------------------------------------------------------------------------
99+
100+
test("let x; export {x};", {
101+
"type": "Program",
102+
"start": 0,
103+
"end": 18,
104+
"body": [
105+
{
106+
"type": "VariableDeclaration",
107+
"start": 0,
108+
"end": 6,
109+
"declarations": [
110+
{
111+
"type": "VariableDeclarator",
112+
"start": 4,
113+
"end": 5,
114+
"id": {
115+
"type": "Identifier",
116+
"start": 4,
117+
"end": 5,
118+
"name": "x"
119+
},
120+
"init": null
121+
}
122+
],
123+
"kind": "let"
124+
},
125+
{
126+
"type": "ExportNamedDeclaration",
127+
"start": 7,
128+
"end": 18,
129+
"declaration": null,
130+
"specifiers": [
131+
{
132+
"type": "ExportSpecifier",
133+
"start": 15,
134+
"end": 16,
135+
"local": {
136+
"type": "Identifier",
137+
"start": 15,
138+
"end": 16,
139+
"name": "x"
140+
},
141+
"exported": {
142+
"type": "Identifier",
143+
"start": 15,
144+
"end": 16,
145+
"name": "x"
146+
}
147+
}
148+
],
149+
"source": null,
150+
"attributes": []
151+
}
152+
],
153+
"sourceType": "module"
154+
}, { sourceType: "module", ecmaVersion: 16 })
155+
156+
test("let x; export {x as y};", {
157+
"type": "Program",
158+
"start": 0,
159+
"end": 23,
160+
"body": [
161+
{
162+
"type": "VariableDeclaration",
163+
"start": 0,
164+
"end": 6,
165+
"declarations": [
166+
{
167+
"type": "VariableDeclarator",
168+
"start": 4,
169+
"end": 5,
170+
"id": {
171+
"type": "Identifier",
172+
"start": 4,
173+
"end": 5,
174+
"name": "x"
175+
},
176+
"init": null
177+
}
178+
],
179+
"kind": "let"
180+
},
181+
{
182+
"type": "ExportNamedDeclaration",
183+
"start": 7,
184+
"end": 23,
185+
"declaration": null,
186+
"specifiers": [
187+
{
188+
"type": "ExportSpecifier",
189+
"start": 15,
190+
"end": 21,
191+
"local": {
192+
"type": "Identifier",
193+
"start": 15,
194+
"end": 16,
195+
"name": "x"
196+
},
197+
"exported": {
198+
"type": "Identifier",
199+
"start": 20,
200+
"end": 21,
201+
"name": "y"
202+
}
203+
}
204+
],
205+
"source": null,
206+
"attributes": []
207+
}
208+
],
209+
"sourceType": "module"
210+
}, {sourceType: "module", ecmaVersion: 16})
211+
212+
testFail("export {x} from \"source\"", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 })
213+
testFail("export {x as y} from \"source\"", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 })
214+
testFail("export {x}; let x;", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 })
215+
testFail("export {x as y}; let x;", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 })

0 commit comments

Comments
 (0)