Skip to content

Commit fb7b56f

Browse files
sanex3339michaelficarra
authored andcommitted
Optional catch binding support (estools#405)
* Added support for optional-catch binding * Reverted optional catch binding condition
1 parent c917cf2 commit fb7b56f

File tree

6 files changed

+112
-9
lines changed

6 files changed

+112
-9
lines changed

escodegen.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,15 +1176,19 @@
11761176
withIndent(function () {
11771177
var guard;
11781178

1179-
result = [
1180-
'catch' + space + '(',
1181-
that.generateExpression(stmt.param, Precedence.Sequence, E_TTT),
1182-
')'
1183-
];
1179+
if (stmt.param) {
1180+
result = [
1181+
'catch' + space + '(',
1182+
that.generateExpression(stmt.param, Precedence.Sequence, E_TTT),
1183+
')'
1184+
];
11841185

1185-
if (stmt.guard) {
1186-
guard = that.generateExpression(stmt.guard, Precedence.Sequence, E_TTT);
1187-
result.splice(2, 0, ' if ', guard);
1186+
if (stmt.guard) {
1187+
guard = that.generateExpression(stmt.guard, Precedence.Sequence, E_TTT);
1188+
result.splice(2, 0, ' if ', guard);
1189+
}
1190+
} else {
1191+
result = ['catch'];
11881192
}
11891193
});
11901194
result.push(this.maybeBlock(stmt.body, S_TFFF));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"source-map": "~0.6.1"
4040
},
4141
"devDependencies": {
42-
"acorn": "^4.0.4",
42+
"acorn": "^7.1.0",
4343
"bluebird": "^3.4.7",
4444
"bower-registry-client": "^1.0.0",
4545
"chai": "^3.5.0",

test/compare-acorn-es2019.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
*/
24+
25+
'use strict';
26+
27+
var fs = require('fs'),
28+
acorn = require('acorn'),
29+
escodegen = require('./loader'),
30+
chai = require('chai'),
31+
expect = chai.expect;
32+
33+
function test(code, expected) {
34+
var tree, actual, options, StringObject;
35+
36+
// alias, so that JSLint does not complain.
37+
StringObject = String;
38+
39+
options = {
40+
ranges: true,
41+
locations: false,
42+
ecmaVersion: 10
43+
};
44+
45+
tree = acorn.parse(code, options);
46+
47+
// for UNIX text comment
48+
actual = escodegen.generate(tree).replace(/[\n\r]$/, '') + '\n';
49+
expect(actual).to.be.equal(expected);
50+
}
51+
52+
function testMin(code, expected) {
53+
var tree, actual, options, StringObject;
54+
55+
// alias, so that JSLint does not complain.
56+
StringObject = String;
57+
58+
options = {
59+
ranges: true,
60+
locations: false,
61+
ecmaVersion: 10
62+
};
63+
64+
tree = acorn.parse(code, options);
65+
66+
// for UNIX text comment
67+
actual = escodegen.generate(tree, {
68+
format: escodegen.FORMAT_MINIFY,
69+
raw: false
70+
}).replace(/[\n\r]$/, '') + '\n';
71+
expect(actual).to.be.equal(expected);
72+
}
73+
74+
describe('compare acorn es2019 test', function () {
75+
fs.readdirSync(__dirname + '/compare-acorn-es2019').sort().forEach(function(file) {
76+
var code, expected, exp, min;
77+
if (/\.js$/.test(file) && !/expected\.js$/.test(file) && !/expected\.min\.js$/.test(file)) {
78+
it(file, function () {
79+
exp = file.replace(/\.js$/, '.expected.js');
80+
min = file.replace(/\.js$/, '.expected.min.js');
81+
code = fs.readFileSync(__dirname + '/compare-acorn-es2019/' + file, 'utf-8');
82+
expected = fs.readFileSync(__dirname + '/compare-acorn-es2019/' + exp, 'utf-8');
83+
test(code, expected);
84+
if (fs.existsSync(__dirname + '/compare-acorn-es2019/' + min)) {
85+
expected = fs.readFileSync(__dirname + '/compare-acorn-es2019/' + min, 'utf-8');
86+
testMin(code, expected);
87+
}
88+
});
89+
}
90+
});
91+
});
92+
/* vim: set sw=4 ts=4 et tw=80 : */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
try {
2+
} catch {
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
try{}catch{}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
try {
2+
} catch {
3+
}

0 commit comments

Comments
 (0)