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

Handle correctly octal or hex values located in template strings #29

Merged
merged 3 commits into from
Apr 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dist/meriyah.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6314,7 +6314,7 @@
case 67108877: {
nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
parser.assignable = 1;
const property = parsePropertyOrPrivatePropertyName(parser, context);
const property = parsePropertyOrPrivatePropertyName(parser, context | 65536);
expr = finishNode(parser, context, start, line, column, {
type: 'MemberExpression',
object: expr,
Expand Down Expand Up @@ -6534,7 +6534,7 @@
case 67174411:
const leadingComment = collectLeadingComments(parser);
parser.leadingComments && parser.leadingComments.pop();
const exprNode = parseParenthesizedExpression(parser, context, canAssign, 1, 0, start, line, column);
const exprNode = parseParenthesizedExpression(parser, context | 65536, canAssign, 1, 0, start, line, column);
if (exprNode.leadingComment && leadingComment) {
exprNode.leadingComments.concat(leadingComment);
}
Expand Down Expand Up @@ -6711,7 +6711,7 @@
nextToken(parser, context | 32768);
const args = [];
if (parser.token === 16) {
nextToken(parser, context);
nextToken(parser, context | 65536);
return args;
}
while (parser.token !== 16) {
Expand Down
6 changes: 3 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4043,7 +4043,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 @@ -4407,7 +4407,7 @@ export function parsePrimaryExpression(
parser.leadingComments && parser.leadingComments.pop();
const exprNode = parseParenthesizedExpression(
parser,
context,
context | Context.TaggedTemplate,
canAssign,
BindingKind.ArgumentList,
Origin.None,
Expand Down Expand Up @@ -4802,7 +4802,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
Original file line number Diff line number Diff line change
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`;'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the case from the ticket is still not working.
Tested on https://meriyah.github.io/meriyah/

String.raw`{\rtf1\adeflang1025\ansi\ansicpg1252\uc1`;

// > [1:49]: Invalid hexadecimal escape sequence

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually none of my examples are working in this app and I believe it's because that my fix is available only in version v4.4.2. The version of https://meriyah.github.io/meriyah/ is v4.3.4
Screenshot 2024-04-04 at 11 31 28

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, online version of the parser is outdated.

But the test cases are actually working in old versions, that caused the confusion:

// string from the tests - parsed before the fix
String.raw`{\rtf1adeflang1025ansiansicpg1252\\uc1`;

// string from the ticket - only works after the fix
String.raw`{\rtf1\adeflang1025\ansi\ansicpg1252\uc1`;

Can we add second string to the tests?

Copy link
Contributor Author

@ypapouski ypapouski Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I see your confusion and it's tricky indeed! I must have added an additional '\' because otherwise a js string can't be created:
Screenshot 2024-04-04 at 18 47 16

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not as a regular string, but String.raw actually accepts it.
image

]) {
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'
}
]
]);
});
Loading