diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index a0ee5badf41e..b57006ec6922 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -971,14 +971,26 @@ export default class Tokenizer extends LocationParser { readNumber(startsWithDot: boolean): void { const start = this.state.pos; - let octal = this.input.charCodeAt(start) === charCodes.digit0; let isFloat = false; let isBigInt = false; if (!startsWithDot && this.readInt(10) === null) { 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); if (next === charCodes.dot && !octal) { @@ -1022,18 +1034,7 @@ export default class Tokenizer extends LocationParser { return; } - let val; - 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); - } + const val = octal ? parseInt(str, 8) : parseFloat(str); this.finishToken(tt.num, val); } diff --git a/packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/input.js b/packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/input.js new file mode 100644 index 000000000000..cbc4507bfeae --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/input.js @@ -0,0 +1 @@ +09.5 diff --git a/packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/options.json b/packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/options.json new file mode 100644 index 000000000000..956965603040 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/regression/non-octal-float-strict-mode/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "throws": "Legacy octal literals are not allowed in strict mode (1:0)" +} diff --git a/packages/babel-parser/test/fixtures/core/regression/non-octal-float/input.js b/packages/babel-parser/test/fixtures/core/regression/non-octal-float/input.js new file mode 100644 index 000000000000..cbc4507bfeae --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/regression/non-octal-float/input.js @@ -0,0 +1 @@ +09.5 diff --git a/packages/babel-parser/test/fixtures/core/regression/non-octal-float/output.json b/packages/babel-parser/test/fixtures/core/regression/non-octal-float/output.json new file mode 100644 index 000000000000..f48f7a786775 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/regression/non-octal-float/output.json @@ -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": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/core/regression/octal-float-fail/input.js b/packages/babel-parser/test/fixtures/core/regression/octal-float-fail/input.js new file mode 100644 index 000000000000..80b817b29181 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/regression/octal-float-fail/input.js @@ -0,0 +1 @@ +07.5 diff --git a/packages/babel-parser/test/fixtures/core/regression/octal-float-fail/options.json b/packages/babel-parser/test/fixtures/core/regression/octal-float-fail/options.json new file mode 100644 index 000000000000..ff1ac8247e68 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/regression/octal-float-fail/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \";\" (1:2)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/500/options.json b/packages/babel-parser/test/fixtures/core/uncategorised/500/options.json index ec4ea95dc9cb..45ca229c1e8e 100644 --- a/packages/babel-parser/test/fixtures/core/uncategorised/500/options.json +++ b/packages/babel-parser/test/fixtures/core/uncategorised/500/options.json @@ -1,3 +1,3 @@ { - "throws": "Invalid number (1:33)" + "throws": "Legacy octal literals are not allowed in strict mode (1:33)" } diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/502/options.json b/packages/babel-parser/test/fixtures/core/uncategorised/502/options.json index dd200e16e5bf..8f6c8218f747 100644 --- a/packages/babel-parser/test/fixtures/core/uncategorised/502/options.json +++ b/packages/babel-parser/test/fixtures/core/uncategorised/502/options.json @@ -1,3 +1,3 @@ { - "throws": "Invalid number (1:36)" + "throws": "Legacy octal literals are not allowed in strict mode (1:36)" } diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/522/options.json b/packages/babel-parser/test/fixtures/core/uncategorised/522/options.json index 3696be182d05..0b443b9bacfa 100644 --- a/packages/babel-parser/test/fixtures/core/uncategorised/522/options.json +++ b/packages/babel-parser/test/fixtures/core/uncategorised/522/options.json @@ -1,3 +1,3 @@ { - "throws": "Invalid number (1:65)" + "throws": "Legacy octal literals are not allowed in strict mode (1:65)" } diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/550/options.json b/packages/babel-parser/test/fixtures/core/uncategorised/550/options.json index f635fb88c5c9..5cee88d901e4 100644 --- a/packages/babel-parser/test/fixtures/core/uncategorised/550/options.json +++ b/packages/babel-parser/test/fixtures/core/uncategorised/550/options.json @@ -1,3 +1,3 @@ { - "throws": "Invalid number (2:10)" + "throws": "Legacy octal literals are not allowed in strict mode (2:10)" } diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/552/options.json b/packages/babel-parser/test/fixtures/core/uncategorised/552/options.json index f635fb88c5c9..5cee88d901e4 100644 --- a/packages/babel-parser/test/fixtures/core/uncategorised/552/options.json +++ b/packages/babel-parser/test/fixtures/core/uncategorised/552/options.json @@ -1,3 +1,3 @@ { - "throws": "Invalid number (2:10)" + "throws": "Legacy octal literals are not allowed in strict mode (2:10)" } diff --git a/packages/babel-parser/test/fixtures/es2015/uncategorised/249/options.json b/packages/babel-parser/test/fixtures/es2015/uncategorised/249/options.json index 0f2c1bee90b0..79a05e62290e 100644 --- a/packages/babel-parser/test/fixtures/es2015/uncategorised/249/options.json +++ b/packages/babel-parser/test/fixtures/es2015/uncategorised/249/options.json @@ -1,3 +1,3 @@ { - "throws": "Invalid number (1:21)" + "throws": "Legacy octal literals are not allowed in strict mode (1:21)" } diff --git a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0094/options.json b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0094/options.json index 0f2c1bee90b0..79a05e62290e 100644 --- a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0094/options.json +++ b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0094/options.json @@ -1,3 +1,3 @@ { - "throws": "Invalid number (1:21)" + "throws": "Legacy octal literals are not allowed in strict mode (1:21)" } diff --git a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0218/options.json b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0218/options.json index ec4ea95dc9cb..45ca229c1e8e 100644 --- a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0218/options.json +++ b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0218/options.json @@ -1,3 +1,3 @@ { - "throws": "Invalid number (1:33)" + "throws": "Legacy octal literals are not allowed in strict mode (1:33)" } diff --git a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0220/options.json b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0220/options.json index dd200e16e5bf..8f6c8218f747 100644 --- a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0220/options.json +++ b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0220/options.json @@ -1,3 +1,3 @@ { - "throws": "Invalid number (1:36)" + "throws": "Legacy octal literals are not allowed in strict mode (1:36)" }