Parse non-octals with leading zeros in non strict mode correctly (#9114)
* Parse non-octals with leading zeros in non strict mode correctly * Better error message
This commit is contained in:
parent
07eaa3c63f
commit
3932830535
@ -971,14 +971,26 @@ export default class Tokenizer extends LocationParser {
|
|||||||
|
|
||||||
readNumber(startsWithDot: boolean): void {
|
readNumber(startsWithDot: boolean): void {
|
||||||
const start = this.state.pos;
|
const start = this.state.pos;
|
||||||
let octal = this.input.charCodeAt(start) === charCodes.digit0;
|
|
||||||
let isFloat = false;
|
let isFloat = false;
|
||||||
let isBigInt = false;
|
let isBigInt = false;
|
||||||
|
|
||||||
if (!startsWithDot && this.readInt(10) === null) {
|
if (!startsWithDot && this.readInt(10) === null) {
|
||||||
this.raise(start, "Invalid number");
|
this.raise(start, "Invalid number");
|
||||||
}
|
}
|
||||||
if (octal && this.state.pos == start + 1) octal = false; // number === 0
|
let octal =
|
||||||
|
this.state.pos - start >= 2 &&
|
||||||
|
this.input.charCodeAt(start) === charCodes.digit0;
|
||||||
|
if (octal) {
|
||||||
|
if (this.state.strict) {
|
||||||
|
this.raise(
|
||||||
|
start,
|
||||||
|
"Legacy octal literals are not allowed in strict mode",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (/[89]/.test(this.input.slice(start, this.state.pos))) {
|
||||||
|
octal = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let next = this.input.charCodeAt(this.state.pos);
|
let next = this.input.charCodeAt(this.state.pos);
|
||||||
if (next === charCodes.dot && !octal) {
|
if (next === charCodes.dot && !octal) {
|
||||||
@ -1022,18 +1034,7 @@ export default class Tokenizer extends LocationParser {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let val;
|
const val = octal ? parseInt(str, 8) : parseFloat(str);
|
||||||
if (isFloat) {
|
|
||||||
val = parseFloat(str);
|
|
||||||
} else if (!octal || str.length === 1) {
|
|
||||||
val = parseInt(str, 10);
|
|
||||||
} else if (this.state.strict) {
|
|
||||||
this.raise(start, "Invalid number");
|
|
||||||
} else if (/[89]/.test(str)) {
|
|
||||||
val = parseInt(str, 10);
|
|
||||||
} else {
|
|
||||||
val = parseInt(str, 8);
|
|
||||||
}
|
|
||||||
this.finishToken(tt.num, val);
|
this.finishToken(tt.num, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
1
packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
09.5
|
||||||
4
packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/options.json
vendored
Normal file
4
packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"sourceType": "module",
|
||||||
|
"throws": "Legacy octal literals are not allowed in strict mode (1:0)"
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/core/regression/non-octal-float/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/core/regression/non-octal-float/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
09.5
|
||||||
70
packages/babel-parser/test/fixtures/core/regression/non-octal-float/output.json
vendored
Normal file
70
packages/babel-parser/test/fixtures/core/regression/non-octal-float/output.json
vendored
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
{
|
||||||
|
"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",
|
||||||
|
"interpreter": null,
|
||||||
|
"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": 9.5,
|
||||||
|
"raw": "09.5"
|
||||||
|
},
|
||||||
|
"value": 9.5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/core/regression/octal-float-fail/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/core/regression/octal-float-fail/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
07.5
|
||||||
3
packages/babel-parser/test/fixtures/core/regression/octal-float-fail/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/core/regression/octal-float-fail/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"throws": "Unexpected token, expected \";\" (1:2)"
|
||||||
|
}
|
||||||
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"throws": "Invalid number (1:33)"
|
"throws": "Legacy octal literals are not allowed in strict mode (1:33)"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"throws": "Invalid number (1:36)"
|
"throws": "Legacy octal literals are not allowed in strict mode (1:36)"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"throws": "Invalid number (1:65)"
|
"throws": "Legacy octal literals are not allowed in strict mode (1:65)"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"throws": "Invalid number (2:10)"
|
"throws": "Legacy octal literals are not allowed in strict mode (2:10)"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"throws": "Invalid number (2:10)"
|
"throws": "Legacy octal literals are not allowed in strict mode (2:10)"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"throws": "Invalid number (1:21)"
|
"throws": "Legacy octal literals are not allowed in strict mode (1:21)"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"throws": "Invalid number (1:21)"
|
"throws": "Legacy octal literals are not allowed in strict mode (1:21)"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"throws": "Invalid number (1:33)"
|
"throws": "Legacy octal literals are not allowed in strict mode (1:33)"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"throws": "Invalid number (1:36)"
|
"throws": "Legacy octal literals are not allowed in strict mode (1:36)"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user