Skip to content

Commit

Permalink
Merge pull request #6452 from webpack/update_acorn
Browse files Browse the repository at this point in the history
Update parser to support ES 2018
  • Loading branch information
sokra authored Feb 24, 2018
2 parents 9179980 + b0949cb commit c7eb895
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const joinRanges = (startRange, endRange) => {
const defaultParserOptions = {
ranges: true,
locations: true,
ecmaVersion: 2017,
ecmaVersion: 2018,
sourceType: "module",
onComment: null,
plugins: {
Expand Down Expand Up @@ -1297,6 +1297,10 @@ class Parser extends Tapable {
walkObjectExpression(expression) {
for(let propIndex = 0, len = expression.properties.length; propIndex < len; propIndex++) {
const prop = expression.properties[propIndex];
if(prop.type === "SpreadElement") {
this.walkExpression(prop.argument);
continue;
}
if(prop.computed)
this.walkExpression(prop.key);
if(prop.shorthand)
Expand Down Expand Up @@ -1856,7 +1860,7 @@ class Parser extends Tapable {
}

static parse(code, options) {
const type = options.sourceType;
const type = options ? options.sourceType : "module";
const parserOptions = Object.assign(Object.create(null), defaultParserOptions, options);

if(type === "auto") {
Expand Down
19 changes: 18 additions & 1 deletion test/Parser.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,8 @@ describe("Parser", () => {
const cases = {
"async function": "async function x() {}",
"async arrow function": "async () => {}",
"await expression": "async function x(y) { await y }"
"await expression": "async function x(y) { await y }",
"await iteration": "async function f() { for await (x of xs); }"
};
const parser = new Parser();
Object.keys(cases).forEach((name) => {
Expand Down Expand Up @@ -511,4 +512,20 @@ describe("Parser", () => {
});
});
});

describe("object rest/spread support", () => {
describe("should accept", () => {
const cases = {
"object spread": "({...obj})",
"object rest": "({...obj} = foo)"
};
Object.keys(cases).forEach((name) => {
const expr = cases[name];
it(name, () => {
const actual = Parser.parse(expr);
should.strictEqual(typeof actual, "object");
});
});
});
});
});
1 change: 1 addition & 0 deletions test/cases/parsing/spread/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "ok";
11 changes: 11 additions & 0 deletions test/cases/parsing/spread/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import X, { A, B } from "./module";
import * as M from "./module";

it("should support spread operator", function() {
var o1 = { ...X };
o1.should.be.eql({ A: "A", B: "B" });
var o2 = { ...({ X }) };
o2.should.be.eql({ X: { A: "A", B: "B" } });
var o3 = { ...M };
o3.should.be.eql({ default: { A: "A", B: "B" }, A: "A", B: "B" });
});
6 changes: 6 additions & 0 deletions test/cases/parsing/spread/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const A = "A";
const B = "B";

export default { A, B };

export { A, B };
5 changes: 5 additions & 0 deletions test/cases/parsing/spread/test.filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var supportsSpread = require("../../../helpers/supportsSpread");

module.exports = function(config) {
return supportsSpread();
};
9 changes: 9 additions & 0 deletions test/helpers/supportsSpread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function supportsSpread() {
try {
var x = { a: true }, y; // eslint-disable-line no-unused-vars
eval("y = { ...x }");
return y !== x && y.a;
} catch(e) {
return false;
}
};
6 changes: 5 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ acorn@^3.0.4:
version "3.3.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a"

acorn@^5.0.0, acorn@^5.2.1:
acorn@^5.0.0:
version "5.4.1"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.4.1.tgz#fdc58d9d17f4a4e98d102ded826a9b9759125102"

acorn@^5.2.1:
version "5.3.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.3.0.tgz#7446d39459c54fb49a80e6ee6478149b940ec822"

Expand Down

0 comments on commit c7eb895

Please sign in to comment.