Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix template strings with octal or hex values #255

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/parser.ts
Expand Up @@ -3929,7 +3929,7 @@ export function parseMemberOrUpdateExpression(

parser.assignable = AssignmentKind.Assignable;

const property = parsePropertyOrPrivatePropertyName(parser, context);
const property = parsePropertyOrPrivatePropertyName(parser, context | Context.TaggedTemplate);

expr = finishNode(parser, context, start, line, column, {
type: 'MemberExpression',
Expand Down Expand Up @@ -4287,7 +4287,7 @@ export function parsePrimaryExpression(
case Token.LeftParen:
return parseParenthesizedExpression(
parser,
context,
context | Context.TaggedTemplate,
canAssign,
BindingKind.ArgumentList,
Origin.None,
Expand Down Expand Up @@ -4663,7 +4663,7 @@ export function parseArguments(
const args: (ESTree.Expression | ESTree.SpreadElement)[] = [];

if (parser.token === Token.RightParen) {
nextToken(parser, context);
nextToken(parser, context | Context.TaggedTemplate);
return args;
}

Expand Down
138 changes: 137 additions & 1 deletion test/parser/expressions/template.ts
Expand Up @@ -338,7 +338,15 @@ describe('Expressions - Template', () => {
'test`\\18`;',
'(`\n`)',
'(`\r`)',
'new nestedNewOperatorFunction`1``2``3``array`'
'new nestedNewOperatorFunction`1``2``3``array`',
"tag()`'\\00a0'`;",
"(tag = () => {})`'\\00a0'`",
"(() => {})`'\\00a0'`",
"(function tag() { return () => {}; })()`'\\00a0'`",
"(function() { return () => {}; })()`'\\00a0'`",
"(function tag() {})`'\\00a0'`",
"(function() {})`'\\00a0'`",
'String.raw`{\rtf1adeflang1025ansiansicpg1252\\uc1`;'
]) {
it(`${arg}`, () => {
t.doesNotThrow(() => {
Expand Down Expand Up @@ -4033,6 +4041,134 @@ describe('Expressions - Template', () => {
}
}
}
],
[
"tag()`'\\00a0'`;",
Context.None,
{
body: [
{
expression: {
quasi: {
expressions: [],
quasis: [
{
tail: true,
type: 'TemplateElement',
value: {
cooked: undefined,
raw: "'\\00a0'"
}
}
],
type: 'TemplateLiteral'
},
tag: {
arguments: [],
callee: {
name: 'tag',
type: 'Identifier'
},
type: 'CallExpression'
},
type: 'TaggedTemplateExpression'
},
type: 'ExpressionStatement'
}
],
sourceType: 'script',
type: 'Program'
}
],
[
"(tag = () => {})`'\\00a0'`",
Context.None,
{
body: [
{
expression: {
quasi: {
expressions: [],
quasis: [
{
tail: true,
type: 'TemplateElement',
value: {
cooked: undefined,
raw: "'\\00a0'"
}
}
],
type: 'TemplateLiteral'
},
tag: {
left: {
name: 'tag',
type: 'Identifier'
},
operator: '=',
right: {
async: false,
body: {
body: [],
type: 'BlockStatement'
},
expression: false,
params: [],
type: 'ArrowFunctionExpression'
},
type: 'AssignmentExpression'
},
type: 'TaggedTemplateExpression'
},
type: 'ExpressionStatement'
}
],
sourceType: 'script',
type: 'Program'
}
],
[
'String.raw`{\rtf1adeflang1025ansiansicpg1252\\uc1`;',
Context.None,
{
body: [
{
expression: {
quasi: {
expressions: [],
quasis: [
{
tail: true,
type: 'TemplateElement',
value: {
cooked: undefined,
raw: '{\rtf1adeflang1025ansiansicpg1252\\uc1'
}
}
],
type: 'TemplateLiteral'
},
tag: {
computed: false,
object: {
name: 'String',
type: 'Identifier'
},
property: {
name: 'raw',
type: 'Identifier'
},
type: 'MemberExpression'
},
type: 'TaggedTemplateExpression'
},
type: 'ExpressionStatement'
}
],
sourceType: 'script',
type: 'Program'
}
]
]);
});