Skip to content

Commit d10cdb0

Browse files
fix: rendering slots with non-literal names (#5003)
* fix: rendering slot when name is not a literal * fix: remove expected failure * fix: missing fixture exclusion
1 parent ee2edae commit d10cdb0

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

packages/@lwc/ssr-compiler/src/__tests__/utils/expected-failures.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const expectedFailures = new Set([
2121
'render-dynamic-value/index.js',
2222
'scoped-slots/advanced/index.js',
2323
'scoped-slots/default-slot/index.js',
24-
'scoped-slots/expression/index.js',
2524
'scoped-slots/mixed-with-light-dom-slots-inside/index.js',
2625
'scoped-slots/mixed-with-light-dom-slots-outside/index.js',
2726
'slot-forwarding/slots/mixed/index.js',

packages/@lwc/ssr-compiler/src/compile-template/shared.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import type {
2323
Property as EsProperty,
2424
Statement as EsStatement,
2525
} from 'estree';
26+
import type {
27+
ComplexExpression as IrComplexExpression,
28+
Expression as IrExpression,
29+
Literal as IrLiteral,
30+
} from '@lwc/template-compiler';
2631

2732
export function optimizeAdjacentYieldStmts(statements: EsStatement[]): EsStatement[] {
2833
let prevStmt: EsStatement | null = null;
@@ -172,3 +177,11 @@ export function getChildAttrsOrProps(
172177

173178
return b.objectExpression(objectAttrsOrProps);
174179
}
180+
181+
/**
182+
* Determine if the provided node is of type Literal
183+
* @param node
184+
*/
185+
export function isLiteral(node: IrLiteral | IrExpression | IrComplexExpression): node is IrLiteral {
186+
return node.type === 'Literal';
187+
}

packages/@lwc/ssr-compiler/src/compile-template/transformers/component/slotted-content.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { builders as b, is } from 'estree-toolkit';
1010
import { bAttributeValue, optimizeAdjacentYieldStmts } from '../../shared';
1111
import { esTemplate, esTemplateWithYield } from '../../../estemplate';
1212
import { irChildrenToEs, irToEs } from '../../ir-to-es';
13+
import { isLiteral } from '../../shared';
14+
import { expressionIrToEs } from '../../expression';
1315
import { isNullableOf } from '../../../estree/validators';
1416
import type { CallExpression as EsCallExpression, Expression as EsExpression } from 'estree';
1517

@@ -186,13 +188,14 @@ export function getSlottedContent(
186188
const boundVariableName = child.slotData.value.name;
187189
const boundVariable = b.identifier(boundVariableName);
188190
cxt.pushLocalVars([boundVariableName]);
191+
192+
const slotName = isLiteral(child.slotName)
193+
? b.literal(child.slotName.value)
194+
: expressionIrToEs(child.slotName, cxt);
195+
189196
// TODO [#4768]: what if the bound variable is `generateMarkup` or some framework-specific identifier?
190197
const addLightContentExpr = b.expressionStatement(
191-
bAddLightContent(
192-
child.slotName as EsExpression,
193-
boundVariable,
194-
irChildrenToEs(child.children, cxt)
195-
)
198+
bAddLightContent(slotName, boundVariable, irChildrenToEs(child.children, cxt))
196199
);
197200
cxt.popLocalVars();
198201
return addLightContentExpr;

packages/@lwc/ssr-compiler/src/compile-template/transformers/text.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,21 @@
88
import { builders as b, is } from 'estree-toolkit';
99
import { esTemplateWithYield } from '../../estemplate';
1010
import { expressionIrToEs } from '../expression';
11+
import { isLiteral } from '../shared';
1112

1213
import { bYieldTextContent, isLastConcatenatedNode } from '../adjacent-text-nodes';
1314
import type {
1415
Statement as EsStatement,
1516
ExpressionStatement as EsExpressionStatement,
1617
} from 'estree';
17-
import type {
18-
ComplexExpression as IrComplexExpression,
19-
Expression as IrExpression,
20-
Literal as IrLiteral,
21-
Text as IrText,
22-
} from '@lwc/template-compiler';
18+
import type { Text as IrText } from '@lwc/template-compiler';
2319
import type { Transformer } from '../types';
2420

2521
const bBufferTextContent = esTemplateWithYield`
2622
didBufferTextContent = true;
2723
textContentBuffer += massageTextContent(${/* string value */ is.expression});
2824
`<EsExpressionStatement[]>;
2925

30-
function isLiteral(node: IrLiteral | IrExpression | IrComplexExpression): node is IrLiteral {
31-
return node.type === 'Literal';
32-
}
33-
3426
export const Text: Transformer<IrText> = function Text(node, cxt): EsStatement[] {
3527
cxt.import(['htmlEscape', 'massageTextContent']);
3628

0 commit comments

Comments
 (0)