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(ssr-compiler): define setters for reflected attributes #4611

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 37 additions & 15 deletions packages/@lwc/ssr-compiler/src/compile-js/generate-markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,43 @@ function bReflectedAttrsObj(reflectedPropNames: (keyof typeof AriaPropNameToAttr
//
// The props object will be kept up-to-date with any new values set on the corresponding
// property name in the component instance.
const reflectedAttrGetters: Property[] = reflectedPropNames.map((propName) =>
b.property(
'get',
b.literal(AriaPropNameToAttrNameMap[propName]),
b.functionExpression(
null,
[],
b.blockStatement([
b.returnStatement(
b.memberExpression(b.identifier('props'), b.identifier(propName))
),
])
const reflectedAttrAccessors: Property[] = [];
for (const propName of reflectedPropNames) {
reflectedAttrAccessors.push(
b.property(
'get',
b.literal(AriaPropNameToAttrNameMap[propName]),
b.functionExpression(
null,
[],
b.blockStatement([
b.returnStatement(
b.callExpression(b.identifier('String'), [
b.memberExpression(b.identifier('props'), b.identifier(propName)),
])
),
])
)
),
b.property(
'set',
b.literal(AriaPropNameToAttrNameMap[propName]),
b.functionExpression(
null,
[b.identifier('val')],
b.blockStatement([
b.expressionStatement(
b.assignmentExpression(
'=',
b.memberExpression(b.identifier('props'), b.identifier(propName)),
b.identifier('val')
)
),
])
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would benefit from a code comment showing an example output. ASTs are hard to read...

)
)
);
);
}

// This mutates the `attrs` object, adding the reflected aria attributes that have been
// detected. Example:
Expand All @@ -87,7 +109,7 @@ function bReflectedAttrsObj(reflectedPropNames: (keyof typeof AriaPropNameToAttr
b.assignmentExpression(
'=',
b.identifier('attrs'),
b.objectExpression([b.spreadElement(b.identifier('attrs')), ...reflectedAttrGetters])
b.objectExpression([b.spreadElement(b.identifier('attrs')), ...reflectedAttrAccessors])
)
);
}
Expand Down
34 changes: 22 additions & 12 deletions packages/@lwc/ssr-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,29 @@ export class LightningElement implements PropsAvailableAtConstruction {
return (this.__classList = new ClassList(this));
}

getAttribute(attrName: string): string | null {
return this.__attrs[attrName] ?? null;
#setAttribute(attrName: string, attrValue: string | null): void {
ekashida marked this conversation as resolved.
Show resolved Hide resolved
this.__attrs[attrName] = attrValue;
}

setAttribute(attrName: string, value: string | null): void {
this.__attrs[attrName] = String(value);
setAttribute(attrName: string, attrValue: unknown): void {
this.#setAttribute(attrName, String(attrValue));
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a bit confusing to have two methods named setAttribute and #setAttribute. Maybe #setAttributeWithoutTypeCoercion or something?

}

hasAttribute(attrName: string): boolean {
return Boolean(this.__attrs && attrName in this.__attrs);
getAttribute(attrName: unknown): string | null {
if (this.hasAttribute(attrName)) {
return this.__attrs[attrName as string];
}
return null;
}

hasAttribute(attrName: unknown): boolean {
return typeof attrName === 'string' && typeof this.__attrs[attrName] === 'string';
Copy link
Collaborator

Choose a reason for hiding this comment

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

The fact that we have to check for strings here because we don't ensure that the values in this.__attrs is only a string type is odd to me. We should probably do the validation/coercion on the way in.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, so the reason for this is that we can't call delete because that would delete the getter/setter which is required for prop/attr reflection.

Copy link
Collaborator

Choose a reason for hiding this comment

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

}

removeAttribute(attrName: string): void {
this.__attrs[attrName] = null;
if (this.hasAttribute(attrName)) {
this.#setAttribute(attrName, null);
ekashida marked this conversation as resolved.
Show resolved Hide resolved
}
}

addEventListener(
Expand Down Expand Up @@ -299,11 +308,12 @@ export function* renderAttrs(attrs: Attributes) {
if (!attrs) {
return;
}
for (const [key, val] of Object.entries(attrs)) {
if (typeof val === 'string') {
yield val === '' ? ` ${key}` : ` ${key}="${escapeAttrVal(val)}"`;
} else if (val === null) {
return '';
for (const attrName in attrs) {
ekashida marked this conversation as resolved.
Show resolved Hide resolved
const attrVal = attrs[attrName];
if (typeof attrVal === 'string') {
yield attrVal === '' ? ` ${attrName}` : ` ${attrName}="${escapeAttrVal(attrVal)}"`;
} else if (attrVal === null) {
yield '';
}
}
}
Expand Down
Loading