Skip to content

Commit 694da53

Browse files
committed
Support class field declarations.
Fixes: estools#443
1 parent 899cfdf commit 694da53

6 files changed

+152
-3
lines changed

escodegen.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,11 +1126,16 @@
11261126
var result = [ '{', newline], that = this;
11271127

11281128
withIndent(function (indent) {
1129-
var i, iz;
1129+
var bodyFlags, i, iz;
11301130

1131+
bodyFlags = E_TTT;
11311132
for (i = 0, iz = stmt.body.length; i < iz; ++i) {
1133+
if (i === iz - 1) {
1134+
bodyFlags |= F_SEMICOLON_OPT;
1135+
}
1136+
11321137
result.push(indent);
1133-
result.push(that.generateExpression(stmt.body[i], Precedence.Sequence, E_TTT));
1138+
result.push(that.generateExpression(stmt.body[i], Precedence.Sequence, bodyFlags));
11341139
if (i + 1 < iz) {
11351140
result.push(newline);
11361141
}
@@ -2161,6 +2166,10 @@
21612166
return join(result, fragment);
21622167
},
21632168

2169+
PrivateIdentifier: function(expr, precedence, flags) {
2170+
return toSourceNodeWhenNeeded('#' + expr.name, expr);
2171+
},
2172+
21642173
Property: function (expr, precedence, flags) {
21652174
if (expr.kind === 'get' || expr.kind === 'set') {
21662175
return [
@@ -2192,6 +2201,22 @@
21922201
];
21932202
},
21942203

2204+
PropertyDefinition: function(expr, precedence, flags) {
2205+
var keywords = [];
2206+
if (expr.static) {
2207+
keywords.push('static');
2208+
}
2209+
2210+
return join(keywords, [
2211+
this.generatePropertyKey(expr.key, expr.computed),
2212+
space,
2213+
'=',
2214+
space,
2215+
this.generateExpression(expr.value, Precedence.Assignment, E_TTT),
2216+
this.semicolon(flags),
2217+
]);
2218+
},
2219+
21952220
ObjectExpression: function (expr, precedence, flags) {
21962221
var multiline, result, fragment, that = this;
21972222

@@ -2513,6 +2538,9 @@
25132538
return generateVerbatim(expr, precedence);
25142539
}
25152540

2541+
if (!(type in this)) {
2542+
throw new Error('Unknown node type: ' + type);
2543+
}
25162544
result = this[type](expr, precedence, flags);
25172545

25182546

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"source-map": "~0.6.1"
3939
},
4040
"devDependencies": {
41-
"acorn": "^8.0.4",
41+
"acorn": "^8.2.0",
4242
"bluebird": "^3.4.7",
4343
"bower-registry-client": "^1.0.0",
4444
"chai": "^4.2.0",

test/compare-acorn-es2022.js

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

0 commit comments

Comments
 (0)