[parser] Invalid NonOctal Decimal (#10467)

* Add test

* Add nonOctalDecimal verification

* Update regex and code style

* Refactor non octal detection

* Add numeric separator test

* Disallow numeric separators in non octals

* Update whitelist

* Better test naming

* Disallow numeric separators in non oct for all numbers

* Specific error above the general error

* Add test for invalid leading zero in num separator

* Add start position to error
This commit is contained in:
Gonzalo Rodríguez
2019-09-23 17:07:52 -04:00
committed by Nicolò Ribaudo
parent f339d2d034
commit 69d00dc5b0
9 changed files with 27 additions and 17 deletions

View File

@@ -993,6 +993,7 @@ export default class Tokenizer extends LocationParser {
const start = this.state.pos;
let isFloat = false;
let isBigInt = false;
let isNonOctalDecimalInt = false;
if (!startsWithDot && this.readInt(10) === null) {
this.raise(start, "Invalid number");
@@ -1009,6 +1010,7 @@ export default class Tokenizer extends LocationParser {
}
if (/[89]/.test(this.input.slice(start, this.state.pos))) {
octal = false;
isNonOctalDecimalInt = true;
}
}
@@ -1033,10 +1035,26 @@ export default class Tokenizer extends LocationParser {
next = this.input.charCodeAt(this.state.pos);
}
// disallow numeric separators in non octal decimals
if (this.hasPlugin("numericSeparator") && isNonOctalDecimalInt) {
const underscorePos = this.input
.slice(start, this.state.pos)
.indexOf("_");
if (underscorePos > 0) {
this.raise(
underscorePos + start,
"Numeric separator can not be used after leading 0",
);
}
}
if (this.hasPlugin("bigInt")) {
if (next === charCodes.lowercaseN) {
// disallow floats and legacy octal syntax, new style octal ("0o") is handled in this.readRadixNumber
if (isFloat || octal) this.raise(start, "Invalid BigIntLiteral");
// disallow floats, legacy octal syntax and non octal decimals
// new style octal ("0o") is handled in this.readRadixNumber
if (isFloat || octal || isNonOctalDecimalInt) {
this.raise(start, "Invalid BigIntLiteral");
}
++this.state.pos;
isBigInt = true;
}

View File

@@ -0,0 +1 @@
{ "throws": "Invalid BigIntLiteral (1:0)" }

View File

@@ -0,0 +1 @@
{ "throws": "Numeric separator can not be used after leading 0 (1:1)" }

View File

@@ -0,0 +1 @@
{ "throws": "Numeric separator can not be used after leading 0 (1:2)" }

View File

@@ -1,3 +1,3 @@
{
"plugins": ["numericSeparator"]
"plugins": ["bigInt", "numericSeparator"]
}