Skip to content

Commit

Permalink
Merge pull request #7473 from webpack/feat/optional-catch-binding
Browse files Browse the repository at this point in the history
Add support for ECMAScript 2019
  • Loading branch information
sokra committed Jun 7, 2018
2 parents 551384a + e4ac083 commit 9ac4045
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 6 deletions.
10 changes: 8 additions & 2 deletions lib/Parser.js
Expand Up @@ -23,7 +23,7 @@ const joinRanges = (startRange, endRange) => {
const defaultParserOptions = {
ranges: true,
locations: true,
ecmaVersion: 2018,
ecmaVersion: 2019,
sourceType: "module",
onComment: null,
plugins: {
Expand All @@ -34,6 +34,8 @@ const defaultParserOptions = {
// regexp to match at lease one "magic comment"
const webpackCommentRegExp = new RegExp(/(^|\W)webpack[A-Z]{1,}[A-Za-z]{1,}:/);

const EMPTY_ARRAY = [];

const EMPTY_COMMENT_OPTIONS = {
options: null,
errors: null
Expand Down Expand Up @@ -1356,7 +1358,11 @@ class Parser extends Tapable {
}

walkCatchClause(catchClause) {
this.inScope([catchClause.param], () => {
// Error binding is optional in catch clause since ECMAScript 2019
const errorBinding =
catchClause.param === null ? EMPTY_ARRAY : [catchClause.param];

this.inScope(errorBinding, () => {
this.prewalkStatement(catchClause.body);
this.walkStatement(catchClause.body);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -10,7 +10,7 @@
"@webassemblyjs/wasm-edit": "1.5.11",
"@webassemblyjs/wasm-opt": "1.5.11",
"@webassemblyjs/wasm-parser": "1.5.11",
"acorn": "^5.0.0",
"acorn": "^5.6.2",
"acorn-dynamic-import": "^3.0.0",
"ajv": "^6.1.0",
"ajv-keywords": "^3.1.0",
Expand Down
15 changes: 15 additions & 0 deletions test/Parser.unittest.js
Expand Up @@ -618,4 +618,19 @@ describe("Parser", () => {
});
});
});

describe("optional catch binding support", () => {
describe("should accept", () => {
const cases = {
"optional binding": "try {} catch {}"
};
Object.keys(cases).forEach(name => {
const expr = cases[name];
it(name, () => {
const actual = Parser.parse(expr);
expect(typeof actual).toBe("object");
});
});
});
});
});
5 changes: 5 additions & 0 deletions test/cases/parsing/optional-catch-binding/index.js
@@ -0,0 +1,5 @@
import f from "./module";

it("should support optional catch binding", () => {
expect(f()).toBe(true);
});
7 changes: 7 additions & 0 deletions test/cases/parsing/optional-catch-binding/module.js
@@ -0,0 +1,7 @@
export default function() {
try {
throw new Error();
} catch {
return true;
}
};
9 changes: 9 additions & 0 deletions test/cases/parsing/optional-catch-binding/test.filter.js
@@ -0,0 +1,9 @@
const supportsOptionalCatchBinding = require("../../../helpers/supportsOptionalCatchBinding");

module.exports = function(config) {
// XXX: Disable this test if UglifyJS is used because it does not support ES 2019
if (config.mode === "production") {
return false;
}
return supportsOptionalCatchBinding();
};
8 changes: 8 additions & 0 deletions test/helpers/supportsOptionalCatchBinding.js
@@ -0,0 +1,8 @@
module.exports = function supportsOptionalCatchBinding() {
try {
eval("try {} catch {}");
return true;
} catch(e) {
return false;
}
};
6 changes: 3 additions & 3 deletions yarn.lock
Expand Up @@ -234,9 +234,9 @@ acorn@^4.0.4, acorn@~4.0.2:
version "4.0.13"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.13.tgz#105495ae5361d697bd195c825192e1ad7f253787"

acorn@^5.0.0, acorn@^5.3.0, acorn@^5.5.0:
version "5.5.3"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.3.tgz#f473dd47e0277a08e28e9bec5aeeb04751f0b8c9"
acorn@^5.0.0, acorn@^5.3.0, acorn@^5.5.0, acorn@^5.6.2:
version "5.6.2"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.6.2.tgz#b1da1d7be2ac1b4a327fb9eab851702c5045b4e7"

ajv-keywords@^2.1.0:
version "2.1.1"
Expand Down

0 comments on commit 9ac4045

Please sign in to comment.