diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index b21f7dfc5165..7cd39dd4bc2e 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -65,7 +65,7 @@ const keywordRelationalOperator = /in(?:stanceof)?/y; * @param {*} tokens * @returns */ -function babel7CompatTokens(tokens) { +function babel7CompatTokens(tokens, input) { for (let i = 0; i < tokens.length; i++) { const token = tokens[i]; const { type } = token; @@ -106,7 +106,7 @@ function babel7CompatTokens(tokens) { const backquoteEnd = start + 1; const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1); let startToken; - if (value.charCodeAt(0) === charCodes.graveAccent) { + if (input.charCodeAt(start) === charCodes.graveAccent) { // $FlowIgnore: hacky way to create token startToken = new Token({ type: getExportedToken(tt.backQuote), @@ -135,7 +135,7 @@ function babel7CompatTokens(tokens) { // ends with '`' templateElementEnd = end - 1; templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1); - templateValue = value.slice(1, -1); + templateValue = value === null ? null : value.slice(1, -1); // $FlowIgnore: hacky way to create token endToken = new Token({ type: getExportedToken(tt.backQuote), @@ -149,7 +149,7 @@ function babel7CompatTokens(tokens) { // ends with `${` templateElementEnd = end - 2; templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2); - templateValue = value.slice(1, -2); + templateValue = value === null ? null : value.slice(1, -2); // $FlowIgnore: hacky way to create token endToken = new Token({ type: getExportedToken(tt.dollarBraceL), @@ -197,7 +197,9 @@ export default class StatementParser extends ExpressionParser { file.program = this.parseProgram(program); file.comments = this.state.comments; - if (this.options.tokens) file.tokens = babel7CompatTokens(this.tokens); + if (this.options.tokens) { + file.tokens = babel7CompatTokens(this.tokens, this.input); + } return this.finishNode(file, "File"); } diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 9c70fe7ab5ea..9a647a68775d 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -1382,7 +1382,7 @@ export default class Tokenizer extends ParserErrors { // Reads template string tokens. readTemplateToken(): void { let out = "", - chunkStart = this.state.pos, // eat '`' or `}` + chunkStart = this.state.pos, containsInvalid = false; ++this.state.pos; // eat '`' or `}` for (;;) { diff --git a/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-octal/input.js b/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-octal/input.js new file mode 100644 index 000000000000..4599ae5326a3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-octal/input.js @@ -0,0 +1,2 @@ +`\1`; +`\1${x}\2${y}\3`; diff --git a/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-octal/output.json b/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-octal/output.json new file mode 100644 index 000000000000..b39440c1505f --- /dev/null +++ b/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-octal/output.json @@ -0,0 +1,351 @@ +{ + "type": "File", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":17}}, + "errors": [ + "SyntaxError: Invalid escape sequence in template. (1:2)", + "SyntaxError: Invalid escape sequence in template. (2:2)", + "SyntaxError: Invalid escape sequence in template. (2:8)", + "SyntaxError: Invalid escape sequence in template. (2:14)" + ], + "program": { + "type": "Program", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":17}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "expression": { + "type": "TemplateLiteral", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "expressions": [], + "quasis": [ + { + "type": "TemplateElement", + "start":1,"end":3,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":3}}, + "value": { + "raw": "\\1", + "cooked": null + }, + "tail": true + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":6,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":17}}, + "expression": { + "type": "TemplateLiteral", + "start":6,"end":22,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":16}}, + "expressions": [ + { + "type": "Identifier", + "start":11,"end":12,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":6},"identifierName":"x"}, + "name": "x" + }, + { + "type": "Identifier", + "start":17,"end":18,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12},"identifierName":"y"}, + "name": "y" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start":7,"end":9,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":3}}, + "value": { + "raw": "\\1", + "cooked": null + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":13,"end":15,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":9}}, + "value": { + "raw": "\\2", + "cooked": null + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":19,"end":21,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":15}}, + "value": { + "raw": "\\3", + "cooked": null + }, + "tail": true + } + ] + } + } + ], + "directives": [] + }, + "tokens": [ + { + "type": { + "label": "`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "`", + "start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1}} + }, + { + "type": { + "label": "template", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": null, + "start":1,"end":3,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":3}} + }, + { + "type": { + "label": "`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "`", + "start":3,"end":4,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":4}} + }, + { + "type": { + "label": ";", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5}} + }, + { + "type": { + "label": "`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "`", + "start":6,"end":7,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":1}} + }, + { + "type": { + "label": "template", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": null, + "start":7,"end":9,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":3}} + }, + { + "type": { + "label": "${", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "${", + "start":9,"end":11,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":5}} + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": "x", + "start":11,"end":12,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":6}} + }, + { + "type": { + "label": "}", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "}", + "start":12,"end":13,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":7}} + }, + { + "type": { + "label": "template", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": null, + "start":13,"end":15,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":9}} + }, + { + "type": { + "label": "${", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "${", + "start":15,"end":17,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":11}} + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": "y", + "start":17,"end":18,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12}} + }, + { + "type": { + "label": "}", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "}", + "start":18,"end":19,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":13}} + }, + { + "type": { + "label": "template", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": null, + "start":19,"end":21,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":15}} + }, + { + "type": { + "label": "`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "`", + "start":21,"end":22,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":16}} + }, + { + "type": { + "label": ";", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start":22,"end":23,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":17}} + }, + { + "type": { + "label": "eof", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start":23,"end":23,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":17}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-unicode-escape-codepoint/input.js b/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-unicode-escape-codepoint/input.js new file mode 100644 index 000000000000..1ef2bb8552f8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-unicode-escape-codepoint/input.js @@ -0,0 +1,2 @@ +`\u{g}`; +`\u{g}${x}\u{g}${y}\u{g}`; diff --git a/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-unicode-escape-codepoint/output.json b/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-unicode-escape-codepoint/output.json new file mode 100644 index 000000000000..0c4984332c77 --- /dev/null +++ b/packages/babel-parser/test/fixtures/tokens/template-string-babel-7/invalid-unicode-escape-codepoint/output.json @@ -0,0 +1,351 @@ +{ + "type": "File", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":26}}, + "errors": [ + "SyntaxError: Invalid escape sequence in template. (1:2)", + "SyntaxError: Invalid escape sequence in template. (2:2)", + "SyntaxError: Invalid escape sequence in template. (2:11)", + "SyntaxError: Invalid escape sequence in template. (2:20)" + ], + "program": { + "type": "Program", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":26}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "expression": { + "type": "TemplateLiteral", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "expressions": [], + "quasis": [ + { + "type": "TemplateElement", + "start":1,"end":6,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":6}}, + "value": { + "raw": "\\u{g}", + "cooked": null + }, + "tail": true + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":9,"end":35,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":26}}, + "expression": { + "type": "TemplateLiteral", + "start":9,"end":34,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":25}}, + "expressions": [ + { + "type": "Identifier", + "start":17,"end":18,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9},"identifierName":"x"}, + "name": "x" + }, + { + "type": "Identifier", + "start":26,"end":27,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":18},"identifierName":"y"}, + "name": "y" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start":10,"end":15,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":6}}, + "value": { + "raw": "\\u{g}", + "cooked": null + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":19,"end":24,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":15}}, + "value": { + "raw": "\\u{g}", + "cooked": null + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":28,"end":33,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":24}}, + "value": { + "raw": "\\u{g}", + "cooked": null + }, + "tail": true + } + ] + } + } + ], + "directives": [] + }, + "tokens": [ + { + "type": { + "label": "`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "`", + "start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1}} + }, + { + "type": { + "label": "template", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": null, + "start":1,"end":6,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":6}} + }, + { + "type": { + "label": "`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "`", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}} + }, + { + "type": { + "label": ";", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8}} + }, + { + "type": { + "label": "`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "`", + "start":9,"end":10,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":1}} + }, + { + "type": { + "label": "template", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": null, + "start":10,"end":15,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":6}} + }, + { + "type": { + "label": "${", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "${", + "start":15,"end":17,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":8}} + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": "x", + "start":17,"end":18,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9}} + }, + { + "type": { + "label": "}", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "}", + "start":18,"end":19,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":10}} + }, + { + "type": { + "label": "template", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": null, + "start":19,"end":24,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":15}} + }, + { + "type": { + "label": "${", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "${", + "start":24,"end":26,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":17}} + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": "y", + "start":26,"end":27,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":18}} + }, + { + "type": { + "label": "}", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "}", + "start":27,"end":28,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":19}} + }, + { + "type": { + "label": "template", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": null, + "start":28,"end":33,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":24}} + }, + { + "type": { + "label": "`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "`", + "start":33,"end":34,"loc":{"start":{"line":2,"column":24},"end":{"line":2,"column":25}} + }, + { + "type": { + "label": ";", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start":34,"end":35,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":26}} + }, + { + "type": { + "label": "eof", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start":35,"end":35,"loc":{"start":{"line":2,"column":26},"end":{"line":2,"column":26}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/tokens/template-string/invalid-octal/input.js b/packages/babel-parser/test/fixtures/tokens/template-string/invalid-octal/input.js new file mode 100644 index 000000000000..4599ae5326a3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/tokens/template-string/invalid-octal/input.js @@ -0,0 +1,2 @@ +`\1`; +`\1${x}\2${y}\3`; diff --git a/packages/babel-parser/test/fixtures/tokens/template-string/invalid-octal/output.json b/packages/babel-parser/test/fixtures/tokens/template-string/invalid-octal/output.json new file mode 100644 index 000000000000..3fcbbf57dfb9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/tokens/template-string/invalid-octal/output.json @@ -0,0 +1,222 @@ +{ + "type": "File", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":17}}, + "errors": [ + "SyntaxError: Invalid escape sequence in template. (1:2)", + "SyntaxError: Invalid escape sequence in template. (2:2)", + "SyntaxError: Invalid escape sequence in template. (2:8)", + "SyntaxError: Invalid escape sequence in template. (2:14)" + ], + "program": { + "type": "Program", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":17}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "expression": { + "type": "TemplateLiteral", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "expressions": [], + "quasis": [ + { + "type": "TemplateElement", + "start":1,"end":3,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":3}}, + "value": { + "raw": "\\1", + "cooked": null + }, + "tail": true + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":6,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":17}}, + "expression": { + "type": "TemplateLiteral", + "start":6,"end":22,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":16}}, + "expressions": [ + { + "type": "Identifier", + "start":11,"end":12,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":6},"identifierName":"x"}, + "name": "x" + }, + { + "type": "Identifier", + "start":17,"end":18,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12},"identifierName":"y"}, + "name": "y" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start":7,"end":9,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":3}}, + "value": { + "raw": "\\1", + "cooked": null + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":13,"end":15,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":9}}, + "value": { + "raw": "\\2", + "cooked": null + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":19,"end":21,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":15}}, + "value": { + "raw": "\\3", + "cooked": null + }, + "tail": true + } + ] + } + } + ], + "directives": [] + }, + "tokens": [ + { + "type": { + "label": "...`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": null, + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}} + }, + { + "type": { + "label": ";", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5}} + }, + { + "type": { + "label": "...${", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": null, + "start":6,"end":11,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":5}} + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "x", + "start":11,"end":12,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":6}} + }, + { + "type": { + "label": "...${", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": null, + "start":12,"end":17,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":11}} + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "y", + "start":17,"end":18,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12}} + }, + { + "type": { + "label": "...`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": null, + "start":18,"end":22,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}} + }, + { + "type": { + "label": ";", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start":22,"end":23,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":17}} + }, + { + "type": { + "label": "eof", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start":23,"end":23,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":17}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/tokens/template-string/invalid-unicode-escape-codepoint/input.js b/packages/babel-parser/test/fixtures/tokens/template-string/invalid-unicode-escape-codepoint/input.js new file mode 100644 index 000000000000..1ef2bb8552f8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/tokens/template-string/invalid-unicode-escape-codepoint/input.js @@ -0,0 +1,2 @@ +`\u{g}`; +`\u{g}${x}\u{g}${y}\u{g}`; diff --git a/packages/babel-parser/test/fixtures/tokens/template-string/invalid-unicode-escape-codepoint/output.json b/packages/babel-parser/test/fixtures/tokens/template-string/invalid-unicode-escape-codepoint/output.json new file mode 100644 index 000000000000..050b0345541e --- /dev/null +++ b/packages/babel-parser/test/fixtures/tokens/template-string/invalid-unicode-escape-codepoint/output.json @@ -0,0 +1,222 @@ +{ + "type": "File", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":26}}, + "errors": [ + "SyntaxError: Invalid escape sequence in template. (1:2)", + "SyntaxError: Invalid escape sequence in template. (2:2)", + "SyntaxError: Invalid escape sequence in template. (2:11)", + "SyntaxError: Invalid escape sequence in template. (2:20)" + ], + "program": { + "type": "Program", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":26}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "expression": { + "type": "TemplateLiteral", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "expressions": [], + "quasis": [ + { + "type": "TemplateElement", + "start":1,"end":6,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":6}}, + "value": { + "raw": "\\u{g}", + "cooked": null + }, + "tail": true + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":9,"end":35,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":26}}, + "expression": { + "type": "TemplateLiteral", + "start":9,"end":34,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":25}}, + "expressions": [ + { + "type": "Identifier", + "start":17,"end":18,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9},"identifierName":"x"}, + "name": "x" + }, + { + "type": "Identifier", + "start":26,"end":27,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":18},"identifierName":"y"}, + "name": "y" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start":10,"end":15,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":6}}, + "value": { + "raw": "\\u{g}", + "cooked": null + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":19,"end":24,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":15}}, + "value": { + "raw": "\\u{g}", + "cooked": null + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":28,"end":33,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":24}}, + "value": { + "raw": "\\u{g}", + "cooked": null + }, + "tail": true + } + ] + } + } + ], + "directives": [] + }, + "tokens": [ + { + "type": { + "label": "...`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": null, + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + }, + { + "type": { + "label": ";", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8}} + }, + { + "type": { + "label": "...${", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": null, + "start":9,"end":17,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":8}} + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "x", + "start":17,"end":18,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9}} + }, + { + "type": { + "label": "...${", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": null, + "start":18,"end":26,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":17}} + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "y", + "start":26,"end":27,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":18}} + }, + { + "type": { + "label": "...`", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": null, + "start":27,"end":34,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":25}} + }, + { + "type": { + "label": ";", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start":34,"end":35,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":26}} + }, + { + "type": { + "label": "eof", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start":35,"end":35,"loc":{"start":{"line":2,"column":26},"end":{"line":2,"column":26}} + } + ] +} \ No newline at end of file