Skip to content

Commit

Permalink
fix: disallow \8, \9 in strict mode string (#11852)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 21, 2020
1 parent 3680f01 commit 2bf38fb
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -1357,6 +1357,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:
Expand Down Expand Up @@ -1385,7 +1387,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
Expand Down
Expand Up @@ -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)"
Expand Down
Expand Up @@ -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)",
Expand Down
@@ -0,0 +1,5 @@
"\8","\9";
() => {
"use strict";
"\8", "\9";
}
@@ -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": []
}
}
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down

0 comments on commit 2bf38fb

Please sign in to comment.