diff --git a/packages/babel-parser/src/parser/error-message.js b/packages/babel-parser/src/parser/error-message.js index 5a7df725361c..1651bb344dc3 100644 --- a/packages/babel-parser/src/parser/error-message.js +++ b/packages/babel-parser/src/parser/error-message.js @@ -139,6 +139,7 @@ export const ErrorMessages = Object.freeze({ StrictEvalArgumentsBinding: "Binding '%0' in strict mode", StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block", + StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'", StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode", StrictWith: "'with' in strict mode", SuperNotAllowed: diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 1a8af6cbbdd8..56d880661e56 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -1362,6 +1362,8 @@ export default class Tokenizer extends ParserErrors { case charCodes.digit9: if (inTemplate) { return null; + } else if (this.state.strict) { + this.raise(this.state.pos - 1, Errors.StrictNumericEscape); } // fall through default: @@ -1390,7 +1392,7 @@ export default class Tokenizer extends ParserErrors { if (inTemplate) { return null; } else if (this.state.strict) { - this.raise(codePos, Errors.StrictOctalLiteral); + this.raise(codePos, Errors.StrictNumericEscape); } else { // This property is used to throw an error for // an octal literal in a directive that occurs prior diff --git a/packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive-function/input.js b/packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive-function/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive-function/input.js rename to packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive-function/input.js diff --git a/packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive-function/options.json b/packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive-function/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive-function/options.json rename to packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive-function/options.json diff --git a/packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive-function/output.json b/packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive-function/output.json similarity index 98% rename from packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive-function/output.json rename to packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive-function/output.json index 2eeced4be950..78d725b77c45 100644 --- a/packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive-function/output.json +++ b/packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive-function/output.json @@ -5,9 +5,9 @@ "SyntaxError: Legacy octal literals are not allowed in strict mode (2:4)", "SyntaxError: Legacy octal literals are not allowed in strict mode (7:4)", "SyntaxError: Legacy octal literals are not allowed in strict mode (8:4)", - "SyntaxError: Legacy octal literals are not allowed in strict mode (14:4)", - "SyntaxError: Legacy octal literals are not allowed in strict mode (19:4)", - "SyntaxError: Legacy octal literals are not allowed in strict mode (20:4)", + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (14:4)", + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (19:4)", + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (20:4)", "SyntaxError: Legacy octal literals are not allowed in strict mode (25:2)", "SyntaxError: Legacy octal literals are not allowed in strict mode (30:2)", "SyntaxError: Legacy octal literals are not allowed in strict mode (31:2)" diff --git a/packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive/input.js b/packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive/input.js rename to packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive/input.js diff --git a/packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive/output.json b/packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive/output.json similarity index 92% rename from packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive/output.json rename to packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive/output.json index f4d5ed29410f..e57ef5ed03f3 100644 --- a/packages/babel-parser/test/fixtures/core/regression/invalid-octal-strict-directive/output.json +++ b/packages/babel-parser/test/fixtures/core/escape-string/invalid-octal-strict-directive/output.json @@ -2,13 +2,13 @@ "type": "File", "start":0,"end":96,"loc":{"start":{"line":1,"column":0},"end":{"line":14,"column":22}}, "errors": [ - "SyntaxError: Legacy octal literals are not allowed in strict mode (8:2)", - "SyntaxError: Legacy octal literals are not allowed in strict mode (9:2)", + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (8:2)", + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (9:2)", "SyntaxError: Legacy octal literals are not allowed in strict mode (11:0)", "SyntaxError: Legacy octal literals are not allowed in strict mode (12:0)", - "SyntaxError: Legacy octal literals are not allowed in strict mode (14:2)", - "SyntaxError: Legacy octal literals are not allowed in strict mode (14:10)", - "SyntaxError: Legacy octal literals are not allowed in strict mode (14:18)", + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (14:2)", + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (14:10)", + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (14:18)", "SyntaxError: Legacy octal literals are not allowed in strict mode (1:2)", "SyntaxError: Legacy octal literals are not allowed in strict mode (1:10)", "SyntaxError: Legacy octal literals are not allowed in strict mode (1:18)", diff --git a/packages/babel-parser/test/fixtures/core/escape-string/non-octal-eight-and-nine/input.js b/packages/babel-parser/test/fixtures/core/escape-string/non-octal-eight-and-nine/input.js new file mode 100644 index 000000000000..191e53efd416 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/escape-string/non-octal-eight-and-nine/input.js @@ -0,0 +1,5 @@ +"\8","\9"; +() => { + "use strict"; + "\8", "\9"; +} diff --git a/packages/babel-parser/test/fixtures/core/escape-string/non-octal-eight-and-nine/output.json b/packages/babel-parser/test/fixtures/core/escape-string/non-octal-eight-and-nine/output.json new file mode 100644 index 000000000000..0c5b417df26e --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/escape-string/non-octal-eight-and-nine/output.json @@ -0,0 +1,106 @@ +{ + "type": "File", + "start":0,"end":50,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, + "errors": [ + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (4:4)", + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (4:10)" + ], + "program": { + "type": "Program", + "start":0,"end":50,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "expression": { + "type": "SequenceExpression", + "start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}}, + "expressions": [ + { + "type": "StringLiteral", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "extra": { + "rawValue": "8", + "raw": "\"\\8\"" + }, + "value": "8" + }, + { + "type": "StringLiteral", + "start":5,"end":9,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":9}}, + "extra": { + "rawValue": "9", + "raw": "\"\\9\"" + }, + "value": "9" + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":11,"end":50,"loc":{"start":{"line":2,"column":0},"end":{"line":5,"column":1}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":11,"end":50,"loc":{"start":{"line":2,"column":0},"end":{"line":5,"column":1}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":17,"end":50,"loc":{"start":{"line":2,"column":6},"end":{"line":5,"column":1}}, + "body": [ + { + "type": "ExpressionStatement", + "start":37,"end":48,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":13}}, + "expression": { + "type": "SequenceExpression", + "start":37,"end":47,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":12}}, + "expressions": [ + { + "type": "StringLiteral", + "start":37,"end":41,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":6}}, + "extra": { + "rawValue": "8", + "raw": "\"\\8\"" + }, + "value": "8" + }, + { + "type": "StringLiteral", + "start":43,"end":47,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":12}}, + "extra": { + "rawValue": "9", + "raw": "\"\\9\"" + }, + "value": "9" + } + ] + } + } + ], + "directives": [ + { + "type": "Directive", + "start":21,"end":34,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":15}}, + "value": { + "type": "DirectiveLiteral", + "start":21,"end":33,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":14}}, + "value": "use strict", + "extra": { + "raw": "\"use strict\"", + "rawValue": "use strict" + } + } + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/503/input.js b/packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-directive/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/core/uncategorised/503/input.js rename to packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-directive/input.js diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/503/output.json b/packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-directive/output.json similarity index 97% rename from packages/babel-parser/test/fixtures/core/uncategorised/503/output.json rename to packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-directive/output.json index d559db4b5683..2c6c0e03d277 100644 --- a/packages/babel-parser/test/fixtures/core/uncategorised/503/output.json +++ b/packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-directive/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":76,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":76}}, "errors": [ - "SyntaxError: Legacy octal literals are not allowed in strict mode (1:69)" + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (1:69)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/501/input.js b/packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-property-name/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/core/uncategorised/501/input.js rename to packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-property-name/input.js diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/501/output.json b/packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-property-name/output.json similarity index 97% rename from packages/babel-parser/test/fixtures/core/uncategorised/501/output.json rename to packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-property-name/output.json index 2c29a7df14b9..0491546b1a61 100644 --- a/packages/babel-parser/test/fixtures/core/uncategorised/501/output.json +++ b/packages/babel-parser/test/fixtures/core/escape-string/numeric-escape-in-property-name/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":50,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":50}}, "errors": [ - "SyntaxError: Legacy octal literals are not allowed in strict mode (1:38)" + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (1:38)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/499/input.js b/packages/babel-parser/test/fixtures/core/escape-string/numeric-escape/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/core/uncategorised/499/input.js rename to packages/babel-parser/test/fixtures/core/escape-string/numeric-escape/input.js diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/499/output.json b/packages/babel-parser/test/fixtures/core/escape-string/numeric-escape/output.json similarity index 96% rename from packages/babel-parser/test/fixtures/core/uncategorised/499/output.json rename to packages/babel-parser/test/fixtures/core/escape-string/numeric-escape/output.json index a7ce0eaeba38..e9e573c73f5e 100644 --- a/packages/babel-parser/test/fixtures/core/uncategorised/499/output.json +++ b/packages/babel-parser/test/fixtures/core/escape-string/numeric-escape/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":40}}, "errors": [ - "SyntaxError: Legacy octal literals are not allowed in strict mode (1:35)" + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (1:35)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0217/output.json b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0217/output.json index a7ce0eaeba38..e9e573c73f5e 100644 --- a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0217/output.json +++ b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0217/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":40}}, "errors": [ - "SyntaxError: Legacy octal literals are not allowed in strict mode (1:35)" + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (1:35)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0219/output.json b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0219/output.json index 2c29a7df14b9..0491546b1a61 100644 --- a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0219/output.json +++ b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0219/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":50,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":50}}, "errors": [ - "SyntaxError: Legacy octal literals are not allowed in strict mode (1:38)" + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (1:38)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0223/output.json b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0223/output.json index d559db4b5683..2c6c0e03d277 100644 --- a/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0223/output.json +++ b/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0223/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":76,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":76}}, "errors": [ - "SyntaxError: Legacy octal literals are not allowed in strict mode (1:69)" + "SyntaxError: The only valid numeric escape in strict mode is '\\0' (1:69)" ], "program": { "type": "Program",