Skip to content

Commit

Permalink
fix(parser): template strings with octal or hex values
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Apr 3, 2024
2 parents e1dc66b + 9af2530 commit c518664
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 4 deletions.
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'
}
]
]);
});

0 comments on commit c518664

Please sign in to comment.