Skip to content

Commit 43cc9ca

Browse files
committed
Fix number parsing
1 parent 642be07 commit 43cc9ca

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

feature/number.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const num = a => {
1717
let str = strip(next(c =>
1818
// . is decimal only if NOT range (..) and NOT member access (.name)
1919
// Allows trailing decimal: 1. → 1, 0.95.toFixed → stops at second .
20-
(c === PERIOD && (c = cur.charCodeAt(idx + 1)) !== PERIOD && !(parse.id(c) && c > _9)) ||
20+
(c === PERIOD && (c = cur.charCodeAt(idx + 1)) !== PERIOD && !(parse.id(c) && c > _9 && c !== _e && c !== _E)) ||
2121
(c >= _0 && c <= _9) ||
2222
c === UNDERSCORE ||
2323
((c === _E || c === _e) && ((c = cur.charCodeAt(idx + 1)) >= _0 && c <= _9 || c === PLUS || c === MINUS) ? 2 : 0)

test/parse.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ test('parse: bad number', t => {
128128
throws(t => parse('.e-1'))
129129
})
130130

131+
test('parse: trailing-dot exponent (1.e3)', t => {
132+
// A trailing dot with no fractional digits, then an exponent, is one numeric
133+
// literal in JS (`1.e3 === 1000`). The lexer currently stops at `1.` (= 1) and
134+
// re-reads `e3` as a member access `(1).e3` → wrong value. These working forms
135+
// are the regression guards; the trailing-dot-exponent ones are the bug.
136+
is(parse('1.5e3'), [, 1500]) // fractional digits present — already correct
137+
is(parse('1.'), [, 1]) // trailing dot, no exponent — already correct
138+
is(parse('1e3'), [, 1000]) // no dot — already correct
139+
is(parse('1.e3'), [, 1000]) // BUG: parses as ['.', [, 1], 'e3']
140+
is(parse('0.e1'), [, 0]) // BUG: ['.', [, 0], 'e1']
141+
is(parse('0.E1'), [, 0]) // BUG: ['.', [, 0], 'E1']
142+
is(parse('1.e-3'), [, 1e-3]) // BUG: ['-', ['.', [, 1], 'e'], [, 3]]
143+
is(parse('1.e+3'), [, 1000]) // BUG: ['+', ['.', [, 1], 'e'], [, 3]]
144+
})
145+
131146
test('parse: intersecting binary', t => {
132147
is(parse('a | b'), ['|', 'a', 'b'], 'a|b')
133148
is(parse('a || b'), ['||', 'a', 'b'], 'a||b')

0 commit comments

Comments
 (0)