Skip to content

Commit

Permalink
Fixed invalid number literal parsing (babel#473)
Browse files Browse the repository at this point in the history
* Fixed invalid number literal parsing

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

* Fix tests
  • Loading branch information
oleksandr-kuzmenko authored and danez committed May 10, 2017
1 parent 406c3da commit b98f463
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,30 +564,34 @@ export default class Tokenizer {

readNumber(startsWithDot) {
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");
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/core/uncategorised/554/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a = 0123.;
3 changes: 3 additions & 0 deletions test/fixtures/core/uncategorised/554/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:13)"
}

0 comments on commit b98f463

Please sign in to comment.