Skip to content

Commit 885bc01

Browse files
committed
Support numeric separators
1 parent 90ab159 commit 885bc01

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ compile('acorn-import-meta', './lib/import-meta/index.js')
2727
compile('acorn-export-ns-from', './lib/export-ns-from/index.js')
2828
compile('acorn-class-fields', './lib/class-fields/index.js', privateClassElements)
2929
compile('acorn-static-class-features', './lib/static-class-features/index.js', privateClassElements)
30+
compile('acorn-numeric-separator', './lib/numeric-separator/index.js')
3031
compile('acorn-private-class-elements', './lib/private-class-elements/index.js', function (str) {
3132
return str.replace('class extends Parser', 'class Parser_ extends Parser')
3233
})

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var CJSParser = acorn.Parser
55
.extend(require('./lib/bigint'))
66
.extend(require('./lib/class-fields'))
77
.extend(require('./lib/static-class-features'))
8+
.extend(require('./lib/numeric-separator'))
89
var ESModulesParser = CJSParser
910
.extend(require('./lib/export-ns-from'))
1011
.extend(require('./lib/import-meta'))

lib/numeric-separator/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Generated by `npm run build`, do not edit! */
2+
3+
"use strict"
4+
5+
module.exports = function(Parser) {
6+
return /*@__PURE__*/(function (Parser) {
7+
function anonymous () {
8+
Parser.apply(this, arguments);
9+
}
10+
11+
if ( Parser ) anonymous.__proto__ = Parser;
12+
anonymous.prototype = Object.create( Parser && Parser.prototype );
13+
anonymous.prototype.constructor = anonymous;
14+
15+
anonymous.prototype.readInt = function readInt (radix, len) {
16+
// Hack: len is only != null for unicode escape sequences,
17+
// where numeric separators are not allowed
18+
if (len != null) { return Parser.prototype.readInt.call(this, radix, len) }
19+
20+
var start = this.pos, total = 0, acceptUnderscore = false
21+
for (;;) {
22+
var code = this.input.charCodeAt(this.pos), val = (void 0)
23+
if (code >= 97) { val = code - 97 + 10 } // a
24+
else if (code == 95) {
25+
if (!acceptUnderscore) { this.raise(this.pos, "Invalid numeric separator") }
26+
++this.pos
27+
acceptUnderscore = false
28+
continue
29+
} else if (code >= 65) { val = code - 65 + 10 } // A
30+
else if (code >= 48 && code <= 57) { val = code - 48 } // 0-9
31+
else { val = Infinity }
32+
if (val >= radix) { break }
33+
++this.pos
34+
total = total * radix + val
35+
acceptUnderscore = true
36+
}
37+
if (this.pos === start) { return null }
38+
if (!acceptUnderscore) { this.raise(this.pos - 1, "Invalid numeric separator") }
39+
40+
return total
41+
};
42+
43+
anonymous.prototype.readNumber = function readNumber (startsWithDot) {
44+
var token = Parser.prototype.readNumber.call(this, startsWithDot)
45+
var octal = this.end - this.start >= 2 && this.input.charCodeAt(this.start) === 48
46+
var stripped = this.getNumberInput(this.start, this.end)
47+
if (stripped.length < this.end - this.start) {
48+
if (octal) { this.raise(this.start, "Invalid number") }
49+
this.value = parseFloat(stripped)
50+
}
51+
return token
52+
};
53+
54+
// This is used by acorn-bigint
55+
anonymous.prototype.getNumberInput = function getNumberInput (start, end) {
56+
return this.input.slice(start, end).replace(/_/g, "")
57+
};
58+
59+
return anonymous;
60+
}(Parser))
61+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"acorn-class-fields": "^0.3.1",
1717
"acorn-export-ns-from": "^0.1.0",
1818
"acorn-import-meta": "^1.0.0",
19+
"acorn-numeric-separator": "^0.3.0",
1920
"acorn-private-class-elements": "^0.1.1",
2021
"acorn-static-class-features": "^0.2.0",
2122
"buble": "^0.19.8",

test/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ test('supports bigint', function (t) {
7373
t.end()
7474
})
7575

76+
test('supports numeric separators', function (t) {
77+
t.doesNotThrow(function () {
78+
acorn.parse('50_000_000n ** 1n')
79+
})
80+
t.end()
81+
})
82+
7683
test('supports import.meta with sourceType: module', function (t) {
7784
t.doesNotThrow(function () {
7885
acorn.parse('console.log(import.meta.url)', { sourceType: 'module' })

0 commit comments

Comments
 (0)