Skip to content

Commit

Permalink
fix: bring back text.ts temporarily
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed Dec 20, 2024
1 parent 4ab455b commit 36c2e11
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export const expectedFailures = new Set([
'scoped-slots/mixed-with-light-dom-slots-outside/index.js',
'slot-forwarding/slots/mixed/index.js',
'slot-forwarding/slots/dangling/index.js',
// 'slot-not-at-top-level/with-adjacent-text-nodes/lwcIf-as-sibling/light/index.js',
// 'slot-not-at-top-level/with-adjacent-text-nodes/lwcIf/light/index.js',
// 'slot-not-at-top-level/with-adjacent-text-nodes/if/light/index.js',
// 'slot-not-at-top-level/with-adjacent-text-nodes/if-as-sibling/light/index.js',
'slot-not-at-top-level/with-adjacent-text-nodes/lwcIf-as-sibling/light/index.js',
'slot-not-at-top-level/with-adjacent-text-nodes/lwcIf/light/index.js',
'slot-not-at-top-level/with-adjacent-text-nodes/if/light/index.js',
'slot-not-at-top-level/with-adjacent-text-nodes/if-as-sibling/light/index.js',
'wire/errors/throws-on-computed-key/index.js',
'wire/errors/throws-when-colliding-prop-then-method/index.js',
]);
35 changes: 15 additions & 20 deletions packages/@lwc/ssr-compiler/src/compile-template/ir-to-es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { createNewContext } from './context';
import { IfBlock } from './transformers/lwcIf';
import { isLiteral } from './shared';
import { expressionIrToEs } from './expression';
import { Text } from './transformers/text';
import type {
ChildNode as IrChildNode,
Comment as IrComment,
Expand Down Expand Up @@ -66,8 +67,7 @@ const transformers: Transformers = {
If: LegacyIf,
IfBlock,
Root,
// Text nodes are always handled by the parent to do adjacent text concatination
Text: defaultTransformer,
Text,
// lwc:elseif cannot exist without an lwc:if (IfBlock); this gets handled by that transformer
ElseifBlock: defaultTransformer,
// lwc:elseif cannot exist without an lwc:elseif (IfBlock); this gets handled by that transformer
Expand Down Expand Up @@ -95,24 +95,19 @@ const isConcatenatableTextNode = (
child.type === 'Text' || (child.type === 'Comment' && !cxt.templateOptions.preserveComments);

const bMassageTextContent = esTemplate`
// We are at the end of a series of text nodes - flush to a concatenated string
// We only render the ZWJ if there were actually any dynamic text nodes rendered
// The ZWJ is just so hydration can compare the SSR'd dynamic text content against
// the CSR'd text content.
massageTextContent(${/* string value */ is.expression});
`<EsCallExpression>;
massageTextContent(${/* string value */ is.expression});
`<EsCallExpression>;

const bYieldTextContent = esTemplateWithYield`
// We are at the end of a series of text nodes - flush to a concatenated string
// We only render the ZWJ if there were actually any dynamic text nodes rendered
// The ZWJ is just so hydration can compare the SSR'd dynamic text content against
// the CSR'd text content.
{
const text = ${/* string value */ is.expression}
yield text === '' ? '\u200D' : htmlEscape(text);
}
`<EsBlockStatement>;
// We are at the end of a series of text nodes - flush to a concatenated string
// We only render the ZWJ if there were actually any dynamic text nodes rendered
// The ZWJ is just so hydration can compare the SSR'd dynamic text content against
// the CSR'd text content.
{
const text = ${/* string value */ is.expression};
yield text === '' ? '\u200D' : htmlEscape(text);
}
`<EsBlockStatement>;

const mergeTextNodes = (nodes: Array<IrText>, cxt: TransformerContext): EsStatement => {
cxt.import(['htmlEscape', 'massageTextContent']);
Expand All @@ -124,8 +119,8 @@ const mergeTextNodes = (nodes: Array<IrText>, cxt: TransformerContext): EsStatem
.map((expression) => bMassageTextContent(expression))
.reduce(
// @ts-expect-error FIXME later
(accumilator: EsBinaryExpression, expression: EsCallExpression | EsBinaryExpression) =>
b.binaryExpression('+', accumilator, expression)
(accumulator: EsBinaryExpression, expression: EsCallExpression | EsBinaryExpression) =>
b.binaryExpression('+', accumulator, expression)
);

return bYieldTextContent(mergedExpression);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import { builders as b, is } from 'estree-toolkit';
import { esTemplateWithYield } from '../../estemplate';
import { expressionIrToEs } from '../expression';
import { isLiteral } from '../shared';

import type { Statement as EsStatement, BlockStatement as EsBlockStatement } from 'estree';
import type { Text as IrText } from '@lwc/template-compiler';
import type { Transformer } from '../types';

const bYieldTextContent = esTemplateWithYield`
{
const text = massageTextContent(${/* string value */ is.expression});
yield text === '' ? '\u200D' : htmlEscape(text);
}`<EsBlockStatement>;

export const Text: Transformer<IrText> = function Text(node, cxt): EsStatement[] {
cxt.import(['htmlEscape', 'massageTextContent']);

const valueToYield = isLiteral(node.value)
? b.literal(node.value.value)
: expressionIrToEs(node.value, cxt);

return [bYieldTextContent(valueToYield)];
};

0 comments on commit 36c2e11

Please sign in to comment.