Skip to content

Commit 9ac4045

Browse files
authored
Merge pull request webpack#7473 from webpack/feat/optional-catch-binding
Add support for ECMAScript 2019
2 parents 551384a + e4ac083 commit 9ac4045

File tree

8 files changed

+56
-6
lines changed

8 files changed

+56
-6
lines changed

lib/Parser.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const joinRanges = (startRange, endRange) => {
2323
const defaultParserOptions = {
2424
ranges: true,
2525
locations: true,
26-
ecmaVersion: 2018,
26+
ecmaVersion: 2019,
2727
sourceType: "module",
2828
onComment: null,
2929
plugins: {
@@ -34,6 +34,8 @@ const defaultParserOptions = {
3434
// regexp to match at lease one "magic comment"
3535
const webpackCommentRegExp = new RegExp(/(^|\W)webpack[A-Z]{1,}[A-Za-z]{1,}:/);
3636

37+
const EMPTY_ARRAY = [];
38+
3739
const EMPTY_COMMENT_OPTIONS = {
3840
options: null,
3941
errors: null
@@ -1356,7 +1358,11 @@ class Parser extends Tapable {
13561358
}
13571359

13581360
walkCatchClause(catchClause) {
1359-
this.inScope([catchClause.param], () => {
1361+
// Error binding is optional in catch clause since ECMAScript 2019
1362+
const errorBinding =
1363+
catchClause.param === null ? EMPTY_ARRAY : [catchClause.param];
1364+
1365+
this.inScope(errorBinding, () => {
13601366
this.prewalkStatement(catchClause.body);
13611367
this.walkStatement(catchClause.body);
13621368
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@webassemblyjs/wasm-edit": "1.5.11",
1111
"@webassemblyjs/wasm-opt": "1.5.11",
1212
"@webassemblyjs/wasm-parser": "1.5.11",
13-
"acorn": "^5.0.0",
13+
"acorn": "^5.6.2",
1414
"acorn-dynamic-import": "^3.0.0",
1515
"ajv": "^6.1.0",
1616
"ajv-keywords": "^3.1.0",

test/Parser.unittest.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,4 +618,19 @@ describe("Parser", () => {
618618
});
619619
});
620620
});
621+
622+
describe("optional catch binding support", () => {
623+
describe("should accept", () => {
624+
const cases = {
625+
"optional binding": "try {} catch {}"
626+
};
627+
Object.keys(cases).forEach(name => {
628+
const expr = cases[name];
629+
it(name, () => {
630+
const actual = Parser.parse(expr);
631+
expect(typeof actual).toBe("object");
632+
});
633+
});
634+
});
635+
});
621636
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import f from "./module";
2+
3+
it("should support optional catch binding", () => {
4+
expect(f()).toBe(true);
5+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function() {
2+
try {
3+
throw new Error();
4+
} catch {
5+
return true;
6+
}
7+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const supportsOptionalCatchBinding = require("../../../helpers/supportsOptionalCatchBinding");
2+
3+
module.exports = function(config) {
4+
// XXX: Disable this test if UglifyJS is used because it does not support ES 2019
5+
if (config.mode === "production") {
6+
return false;
7+
}
8+
return supportsOptionalCatchBinding();
9+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = function supportsOptionalCatchBinding() {
2+
try {
3+
eval("try {} catch {}");
4+
return true;
5+
} catch(e) {
6+
return false;
7+
}
8+
};

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ acorn@^4.0.4, acorn@~4.0.2:
234234
version "4.0.13"
235235
resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.13.tgz#105495ae5361d697bd195c825192e1ad7f253787"
236236

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

241241
ajv-keywords@^2.1.0:
242242
version "2.1.1"

0 commit comments

Comments
 (0)