[estree] Align export ... representations.

This commit is contained in:
Ingvar Stepanyan 2015-03-10 10:37:00 +02:00
parent 722bc65e79
commit ff60ee4fdb
3 changed files with 154 additions and 163 deletions

View File

@ -2694,15 +2694,13 @@
pp.parseExport = function(node) { pp.parseExport = function(node) {
this.next(); this.next();
// export var|const|let|function|class ...; // export * from '...';
if (this.type === tt._var || this.type === tt._const || this.type === tt._let || this.type === tt._function || this.type === tt._class) { if (this.eat(tt.star)) {
node.declaration = this.parseStatement(true); this.expectContextual("from");
node['default'] = false; node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected();
node.specifiers = null; return this.finishNode(node, "ExportAllDeclaration");
node.source = null; }
} else if (this.eat(tt._default)) { // export default ...;
// export default ...;
if (this.eat(tt._default)) {
var expr = this.parseMaybeAssign(); var expr = this.parseMaybeAssign();
if (expr.id) { if (expr.id) {
switch (expr.type) { switch (expr.type) {
@ -2711,51 +2709,43 @@
} }
} }
node.declaration = expr; node.declaration = expr;
node['default'] = true;
node.specifiers = null;
node.source = null;
this.semicolon(); this.semicolon();
} else { return this.finishNode(node, "ExportDefaultDeclaration");
// export * from '...'; }
// export { x, y as z } [from '...']; // export var|const|let|function|class ...;
var isBatch = this.type === tt.star; if (this.type.keyword) {
node.declaration = this.parseStatement(true);
node.specifiers = [];
node.source = null;
} else { // export { x, y as z } [from '...'];
node.declaration = null; node.declaration = null;
node['default'] = false;
node.specifiers = this.parseExportSpecifiers(); node.specifiers = this.parseExportSpecifiers();
if (this.eatContextual("from")) { if (this.eatContextual("from")) {
node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected(); node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected();
} else { } else {
if (isBatch) this.unexpected();
node.source = null; node.source = null;
} }
this.semicolon(); this.semicolon();
} }
return this.finishNode(node, "ExportDeclaration"); return this.finishNode(node, "ExportNamedDeclaration");
}; };
// Parses a comma-separated list of module exports. // Parses a comma-separated list of module exports.
pp.parseExportSpecifiers = function() { pp.parseExportSpecifiers = function() {
var nodes = [], first = true; var nodes = [], first = true;
if (this.type === tt.star) { // export { x, y as z } [from '...']
// export * from '...' this.expect(tt.braceL);
var node = this.startNode(); while (!this.eat(tt.braceR)) {
this.next(); if (!first) {
nodes.push(this.finishNode(node, "ExportBatchSpecifier")); this.expect(tt.comma);
} else { if (this.options.allowTrailingCommas && this.eat(tt.braceR)) break;
// export { x, y as z } [from '...'] } else first = false;
this.expect(tt.braceL);
while (!this.eat(tt.braceR)) {
if (!first) {
this.expect(tt.comma);
if (this.options.allowTrailingCommas && this.eat(tt.braceR)) break;
} else first = false;
var node = this.startNode(); var node = this.startNode();
node.id = this.parseIdent(this.type === tt._default); node.local = this.parseIdent(this.type === tt._default);
node.name = this.eatContextual("as") ? this.parseIdent(true) : null; node.exported = this.eatContextual("as") ? this.parseIdent(true) : node.local;
nodes.push(this.finishNode(node, "ExportSpecifier")); nodes.push(this.finishNode(node, "ExportSpecifier"));
}
} }
return nodes; return nodes;
}; };

View File

@ -1042,9 +1042,11 @@
lp.parseExport = function() { lp.parseExport = function() {
var node = this.startNode(); var node = this.startNode();
this.next(); this.next();
node['default'] = this.eat(tt._default); if (this.eat(tt.star)) {
node.specifiers = node.source = null; node.source = this.eatContextual("from") ? this.parseExprAtom() : null;
if (node['default']) { return this.finishNode(node, "ExportAllDeclaration");
}
if (this.eat(tt._default)) {
var expr = this.parseMaybeAssign(); var expr = this.parseMaybeAssign();
if (expr.id) { if (expr.id) {
switch (expr.type) { switch (expr.type) {
@ -1054,14 +1056,19 @@
} }
node.declaration = expr; node.declaration = expr;
this.semicolon(); this.semicolon();
} else if (this.tok.type.keyword) { return this.finishNode(node, "ExportDefaultDeclaration");
}
if (this.tok.type.keyword) {
node.declaration = this.parseStatement(); node.declaration = this.parseStatement();
node.specifiers = [];
node.source = null;
} else { } else {
node.declaration = null; node.declaration = null;
this.parseExportSpecifierList(node); node.specifiers = this.parseExportSpecifierList();
node.source = this.eatContextual("from") ? this.parseExprAtom() : null;
this.semicolon();
} }
this.semicolon(); return this.finishNode(node, "ExportNamedDeclaration");
return this.finishNode(node, "ExportDeclaration");
}; };
lp.parseImport = function() { lp.parseImport = function() {
@ -1078,15 +1085,16 @@
this.finishNode(elt, "ImportDefaultSpecifier"); this.finishNode(elt, "ImportDefaultSpecifier");
this.eat(tt.comma); this.eat(tt.comma);
} }
this.parseImportSpecifierList(node); node.specifiers = this.parseImportSpecifierList();
node.source = this.eatContextual("from") ? this.parseExprAtom() : null;
if (elt) node.specifiers.unshift(elt); if (elt) node.specifiers.unshift(elt);
} }
this.semicolon(); this.semicolon();
return this.finishNode(node, "ImportDeclaration"); return this.finishNode(node, "ImportDeclaration");
}; };
lp.parseImportSpecifierList = function(node) { lp.parseImportSpecifierList = function() {
var elts = node.specifiers = []; var elts = [];
if (this.tok.type === tt.star) { if (this.tok.type === tt.star) {
var elt = this.startNode(); var elt = this.startNode();
this.next(); this.next();
@ -1114,39 +1122,27 @@
this.eat(tt.braceR); this.eat(tt.braceR);
this.popCx(); this.popCx();
} }
node.source = this.eatContextual("from") ? this.parseExprAtom() : null; return elts;
}; };
lp.parseExportSpecifierList = function(node) { lp.parseExportSpecifierList = function() {
var elts = node.specifiers = []; var elts = [];
if (this.tok.type === tt.star) { var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart;
this.pushCx();
this.eat(tt.braceL);
if (this.curLineStart > continuedLine) continuedLine = this.curLineStart;
while (!this.closes(tt.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
if (this.isContextual("from")) break;
var elt = this.startNode(); var elt = this.startNode();
this.next(); elt.local = this.parseIdent();
if (this.eatContextual("as")) elt.name = this.parseIdent(); elt.exported = this.eatContextual("as") ? this.parseIdent() : elt.local;
elts.push(this.finishNode(elt, "ExportBatchSpecifier")); this.finishNode(elt, "ExportSpecifier");
} else { elts.push(elt);
var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart; this.eat(tt.comma);
this.pushCx();
this.eat(tt.braceL);
if (this.curLineStart > continuedLine) continuedLine = this.curLineStart;
while (!this.closes(tt.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
var elt = this.startNode();
if (this.eat(tt.star)) {
if (this.eatContextual("as")) elt.name = this.parseIdent();
this.finishNode(elt, "ExportBatchSpecifier");
} else {
if (this.isContextual("from")) break;
elt.id = this.parseIdent();
elt.name = this.eatContextual("as") ? this.parseIdent() : null;
this.finishNode(elt, "ExportSpecifier");
}
elts.push(elt);
this.eat(tt.comma);
}
this.eat(tt.braceR);
this.popCx();
} }
node.source = this.eatContextual("from") ? this.parseExprAtom() : null; this.eat(tt.braceR);
this.popCx();
return elts;
}; };
lp.parseExprList = function(close, allowEmpty) { lp.parseExprList = function(close, allowEmpty) {

View File

@ -4453,7 +4453,7 @@ test("var {a:b} = {}", {
test("export var document", { test("export var document", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: { declaration: {
type: "VariableDeclaration", type: "VariableDeclaration",
declarations: [{ declarations: [{
@ -4478,8 +4478,7 @@ test("export var document", {
end: {line: 1, column: 19} end: {line: 1, column: 19}
} }
}, },
default: false, specifiers: [],
specifiers: null,
source: null, source: null,
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 0},
@ -4499,7 +4498,7 @@ test("export var document", {
test("export var document = { }", { test("export var document = { }", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: { declaration: {
type: "VariableDeclaration", type: "VariableDeclaration",
declarations: [{ declarations: [{
@ -4531,8 +4530,7 @@ test("export var document = { }", {
end: {line: 1, column: 25} end: {line: 1, column: 25}
} }
}, },
default: false, specifiers: [],
specifiers: null,
source: null, source: null,
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 0},
@ -4552,7 +4550,7 @@ test("export var document = { }", {
test("export let document", { test("export let document", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: { declaration: {
type: "VariableDeclaration", type: "VariableDeclaration",
declarations: [{ declarations: [{
@ -4577,8 +4575,7 @@ test("export let document", {
end: {line: 1, column: 19} end: {line: 1, column: 19}
} }
}, },
default: false, specifiers: [],
specifiers: null,
source: null, source: null,
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 0},
@ -4598,7 +4595,7 @@ test("export let document", {
test("export let document = { }", { test("export let document = { }", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: { declaration: {
type: "VariableDeclaration", type: "VariableDeclaration",
declarations: [{ declarations: [{
@ -4630,8 +4627,7 @@ test("export let document = { }", {
end: {line: 1, column: 25} end: {line: 1, column: 25}
} }
}, },
default: false, specifiers: [],
specifiers: null,
source: null, source: null,
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 0},
@ -4651,7 +4647,7 @@ test("export let document = { }", {
test("export const document = { }", { test("export const document = { }", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: { declaration: {
type: "VariableDeclaration", type: "VariableDeclaration",
declarations: [{ declarations: [{
@ -4683,8 +4679,7 @@ test("export const document = { }", {
end: {line: 1, column: 27} end: {line: 1, column: 27}
} }
}, },
default: false, specifiers: [],
specifiers: null,
source: null, source: null,
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 0},
@ -4704,7 +4699,7 @@ test("export const document = { }", {
test("export function parse() { }", { test("export function parse() { }", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: { declaration: {
type: "FunctionDeclaration", type: "FunctionDeclaration",
id: { id: {
@ -4731,8 +4726,7 @@ test("export function parse() { }", {
end: {line: 1, column: 27} end: {line: 1, column: 27}
} }
}, },
default: false, specifiers: [],
specifiers: null,
source: null, source: null,
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 0},
@ -4752,7 +4746,7 @@ test("export function parse() { }", {
test("export class Class {}", { test("export class Class {}", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: { declaration: {
type: "ClassDeclaration", type: "ClassDeclaration",
id: { id: {
@ -4777,8 +4771,7 @@ test("export class Class {}", {
end: {line: 1, column: 21} end: {line: 1, column: 21}
} }
}, },
default: false, specifiers: [],
specifiers: null,
source: null, source: null,
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 0},
@ -4798,7 +4791,7 @@ test("export class Class {}", {
test("export default 42", { test("export default 42", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportDefaultDeclaration",
declaration: { declaration: {
type: "Literal", type: "Literal",
value: 42, value: 42,
@ -4808,9 +4801,6 @@ test("export default 42", {
end: {line: 1, column: 17} end: {line: 1, column: 17}
} }
}, },
default: true,
specifiers: null,
source: null,
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 0},
end: {line: 1, column: 17} end: {line: 1, column: 17}
@ -4830,7 +4820,7 @@ test("export default function () {}", {
type: "Program", type: "Program",
range: [0, 29], range: [0, 29],
body: [{ body: [{
type: "ExportDeclaration", type: "ExportDefaultDeclaration",
range: [0, 29], range: [0, 29],
declaration: { declaration: {
type: "FunctionExpression", type: "FunctionExpression",
@ -4844,10 +4834,7 @@ test("export default function () {}", {
range: [27, 29], range: [27, 29],
body: [] body: []
} }
}, }
default: true,
specifiers: null,
source: null
}] }]
}, {ecmaVersion: 6, ranges: true}); }, {ecmaVersion: 6, ranges: true});
@ -4855,7 +4842,7 @@ test("export default function f() {}", {
type: "Program", type: "Program",
range: [0, 30], range: [0, 30],
body: [{ body: [{
type: "ExportDeclaration", type: "ExportDefaultDeclaration",
range: [0, 30], range: [0, 30],
declaration: { declaration: {
type: "FunctionDeclaration", type: "FunctionDeclaration",
@ -4873,10 +4860,7 @@ test("export default function f() {}", {
range: [28, 30], range: [28, 30],
body: [] body: []
} }
}, }
default: true,
specifiers: null,
source: null
}] }]
}, {ecmaVersion: 6, ranges: true}); }, {ecmaVersion: 6, ranges: true});
@ -4884,7 +4868,7 @@ test("export default class {}", {
type: "Program", type: "Program",
range: [0, 23], range: [0, 23],
body: [{ body: [{
type: "ExportDeclaration", type: "ExportDefaultDeclaration",
range: [0, 23], range: [0, 23],
declaration: { declaration: {
type: "ClassExpression", type: "ClassExpression",
@ -4896,10 +4880,7 @@ test("export default class {}", {
range: [21, 23], range: [21, 23],
body: [] body: []
} }
}, }
default: true,
specifiers: null,
source: null
}] }]
}, {ecmaVersion: 6, ranges: true}); }, {ecmaVersion: 6, ranges: true});
@ -4907,7 +4888,7 @@ test("export default class A {}", {
type: "Program", type: "Program",
range: [0, 25], range: [0, 25],
body: [{ body: [{
type: "ExportDeclaration", type: "ExportDefaultDeclaration",
range: [0, 25], range: [0, 25],
declaration: { declaration: {
type: "ClassDeclaration", type: "ClassDeclaration",
@ -4923,10 +4904,7 @@ test("export default class A {}", {
range: [23, 25], range: [23, 25],
body: [] body: []
} }
}, }
default: true,
specifiers: null,
source: null
}] }]
}, {ecmaVersion: 6, ranges: true}); }, {ecmaVersion: 6, ranges: true});
@ -4935,15 +4913,7 @@ testFail("export *", "Unexpected token (1:8)", {ecmaVersion: 6});
test("export * from \"crypto\"", { test("export * from \"crypto\"", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportAllDeclaration",
declaration: null,
specifiers: [{
type: "ExportBatchSpecifier",
loc: {
start: {line: 1, column: 7},
end: {line: 1, column: 8}
}
}],
source: { source: {
type: "Literal", type: "Literal",
value: "crypto", value: "crypto",
@ -4971,11 +4941,19 @@ test("export * from \"crypto\"", {
test("export { encrypt }", { test("export { encrypt }", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: null, declaration: null,
specifiers: [{ specifiers: [{
type: "ExportSpecifier", type: "ExportSpecifier",
id: { exported: {
type: "Identifier",
name: "encrypt",
loc: {
start: {line: 1, column: 9},
end: {line: 1, column: 16}
}
},
local: {
type: "Identifier", type: "Identifier",
name: "encrypt", name: "encrypt",
loc: { loc: {
@ -4983,7 +4961,6 @@ test("export { encrypt }", {
end: {line: 1, column: 16} end: {line: 1, column: 16}
} }
}, },
name: null,
loc: { loc: {
start: {line: 1, column: 9}, start: {line: 1, column: 9},
end: {line: 1, column: 16} end: {line: 1, column: 16}
@ -5008,12 +4985,20 @@ test("export { encrypt }", {
test("export { encrypt, decrypt }", { test("export { encrypt, decrypt }", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: null, declaration: null,
specifiers: [ specifiers: [
{ {
type: "ExportSpecifier", type: "ExportSpecifier",
id: { exported: {
type: "Identifier",
name: "encrypt",
loc: {
start: {line: 1, column: 9},
end: {line: 1, column: 16}
}
},
local: {
type: "Identifier", type: "Identifier",
name: "encrypt", name: "encrypt",
loc: { loc: {
@ -5021,7 +5006,6 @@ test("export { encrypt, decrypt }", {
end: {line: 1, column: 16} end: {line: 1, column: 16}
} }
}, },
name: null,
loc: { loc: {
start: {line: 1, column: 9}, start: {line: 1, column: 9},
end: {line: 1, column: 16} end: {line: 1, column: 16}
@ -5029,7 +5013,15 @@ test("export { encrypt, decrypt }", {
}, },
{ {
type: "ExportSpecifier", type: "ExportSpecifier",
id: { exported: {
type: "Identifier",
name: "decrypt",
loc: {
start: {line: 1, column: 18},
end: {line: 1, column: 25}
}
},
local: {
type: "Identifier", type: "Identifier",
name: "decrypt", name: "decrypt",
loc: { loc: {
@ -5037,7 +5029,6 @@ test("export { encrypt, decrypt }", {
end: {line: 1, column: 25} end: {line: 1, column: 25}
} }
}, },
name: null,
loc: { loc: {
start: {line: 1, column: 18}, start: {line: 1, column: 18},
end: {line: 1, column: 25} end: {line: 1, column: 25}
@ -5063,19 +5054,11 @@ test("export { encrypt, decrypt }", {
test("export { encrypt as default }", { test("export { encrypt as default }", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: null, declaration: null,
specifiers: [{ specifiers: [{
type: "ExportSpecifier", type: "ExportSpecifier",
id: { exported: {
type: "Identifier",
name: "encrypt",
loc: {
start: {line: 1, column: 9},
end: {line: 1, column: 16}
}
},
name: {
type: "Identifier", type: "Identifier",
name: "default", name: "default",
loc: { loc: {
@ -5083,6 +5066,14 @@ test("export { encrypt as default }", {
end: {line: 1, column: 27} end: {line: 1, column: 27}
} }
}, },
local: {
type: "Identifier",
name: "encrypt",
loc: {
start: {line: 1, column: 9},
end: {line: 1, column: 16}
}
},
loc: { loc: {
start: {line: 1, column: 9}, start: {line: 1, column: 9},
end: {line: 1, column: 27} end: {line: 1, column: 27}
@ -5107,12 +5098,20 @@ test("export { encrypt as default }", {
test("export { encrypt, decrypt as dec }", { test("export { encrypt, decrypt as dec }", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: null, declaration: null,
specifiers: [ specifiers: [
{ {
type: "ExportSpecifier", type: "ExportSpecifier",
id: { exported: {
type: "Identifier",
name: "encrypt",
loc: {
start: {line: 1, column: 9},
end: {line: 1, column: 16}
}
},
local: {
type: "Identifier", type: "Identifier",
name: "encrypt", name: "encrypt",
loc: { loc: {
@ -5120,7 +5119,6 @@ test("export { encrypt, decrypt as dec }", {
end: {line: 1, column: 16} end: {line: 1, column: 16}
} }
}, },
name: null,
loc: { loc: {
start: {line: 1, column: 9}, start: {line: 1, column: 9},
end: {line: 1, column: 16} end: {line: 1, column: 16}
@ -5128,15 +5126,7 @@ test("export { encrypt, decrypt as dec }", {
}, },
{ {
type: "ExportSpecifier", type: "ExportSpecifier",
id: { exported: {
type: "Identifier",
name: "decrypt",
loc: {
start: {line: 1, column: 18},
end: {line: 1, column: 25}
}
},
name: {
type: "Identifier", type: "Identifier",
name: "dec", name: "dec",
loc: { loc: {
@ -5144,6 +5134,14 @@ test("export { encrypt, decrypt as dec }", {
end: {line: 1, column: 32} end: {line: 1, column: 32}
} }
}, },
local: {
type: "Identifier",
name: "decrypt",
loc: {
start: {line: 1, column: 18},
end: {line: 1, column: 25}
}
},
loc: { loc: {
start: {line: 1, column: 18}, start: {line: 1, column: 18},
end: {line: 1, column: 32} end: {line: 1, column: 32}
@ -5169,12 +5167,20 @@ test("export { encrypt, decrypt as dec }", {
test("export { default } from \"other\"", { test("export { default } from \"other\"", {
type: "Program", type: "Program",
body: [{ body: [{
type: "ExportDeclaration", type: "ExportNamedDeclaration",
declaration: null, declaration: null,
specifiers: [ specifiers: [
{ {
type: "ExportSpecifier", type: "ExportSpecifier",
id: { exported: {
type: "Identifier",
name: "default",
loc: {
start: {line: 1, column: 9},
end: {line: 1, column: 16}
}
},
local: {
type: "Identifier", type: "Identifier",
name: "default", name: "default",
loc: { loc: {
@ -5182,7 +5188,6 @@ test("export { default } from \"other\"", {
end: {line: 1, column: 16} end: {line: 1, column: 16}
} }
}, },
name: null,
loc: { loc: {
start: {line: 1, column: 9}, start: {line: 1, column: 9},
end: {line: 1, column: 16} end: {line: 1, column: 16}