Skip to content

Commit 92c067e

Browse files
committed
Added support for BigInt and Numeric Separators
1 parent ab53cd5 commit 92c067e

9 files changed

+134
-1
lines changed

escodegen.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,7 +2356,17 @@
23562356
return escapeString(expr.value);
23572357
}
23582358

2359+
// BigInt, eg: 1n
2360+
if (typeof expr.bigint === 'string' && expr.raw) {
2361+
return expr.raw;
2362+
}
2363+
23592364
if (typeof expr.value === 'number') {
2365+
// Has Numeric Separator
2366+
if (expr.raw && expr.raw.indexOf('_') !== -1) {
2367+
return expr.raw;
2368+
}
2369+
23602370
return generateNumber(expr.value);
23612371
}
23622372

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": "^7.3.1",
42+
"acorn": "^8.0.1",
4343
"bluebird": "^3.4.7",
4444
"bower-registry-client": "^1.0.0",
4545
"chai": "^4.2.0",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
0n;
2+
2n;
3+
0x2n;
4+
0o2n;
5+
0b10n;
6+
-0xbf2ed51ff75d380fd3be813ec6185780n;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0n;2n;0x2n;0o2n;0b10n;-0xbf2ed51ff75d380fd3be813ec6185780n

test/compare-acorn-es2020/bigint.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
0n;
2+
2n;
3+
0x2n;
4+
0o2n;
5+
0b10n;
6+
-0xbf2ed51ff75d380fd3be813ec6185780n;

test/compare-acorn-es2021.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: 12
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: 12
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 es2021 test', function () {
78+
fs.readdirSync(__dirname + '/compare-acorn-es2021').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-es2021/' + file, 'utf-8');
85+
expected = fs.readFileSync(__dirname + '/compare-acorn-es2021/' + exp, 'utf-8');
86+
test(code, expected);
87+
if (fs.existsSync(__dirname + '/compare-acorn-es2021/' + min)) {
88+
expected = fs.readFileSync(__dirname + '/compare-acorn-es2021/' + min, 'utf-8');
89+
testMin(code, expected);
90+
}
91+
});
92+
}
93+
});
94+
});
95+
/* vim: set sw=4 ts=4 et tw=80 : */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
123_456;
2+
123_456.123_456e+123_456;
3+
0b1010_0001;
4+
0xDEAD_BEAF;
5+
0o755_666;
6+
123_456n;
7+
.012_345;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123_456;123_456.123_456e+123_456;0b1010_0001;0xDEAD_BEAF;0o755_666;123_456n;.012_345
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
123_456;
2+
123_456.123_456e+123_456;
3+
0b1010_0001;
4+
0xDEAD_BEAF;
5+
0o755_666;
6+
123_456n;
7+
.012_345;

0 commit comments

Comments
 (0)