Skip to content

Commit

Permalink
fix: allow 09.1_1 and 09e1_1 in sloppy mode (#11854)
Browse files Browse the repository at this point in the history
* fix: allow 09.1_1 and 09e1_1 in sloppy mode

* polish: avoid extra input source scanning

* chore: move comment [skip ci]
  • Loading branch information
JLHwung committed Jul 21, 2020
1 parent 48be93b commit 3680f01
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 21 deletions.
37 changes: 16 additions & 21 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -1116,26 +1116,31 @@ export default class Tokenizer extends ParserErrors {
const start = this.state.pos;
let isFloat = false;
let isBigInt = false;
let isNonOctalDecimalInt = false;
let isOctal = false;

if (!startsWithDot && this.readInt(10) === null) {
this.raise(start, Errors.InvalidNumber);
}
let octal =
const hasLeadingZero =
this.state.pos - start >= 2 &&
this.input.charCodeAt(start) === charCodes.digit0;
if (octal) {

if (hasLeadingZero) {
const integer = this.input.slice(start, this.state.pos);
if (this.state.strict) {
this.raise(start, Errors.StrictOctalLiteral);
} else if (this.hasPlugin("numericSeparator")) {
// disallow numeric separators in non octal decimals and legacy octal likes
const underscorePos = integer.indexOf("_");
if (underscorePos > 0) {
this.raise(underscorePos + start, Errors.ZeroDigitNumericSeparator);
}
}
if (/[89]/.test(this.input.slice(start, this.state.pos))) {
octal = false;
isNonOctalDecimalInt = true;
}
isOctal = hasLeadingZero && !/[89]/.test(integer);
}

let next = this.input.charCodeAt(this.state.pos);
if (next === charCodes.dot && !octal) {
if (next === charCodes.dot && !isOctal) {
++this.state.pos;
this.readInt(10);
isFloat = true;
Expand All @@ -1144,7 +1149,7 @@ export default class Tokenizer extends ParserErrors {

if (
(next === charCodes.uppercaseE || next === charCodes.lowercaseE) &&
!octal
!isOctal
) {
next = this.input.charCodeAt(++this.state.pos);
if (next === charCodes.plusSign || next === charCodes.dash) {
Expand All @@ -1155,24 +1160,14 @@ export default class Tokenizer extends ParserErrors {
next = this.input.charCodeAt(this.state.pos);
}

// disallow numeric separators in non octal decimals and legacy octal likes
if (this.hasPlugin("numericSeparator") && (octal || isNonOctalDecimalInt)) {
const underscorePos = this.input
.slice(start, this.state.pos)
.indexOf("_");
if (underscorePos > 0) {
this.raise(underscorePos + start, Errors.ZeroDigitNumericSeparator);
}
}

if (next === charCodes.underscore) {
this.expectPlugin("numericSeparator", this.state.pos);
}

if (next === charCodes.lowercaseN) {
// disallow floats, legacy octal syntax and non octal decimals
// new style octal ("0o") is handled in this.readRadixNumber
if (isFloat || octal || isNonOctalDecimalInt) {
if (isFloat || hasLeadingZero) {
this.raise(start, Errors.InvalidBigIntLiteral);
}
++this.state.pos;
Expand All @@ -1191,7 +1186,7 @@ export default class Tokenizer extends ParserErrors {
return;
}

const val = octal ? parseInt(str, 8) : parseFloat(str);
const val = isOctal ? parseInt(str, 8) : parseFloat(str);
this.finishToken(tt.num, val);
}

Expand Down
@@ -0,0 +1 @@
09e1_1
@@ -0,0 +1,26 @@
{
"type": "File",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"program": {
"type": "Program",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"expression": {
"type": "NumericLiteral",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"extra": {
"rawValue": 900000000000,
"raw": "09e1_1"
},
"value": 900000000000
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
09.1_1
@@ -0,0 +1,26 @@
{
"type": "File",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"program": {
"type": "Program",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"expression": {
"type": "NumericLiteral",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}},
"extra": {
"rawValue": 9.11,
"raw": "09.1_1"
},
"value": 9.11
}
}
],
"directives": []
}
}

0 comments on commit 3680f01

Please sign in to comment.