Skip to content

Commit 7987e41

Browse files
authored
Support export * as ns from "source"
1 parent cda280a commit 7987e41

File tree

6 files changed

+111
-1
lines changed

6 files changed

+111
-1
lines changed

acorn-loose/src/statement.js

+7
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ lp.parseExport = function() {
358358
let node = this.startNode()
359359
this.next()
360360
if (this.eat(tt.star)) {
361+
if (this.options.ecmaVersion >= 11) {
362+
if (this.eatContextual("as")) {
363+
node.exported = this.parseExprAtom()
364+
} else {
365+
node.exported = null
366+
}
367+
}
361368
node.source = this.eatContextual("from") ? this.parseExprAtom() : this.dummyString()
362369
return this.finishNode(node, "ExportAllDeclaration")
363370
}

acorn-walk/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ base.ExportNamedDeclaration = base.ExportDefaultDeclaration = (node, st, c) => {
353353
if (node.source) c(node.source, st, "Expression")
354354
}
355355
base.ExportAllDeclaration = (node, st, c) => {
356+
if (node.exported)
357+
c(node.exported, st)
356358
c(node.source, st, "Expression")
357359
}
358360
base.ImportDeclaration = (node, st, c) => {

acorn/src/statement.js

+8
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,14 @@ pp.parseExport = function(node, exports) {
676676
this.next()
677677
// export * from '...'
678678
if (this.eat(tt.star)) {
679+
if (this.options.ecmaVersion >= 11) {
680+
if (this.eatContextual("as")) {
681+
node.exported = this.parseIdent(true)
682+
this.checkExport(exports, node.exported.name, this.lastTokStart)
683+
} else {
684+
node.exported = null
685+
}
686+
}
679687
this.expectContextual("from")
680688
if (this.type !== tt.string) this.unexpected()
681689
node.source = this.parseExprAtom()

bin/run_test262.js

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const unsupportedFeatures = [
1111
"class-static-fields-public",
1212
"class-static-methods-private",
1313
"coalesce-expression",
14-
"export-star-as-namespace-from-module",
1514
"import.meta",
1615
"numeric-separator-literal",
1716
"optional-chaining",

test/run.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require("./tests-optional-catch-binding.js");
1717
require("./tests-bigint.js");
1818
require("./tests-dynamic-import.js");
19+
require("./tests-export-all-as-ns-from-source.js");
1920
var acorn = require("../acorn")
2021
var acorn_loose = require("../acorn-loose")
2122

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
if (typeof exports != "undefined") {
3+
var driver = require("./driver.js");
4+
var test = driver.test, testFail = driver.testFail;
5+
var acorn = require("../acorn");
6+
}
7+
8+
//------------------------------------------------------------------------------
9+
// export * as ns from "source"
10+
//------------------------------------------------------------------------------
11+
12+
test("export * as ns from \"source\"", {
13+
"type": "Program",
14+
"start": 0,
15+
"end": 28,
16+
"body": [
17+
{
18+
"type": "ExportAllDeclaration",
19+
"start": 0,
20+
"end": 28,
21+
"exported": {
22+
"type": "Identifier",
23+
"start": 12,
24+
"end": 14,
25+
"name": "ns"
26+
},
27+
"source": {
28+
"type": "Literal",
29+
"start": 20,
30+
"end": 28,
31+
"value": "source",
32+
"raw": "\"source\""
33+
}
34+
}
35+
],
36+
"sourceType": "module"
37+
}, { sourceType: "module", ecmaVersion: 11 })
38+
39+
test("export * as foo from \"bar\"", {
40+
"type": "Program",
41+
"start": 0,
42+
"end": 26,
43+
"body": [
44+
{
45+
"type": "ExportAllDeclaration",
46+
"start": 0,
47+
"end": 26,
48+
"exported": {
49+
"type": "Identifier",
50+
"start": 12,
51+
"end": 15,
52+
"name": "foo"
53+
},
54+
"source": {
55+
"type": "Literal",
56+
"start": 21,
57+
"end": 26,
58+
"value": "bar",
59+
"raw": "\"bar\""
60+
}
61+
}
62+
],
63+
"sourceType": "module"
64+
}, { sourceType: "module", ecmaVersion: 11 })
65+
66+
test("export * from \"source\"", {
67+
"type": "Program",
68+
"start": 0,
69+
"end": 22,
70+
"body": [
71+
{
72+
"type": "ExportAllDeclaration",
73+
"start": 0,
74+
"end": 22,
75+
"exported": null,
76+
"source": {
77+
"type": "Literal",
78+
"start": 14,
79+
"end": 22,
80+
"value": "source",
81+
"raw": "\"source\""
82+
}
83+
}
84+
],
85+
"sourceType": "module"
86+
}, { sourceType: "module", ecmaVersion: 11 })
87+
88+
testFail("export * as ns from \"source\"", "'import' and 'export' may appear only with 'sourceType: module' (1:0)", { sourceType: "script", ecmaVersion: 11 })
89+
testFail("export * as ns from \"source\"", "Unexpected token (1:9)", { sourceType: "module", ecmaVersion: 10 })
90+
testFail("export * as ns", "Unexpected token (1:14)", { sourceType: "module", ecmaVersion: 11 })
91+
testFail("export * as from \"source\"", "Unexpected token (1:17)", { sourceType: "module", ecmaVersion: 11 })
92+
testFail("export * as ns \"source\"", "Unexpected token (1:15)", { sourceType: "module", ecmaVersion: 11 })
93+
testFail("export {} as ns from \"source\"", "Unexpected token (1:10)", { sourceType: "module", ecmaVersion: 11 })

0 commit comments

Comments
 (0)