Fixed invalid number literal parsing (#473)

* Fixed invalid number literal parsing

* Don't ignore period or E characters after octal numbers
cherry-pick fix from acorn

* Fix tests
This commit is contained in:
Alex Kuzmenko 2017-04-21 16:22:50 +03:00 committed by Daniel Tschinder
parent d1a5220b89
commit 00d6db9fbb
3 changed files with 12 additions and 4 deletions

View File

@ -582,30 +582,34 @@ export default class Tokenizer {
readNumber(startsWithDot: boolean): void {
const start = this.state.pos;
const firstIsZero = this.input.charCodeAt(start) === 48; // '0'
let octal = this.input.charCodeAt(start) === 48; // '0'
let isFloat = false;
if (!startsWithDot && this.readInt(10) === null) this.raise(start, "Invalid number");
if (octal && this.state.pos == start + 1) octal = false; // number === 0
let next = this.input.charCodeAt(this.state.pos);
if (next === 46) { // '.'
if (next === 46 && !octal) { // '.'
++this.state.pos;
this.readInt(10);
isFloat = true;
next = this.input.charCodeAt(this.state.pos);
}
if (next === 69 || next === 101) { // 'eE'
if ((next === 69 || next === 101) && !octal) { // 'eE'
next = this.input.charCodeAt(++this.state.pos);
if (next === 43 || next === 45) ++this.state.pos; // '+-'
if (this.readInt(10) === null) this.raise(start, "Invalid number");
isFloat = true;
}
if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.state.pos, "Identifier directly after number");
const str = this.input.slice(start, this.state.pos);
let val;
if (isFloat) {
val = parseFloat(str);
} else if (!firstIsZero || str.length === 1) {
} else if (!octal || str.length === 1) {
val = parseInt(str, 10);
} else if (this.state.strict) {
this.raise(start, "Invalid number");

View File

@ -0,0 +1 @@
var a = 0123.;

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:13)"
}