Skip to content

Commit 7fa058a

Browse files
committed
Support class field declarations.
Fixes: #443
1 parent 899cfdf commit 7fa058a

6 files changed

+145
-1
lines changed

escodegen.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,10 @@
21612161
return join(result, fragment);
21622162
},
21632163

2164+
PrivateIdentifier: function(expr, precedence, flags) {
2165+
return toSourceNodeWhenNeeded('#' + expr.name, expr);
2166+
},
2167+
21642168
Property: function (expr, precedence, flags) {
21652169
if (expr.kind === 'get' || expr.kind === 'set') {
21662170
return [
@@ -2192,6 +2196,22 @@
21922196
];
21932197
},
21942198

2199+
PropertyDefinition: function(expr, precedence, flags) {
2200+
var keywords = [];
2201+
if (expr.static) {
2202+
keywords.push('static');
2203+
}
2204+
2205+
return join(keywords, [
2206+
this.generatePropertyKey(expr.key, expr.computed),
2207+
space,
2208+
'=',
2209+
space,
2210+
this.generateExpression(expr.value, Precedence.Assignment, E_TTT),
2211+
this.semicolon(flags),
2212+
]);
2213+
},
2214+
21952215
ObjectExpression: function (expr, precedence, flags) {
21962216
var multiline, result, fragment, that = this;
21972217

@@ -2513,6 +2533,9 @@
25132533
return generateVerbatim(expr, precedence);
25142534
}
25152535

2536+
if (!(type in this)) {
2537+
throw new Error('Unknown node type: ' + type);
2538+
}
25162539
result = this[type](expr, precedence, flags);
25172540

25182541

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 <[email protected]>
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)