Skip to content

Commit

Permalink
Fix number parser (babel#433)
Browse files Browse the repository at this point in the history
Fixed number parser #2

Added one more test
  • Loading branch information
oleksandr-kuzmenko authored and danez committed May 10, 2017
1 parent 8c885ea commit 406c3da
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ export default class Tokenizer {

readNumber(startsWithDot) {
const start = this.state.pos;
const octal = this.input.charCodeAt(this.state.pos) === 48;
const firstIsZero = this.input.charCodeAt(start) === 48; // '0'
let isFloat = false;

if (!startsWithDot && this.readInt(10) === null) this.raise(start, "Invalid number");
Expand All @@ -587,10 +587,12 @@ export default class Tokenizer {
let val;
if (isFloat) {
val = parseFloat(str);
} else if (!octal || str.length === 1) {
} else if (!firstIsZero || str.length === 1) {
val = parseInt(str, 10);
} else if (/[89]/.test(str) || this.state.strict) {
} else if (this.state.strict) {
this.raise(start, "Invalid number");
} else if (/[89]/.test(str)) {
val = parseInt(str, 10);
} else {
val = parseInt(str, 8);
}
Expand Down
69 changes: 69 additions & 0 deletions test/fixtures/core/uncategorised/355/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"expression": {
"type": "NumericLiteral",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"extra": {
"rawValue": 9,
"raw": "09"
},
"value": 9
}
}
],
"directives": []
}
}
3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/355/options.json

This file was deleted.

69 changes: 69 additions & 0 deletions test/fixtures/core/uncategorised/356/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"program": {
"type": "Program",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"expression": {
"type": "NumericLiteral",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"extra": {
"rawValue": 18,
"raw": "018"
},
"value": 18
}
}
],
"directives": []
}
}
3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/356/options.json

This file was deleted.

2 changes: 2 additions & 0 deletions test/fixtures/core/uncategorised/550/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
const a = 07;
3 changes: 3 additions & 0 deletions test/fixtures/core/uncategorised/550/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Invalid number (2:10)"
}
1 change: 1 addition & 0 deletions test/fixtures/core/uncategorised/551/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0111
69 changes: 69 additions & 0 deletions test/fixtures/core/uncategorised/551/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"program": {
"type": "Program",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"expression": {
"type": "NumericLiteral",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"extra": {
"rawValue": 73,
"raw": "0111"
},
"value": 73
}
}
],
"directives": []
}
}
2 changes: 2 additions & 0 deletions test/fixtures/core/uncategorised/552/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
const a = 08;
3 changes: 3 additions & 0 deletions test/fixtures/core/uncategorised/552/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Invalid number (2:10)"
}
1 change: 1 addition & 0 deletions test/fixtures/core/uncategorised/553/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0274134317073
69 changes: 69 additions & 0 deletions test/fixtures/core/uncategorised/553/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"program": {
"type": "Program",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"expression": {
"type": "NumericLiteral",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"extra": {
"rawValue": 25257156155,
"raw": "0274134317073"
},
"value": 25257156155
}
}
],
"directives": []
}
}

0 comments on commit 406c3da

Please sign in to comment.