Skip to content

Commit 8e2b303

Browse files
authored
Merge pull request #3 from javascript-obfuscator/es2022-class-fields-and-private-properties
Added support for ES2022 class fields and private properties
2 parents cea03b4 + e8dfec6 commit 8e2b303

File tree

9 files changed

+200
-14
lines changed

9 files changed

+200
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
v2.2.0
2+
---
3+
* Added support for ES2022 class fields and private properties
4+
15
v2.1.1
26
---
37
* Removed `bin` section from `package.json`

benchmark/old.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
FORMAT_MINIFY,
6262
FORMAT_DEFAULTS;
6363

64-
estraverse = require('estraverse');
64+
estraverse = require('@javascript-obfuscator/estraverse');
6565
esutils = require('esutils');
6666

6767
Syntax = {

escodegen.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
FORMAT_MINIFY,
6565
FORMAT_DEFAULTS;
6666

67-
estraverse = require('estraverse');
67+
estraverse = require('@javascript-obfuscator/estraverse');
6868
esutils = require('esutils');
6969

7070
Syntax = estraverse.Syntax;
@@ -2188,6 +2188,10 @@
21882188
return join(result, fragment);
21892189
},
21902190

2191+
PrivateIdentifier: function (expr, precedence, flags) {
2192+
return '#' + generateIdentifier(expr);
2193+
},
2194+
21912195
Property: function (expr, precedence, flags) {
21922196
if (expr.kind === 'get' || expr.kind === 'set') {
21932197
return [
@@ -2219,6 +2223,27 @@
22192223
];
22202224
},
22212225

2226+
PropertyDefinition: function (expr, precedence, flags) {
2227+
var result;
2228+
2229+
if (expr.static) {
2230+
result = ['static '];
2231+
} else {
2232+
result = [];
2233+
}
2234+
2235+
result.push(this.generatePropertyKey(expr.key, expr.computed));
2236+
2237+
if (expr.value) {
2238+
result.push( space + '=' + space);
2239+
result.push(this.generateExpression(expr.value, Precedence.Assignment, E_TTT));
2240+
}
2241+
2242+
result.push(this.semicolon(flags));
2243+
2244+
return result;
2245+
},
2246+
22222247
ObjectExpression: function (expr, precedence, flags) {
22232248
var multiline, result, fragment, that = this;
22242249

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"escodegen.js",
1111
"package.json"
1212
],
13-
"version": "2.1.1",
13+
"version": "2.2.0",
1414
"engines": {
1515
"node": ">=6.0"
1616
},
@@ -31,7 +31,7 @@
3131
"url": "http://github.com/estools/escodegen.git"
3232
},
3333
"dependencies": {
34-
"estraverse": "^5.2.0",
34+
"@javascript-obfuscator/estraverse": "^5.3.0",
3535
"esutils": "^2.0.2",
3636
"esprima": "^4.0.1",
3737
"optionator": "^0.8.1"
@@ -40,7 +40,7 @@
4040
"source-map": "~0.6.1"
4141
},
4242
"devDependencies": {
43-
"acorn": "^8.0.1",
43+
"acorn": "^8.2.2",
4444
"bluebird": "^3.4.7",
4545
"bower-registry-client": "^1.0.0",
4646
"chai": "^4.2.0",

test/compare-acorn-es2022.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
chaiExclude = require('chai-exclude'),
32+
expect = chai.expect;
33+
34+
chai.use(chaiExclude);
35+
36+
function test(code, expected) {
37+
var tree, actual, actualTree, options;
38+
39+
options = {
40+
ranges: false,
41+
locations: false,
42+
ecmaVersion: 13
43+
};
44+
45+
tree = acorn.parse(code, options);
46+
47+
// for UNIX text comment
48+
actual = escodegen.generate(tree);
49+
actualTree = acorn.parse(actual, options);
50+
51+
expect(actual).to.be.equal(expected);
52+
expect(tree).excludingEvery(['start', 'end']).to.deep.equal(actualTree);
53+
}
54+
55+
function testMin(code, expected) {
56+
var tree, actual, actualTree, options;
57+
58+
options = {
59+
ranges: false,
60+
locations: false,
61+
ecmaVersion: 13
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+
actualTree = acorn.parse(actual, options);
72+
73+
expect(actual).to.be.equal(expected);
74+
expect(tree).excludingEvery(['start', 'end']).to.deep.equal(actualTree);
75+
}
76+
77+
describe('compare acorn es2022 test', function () {
78+
fs.readdirSync(__dirname + '/compare-acorn-es2022').sort().forEach(function(file) {
79+
var code, expected, exp, min;
80+
if (/\.js$/.test(file) && !/expected\.js$/.test(file) && !/expected\.min\.js$/.test(file)) {
81+
it(file, function () {
82+
exp = file.replace(/\.js$/, '.expected.js');
83+
min = file.replace(/\.js$/, '.expected.min.js');
84+
code = fs.readFileSync(__dirname + '/compare-acorn-es2022/' + file, 'utf-8');
85+
expected = fs.readFileSync(__dirname + '/compare-acorn-es2022/' + exp, 'utf-8');
86+
test(code, expected);
87+
if (fs.existsSync(__dirname + '/compare-acorn-es2022/' + min)) {
88+
expected = fs.readFileSync(__dirname + '/compare-acorn-es2022/' + min, 'utf-8');
89+
testMin(code, expected);
90+
}
91+
});
92+
}
93+
});
94+
});
95+
/* vim: set sw=4 ts=4 et tw=80 : */
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class C1 {
2+
aaa;
3+
}
4+
class C2 {
5+
A;
6+
}
7+
class C3 {
8+
'0';
9+
}
10+
class C4 {
11+
100;
12+
}
13+
class C5 {
14+
[0];
15+
}
16+
class C6 {
17+
aaa = bbb;
18+
}
19+
class C7 {
20+
aaa = () => 0;
21+
}
22+
class C8 {
23+
static aaa;
24+
}
25+
class C9 {
26+
static aaa = bbb;
27+
}
28+
class C10 {
29+
static aaa = 1;
30+
bbb = 2;
31+
}
32+
class C11 {
33+
get;
34+
set;
35+
static;
36+
async;
37+
}
38+
class C12 {
39+
#aaa;
40+
}
41+
class C13 {
42+
#A;
43+
}
44+
class C14 {
45+
#𩸽;
46+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class C1{aaa;}class C2{A;}class C3{'0';}class C4{100;}class C5{[0];}class C6{aaa=bbb;}class C7{aaa=()=>0;}class C8{static aaa;}class C9{static aaa=bbb;}class C10{static aaa=1;bbb=2;}class C11{get;set;static;async;}class C12{#aaa;}class C13{#A;}class C14{#𩸽;}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class C1 { aaa; }
2+
class C2 { \u0041; }
3+
class C3 { '0'; }
4+
class C4 { 100; }
5+
class C5 { [0]; }
6+
class C6 { aaa = bbb; }
7+
class C7 { aaa = () => 0; }
8+
class C8 { static aaa; }
9+
class C9 { static aaa = bbb; }
10+
class C10 { static aaa = 1; bbb = 2; }
11+
class C11 { get; set; static; async }
12+
13+
class C12 { #aaa; }
14+
class C13 { #\u0041; }
15+
class C14 { #𩸽; }

0 commit comments

Comments
 (0)