Skip to content

Commit

Permalink
Transformer code review feedback: inline string literals & update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewJakubowicz committed Dec 6, 2021
1 parent 8413ed3 commit c3d0506
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,27 @@ export class QueryAssignedElementsVisitor implements MemberDecoratorVisitor {
}

/**
* Returns a template string which resolves the passed in slot name
* expression.
*
* Special handling is included for string literals and no substitution
* template literals. In this case we inline the slot name into the selector
* to match what is more likely to have been authored.
*
* @param slot Expression that evaluates to the slot name.
* @returns Template string node representing `slot[name=${slot}]`
* @returns Template string node representing `slot[name=${slot}]` except when
* `slot` is a string literal. Then the literal is inlined. I.e. for a slot
* expression of `"list"`, return `slot[name=list]`.
*/
private createNamedSlotSelector(slot: ts.Expression): ts.TemplateExpression {
private createNamedSlotSelector(slot: ts.Expression) {
const factory = this._factory;
if (ts.isStringLiteral(slot) || ts.isNoSubstitutionTemplateLiteral(slot)) {
const inlinedSlotSelector = `slot[name=${slot.text}]`;
return this._factory.createNoSubstitutionTemplateLiteral(
inlinedSlotSelector,
inlinedSlotSelector
);
}
return factory.createTemplateExpression(
factory.createTemplateHead('slot[name=', 'slot[name='),
[factory.createTemplateSpan(slot, factory.createTemplateTail(']', ']'))]
Expand Down
10 changes: 5 additions & 5 deletions packages/ts-transformers/src/tests/idiomatic-decorators-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ const tests = (test: uvu.Test<uvu.Context>, options: ts.CompilerOptions) => {
// listItems comment
get listItems() {
return this.renderRoot
?.querySelector(\`slot[name=\${"list"}]\`)
?.querySelector(\`slot[name=list]\`)
?.assignedElements() ?? [];
}
}
Expand All @@ -632,7 +632,7 @@ const tests = (test: uvu.Test<uvu.Context>, options: ts.CompilerOptions) => {
class MyElement extends LitElement {
// listItems comment
@queryAssignedElements({ slot: 'list', flatten: true })
@queryAssignedElements({ slot: \`list\`, flatten: true })
listItems: NodeListOf<HTMLElement>;
}
`;
Expand All @@ -644,7 +644,7 @@ const tests = (test: uvu.Test<uvu.Context>, options: ts.CompilerOptions) => {
// listItems comment
get listItems() {
return this.renderRoot
?.querySelector(\`slot[name=\${"list"}]\`)
?.querySelector(\`slot[name=list]\`)
?.assignedElements({flatten: true}) ?? [];
}
}
Expand All @@ -671,7 +671,7 @@ const tests = (test: uvu.Test<uvu.Context>, options: ts.CompilerOptions) => {
// listItems comment
get listItems() {
return this.renderRoot
?.querySelector(\`slot[name=\${"list"}]\`)
?.querySelector(\`slot[name=list]\`)
?.assignedElements({ flatten: false })
?.filter((node) => node.matches('.item')
) ?? [];
Expand Down Expand Up @@ -706,7 +706,7 @@ const tests = (test: uvu.Test<uvu.Context>, options: ts.CompilerOptions) => {
// listItems comment
get listItems() {
return this.renderRoot
?.querySelector(\`slot[name=\${"list"}]\`)
?.querySelector(\`slot[name=list]\`)
?.assignedElements({ flatten: isFlatten })
?.filter((node) => node.matches(".item")
) ?? [];
Expand Down

0 comments on commit c3d0506

Please sign in to comment.