Skip to content

Commit

Permalink
Merge pull request #253 from petermuessig/master
Browse files Browse the repository at this point in the history
fix: support member exp before tagged template (String.raw)
  • Loading branch information
6utt3rfly committed Jul 19, 2023
2 parents bcb3c34 + 92e6de0 commit af79e53
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/template/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default {
jsep.hooks.add('gobble-token', gobbleTemplateLiteral);

jsep.hooks.add('after-token', function gobbleTaggedTemplateIdentifier(env) {
if (env.node.type === jsep.IDENTIFIER && this.code === BTICK_CODE) {
if ((env.node.type === jsep.IDENTIFIER || env.node.type === jsep.MEMBER_EXP) && this.code === BTICK_CODE) {
env.node = {
type: TAGGED_TEMPLATE_EXPRESSION,
tag: env.node,
Expand Down
87 changes: 87 additions & 0 deletions packages/template/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,93 @@ const { test } = QUnit;
}, assert);
});

test('should parse String.raw tagged, nested template literal expression', (assert) => {
testParser('String.raw`token ${`nested ${`deeply` + "str"} blah`}`', {
type: 'TaggedTemplateExpression',
tag: {
type: "MemberExpression",
computed: false,
object: {
type: "Identifier",
name: "String"
},
property: {
type: "Identifier",
name:"raw"
}
},
quasi: {
type: 'TemplateLiteral',
quasis: [
{
type: 'TemplateElement',
value: {
raw: 'token ',
cooked: 'token ',
},
tail: false,
},
{
type: 'TemplateElement',
value: {
raw: '',
cooked: '',
},
tail: true,
},
],
expressions: [
{
type: 'TemplateLiteral',
quasis: [
{
type: 'TemplateElement',
value: {
raw: 'nested ',
cooked: 'nested ',
},
tail: false,
},
{
type: 'TemplateElement',
value: {
raw: ' blah',
cooked: ' blah',
},
tail: true,
},
],
expressions: [
{
type: 'BinaryExpression',
operator: '+',
left: {
type: 'TemplateLiteral',
quasis: [
{
type: 'TemplateElement',
value: {
raw: 'deeply',
cooked: 'deeply',
},
tail: true,
},
],
expressions: [],
},
right: {
type: 'Literal',
value: 'str',
raw: '"str"',
},
},
],
},
],
},
}, assert);
});

test('should parse multiple vars within template literal expression', (assert) => {
testParser('`hi ${last}, ${first} ${middle}!`', {
type: 'TemplateLiteral',
Expand Down

0 comments on commit af79e53

Please sign in to comment.