Skip to content

Commit 3b68586

Browse files
ljqxariya
authored andcommitted
Support for ES2019 optional catch binding
1 parent 0911ad8 commit 3b68586

File tree

5 files changed

+226
-28
lines changed

5 files changed

+226
-28
lines changed

src/nodes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ export class CallExpression {
201201

202202
export class CatchClause {
203203
readonly type: string;
204-
readonly param: BindingIdentifier | BindingPattern;
204+
readonly param: BindingIdentifier | BindingPattern | null;
205205
readonly body: BlockStatement;
206-
constructor(param: BindingIdentifier | BindingPattern, body: BlockStatement) {
206+
constructor(param: BindingIdentifier | BindingPattern | null, body: BlockStatement) {
207207
this.type = Syntax.CatchClause;
208208
this.param = param;
209209
this.body = body;

src/parser.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,29 +2715,32 @@ export class Parser {
27152715

27162716
this.expectKeyword('catch');
27172717

2718-
this.expect('(');
2719-
if (this.match(')')) {
2720-
this.throwUnexpectedToken(this.lookahead);
2721-
}
2718+
let param: Node.BindingIdentifier | Node.BindingPattern | null = null;
2719+
if (this.match('(')) {
2720+
this.expect('(');
2721+
if (this.match(')')) {
2722+
this.throwUnexpectedToken(this.lookahead);
2723+
}
27222724

2723-
const params: any[] = [];
2724-
const param = this.parsePattern(params);
2725-
const paramMap = {};
2726-
for (let i = 0; i < params.length; i++) {
2727-
const key = '$' + params[i].value;
2728-
if (Object.prototype.hasOwnProperty.call(paramMap, key)) {
2729-
this.tolerateError(Messages.DuplicateBinding, params[i].value);
2725+
const params: any[] = [];
2726+
param = this.parsePattern(params);
2727+
const paramMap = {};
2728+
for (let i = 0; i < params.length; i++) {
2729+
const key = '$' + params[i].value;
2730+
if (Object.prototype.hasOwnProperty.call(paramMap, key)) {
2731+
this.tolerateError(Messages.DuplicateBinding, params[i].value);
2732+
}
2733+
paramMap[key] = true;
27302734
}
2731-
paramMap[key] = true;
2732-
}
27332735

2734-
if (this.context.strict && param.type === Syntax.Identifier) {
2735-
if (this.scanner.isRestrictedWord((param as Node.Identifier).name)) {
2736-
this.tolerateError(Messages.StrictCatchVariable);
2736+
if (this.context.strict && param.type === Syntax.Identifier) {
2737+
if (this.scanner.isRestrictedWord((param as Node.Identifier).name)) {
2738+
this.tolerateError(Messages.StrictCatchVariable);
2739+
}
27372740
}
2738-
}
27392741

2740-
this.expect(')');
2742+
this.expect(')');
2743+
}
27412744
const body = this.parseBlock();
27422745

27432746
return this.finalize(node, new Node.CatchClause(param, body));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
try {} catch {}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "TryStatement",
6+
"block": {
7+
"type": "BlockStatement",
8+
"body": [],
9+
"range": [
10+
4,
11+
6
12+
],
13+
"loc": {
14+
"start": {
15+
"line": 1,
16+
"column": 4
17+
},
18+
"end": {
19+
"line": 1,
20+
"column": 6
21+
}
22+
}
23+
},
24+
"handler": {
25+
"type": "CatchClause",
26+
"param": null,
27+
"body": {
28+
"type": "BlockStatement",
29+
"body": [],
30+
"range": [
31+
13,
32+
15
33+
],
34+
"loc": {
35+
"start": {
36+
"line": 1,
37+
"column": 13
38+
},
39+
"end": {
40+
"line": 1,
41+
"column": 15
42+
}
43+
}
44+
},
45+
"range": [
46+
7,
47+
15
48+
],
49+
"loc": {
50+
"start": {
51+
"line": 1,
52+
"column": 7
53+
},
54+
"end": {
55+
"line": 1,
56+
"column": 15
57+
}
58+
}
59+
},
60+
"finalizer": null,
61+
"range": [
62+
0,
63+
15
64+
],
65+
"loc": {
66+
"start": {
67+
"line": 1,
68+
"column": 0
69+
},
70+
"end": {
71+
"line": 1,
72+
"column": 15
73+
}
74+
}
75+
}
76+
],
77+
"sourceType": "script",
78+
"range": [
79+
0,
80+
15
81+
],
82+
"loc": {
83+
"start": {
84+
"line": 1,
85+
"column": 0
86+
},
87+
"end": {
88+
"line": 1,
89+
"column": 15
90+
}
91+
},
92+
"tokens": [
93+
{
94+
"type": "Keyword",
95+
"value": "try",
96+
"range": [
97+
0,
98+
3
99+
],
100+
"loc": {
101+
"start": {
102+
"line": 1,
103+
"column": 0
104+
},
105+
"end": {
106+
"line": 1,
107+
"column": 3
108+
}
109+
}
110+
},
111+
{
112+
"type": "Punctuator",
113+
"value": "{",
114+
"range": [
115+
4,
116+
5
117+
],
118+
"loc": {
119+
"start": {
120+
"line": 1,
121+
"column": 4
122+
},
123+
"end": {
124+
"line": 1,
125+
"column": 5
126+
}
127+
}
128+
},
129+
{
130+
"type": "Punctuator",
131+
"value": "}",
132+
"range": [
133+
5,
134+
6
135+
],
136+
"loc": {
137+
"start": {
138+
"line": 1,
139+
"column": 5
140+
},
141+
"end": {
142+
"line": 1,
143+
"column": 6
144+
}
145+
}
146+
},
147+
{
148+
"type": "Keyword",
149+
"value": "catch",
150+
"range": [
151+
7,
152+
12
153+
],
154+
"loc": {
155+
"start": {
156+
"line": 1,
157+
"column": 7
158+
},
159+
"end": {
160+
"line": 1,
161+
"column": 12
162+
}
163+
}
164+
},
165+
{
166+
"type": "Punctuator",
167+
"value": "{",
168+
"range": [
169+
13,
170+
14
171+
],
172+
"loc": {
173+
"start": {
174+
"line": 1,
175+
"column": 13
176+
},
177+
"end": {
178+
"line": 1,
179+
"column": 14
180+
}
181+
}
182+
},
183+
{
184+
"type": "Punctuator",
185+
"value": "}",
186+
"range": [
187+
14,
188+
15
189+
],
190+
"loc": {
191+
"start": {
192+
"line": 1,
193+
"column": 14
194+
},
195+
"end": {
196+
"line": 1,
197+
"column": 15
198+
}
199+
}
200+
}
201+
]
202+
}

test/test-262-whitelist.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,14 +1984,6 @@ test/language/statements/for-await-of/async-gen-dstr-var-obj-ptrn-rest-val-obj.j
19841984
test/language/statements/for-await-of/async-gen-dstr-var-obj-ptrn-rest-val-obj.js(strict mode)
19851985
test/language/statements/for-await-of/let-block-with-newline.js(default)
19861986
test/language/statements/for-await-of/let-identifier-with-newline.js(default)
1987-
test/language/statements/try/optional-catch-binding-finally.js(default)
1988-
test/language/statements/try/optional-catch-binding-finally.js(strict mode)
1989-
test/language/statements/try/optional-catch-binding-lexical.js(default)
1990-
test/language/statements/try/optional-catch-binding-lexical.js(strict mode)
1991-
test/language/statements/try/optional-catch-binding-throws.js(default)
1992-
test/language/statements/try/optional-catch-binding-throws.js(strict mode)
1993-
test/language/statements/try/optional-catch-binding.js(default)
1994-
test/language/statements/try/optional-catch-binding.js(strict mode)
19951987
test/built-ins/Function/prototype/toString/AsyncGenerator.js(default)
19961988
test/built-ins/Function/prototype/toString/AsyncGenerator.js(strict mode)
19971989
test/built-ins/Function/prototype/toString/async-generator-declaration.js(default)

0 commit comments

Comments
 (0)