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: support member exp after template and tagged template #260

Merged
merged 1 commit into from
Mar 27, 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
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
Loading