Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow 09.1_1 and 09e1_1 in sloppy mode #11854

Merged
merged 3 commits into from Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {

// disallow numeric separators in non octal decimals and legacy octal likes
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")) {
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": []
}
}