Skip to content

Commit

Permalink
Merge pull request #260 from anpete/master
Browse files Browse the repository at this point in the history
fix: support member exp after template and tagged template
  • Loading branch information
6utt3rfly authored Mar 27, 2024
2 parents 29049a0 + 69dc0b1 commit c8069bf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/template/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
name: 'jsepTemplateLiteral',

init(jsep) {
function gobbleTemplateLiteral(env) {
function gobbleTemplateLiteral(env, gobbleMember = true) {
if (this.code === BTICK_CODE) {
const node = {
type: TEMPLATE_LITERAL,
Expand Down Expand Up @@ -37,7 +37,13 @@ export default {
pushQuasi();

env.node = node;
return node;

if (gobbleMember) {
// allow . [] and () after template: `foo`.length
env.node = this.gobbleTokenProperty(env.node);
}

return env.node;
}
else if (ch === '$' && this.expr.charAt(this.index + 1) === '{') {
this.index += 2;
Expand Down Expand Up @@ -81,8 +87,13 @@ export default {
env.node = {
type: TAGGED_TEMPLATE_EXPRESSION,
tag: env.node,
quasi: gobbleTemplateLiteral.bind(this)(env),
quasi: gobbleTemplateLiteral.bind(this)(env, false),
};

// allow . [] and () after tagged template: bar`foo`.length
env.node = this.gobbleTokenProperty(env.node);

return env.node;
}
});
}
Expand Down
65 changes: 65 additions & 0 deletions packages/template/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,71 @@ const { test } = QUnit;
}, assert);
});

test('should parse template literal member access', (assert) => {
testParser('`foo`.bar', {
type: 'MemberExpression',
computed: false,
object: {
type: 'TemplateLiteral',
quasis: [
{
type: 'TemplateElement',
value: {
raw: 'foo',
cooked: 'foo',
},
tail: true,
}
],
expressions: []
},
property: {
type: 'Identifier',
name: 'bar'
}
}, assert);
});

test('should parse tagged template literal member access', (assert) => {
testParser('String.raw`foo`.bar', {
type: "MemberExpression",
computed: false,
object: {
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: "foo",
cooked: "foo"
},
tail: true
}
],
expressions: []
}
},
property: {
type: "Identifier",
name: "bar"
}
}, assert);
});

test('should parse tagged, nested template literal expression', (assert) => {
testParser('abc`token ${`nested ${`deeply` + "str"} blah`}`', {
type: 'TaggedTemplateExpression',
Expand Down

0 comments on commit c8069bf

Please sign in to comment.