Skip to content

Commit

Permalink
fix(ssr-compiler): transform of empty text strings (#4963)
Browse files Browse the repository at this point in the history
Co-authored-by: Nolan Lawson <[email protected]>
  • Loading branch information
jhefferman-sfdc and nolanlawson authored Nov 26, 2024
1 parent c3db1c7 commit 33b7737
Showing 1 changed file with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { builders as b, is } from 'estree-toolkit';
import { esTemplateWithYield } from '../../estemplate';
import { expressionIrToEs } from '../expression';

import type { Expression as EsExpression, Statement as EsStatement } from 'estree';
import type {
Expression as EsExpression,
Statement as EsStatement,
BlockStatement as EsBlockStatement,
} from 'estree';
import type {
ComplexExpression as IrComplexExpression,
Expression as IrExpression,
Expand All @@ -22,19 +26,14 @@ import type { Transformer } from '../types';
const bYield = (expr: EsExpression) => b.expressionStatement(b.yieldExpression(expr));

const bYieldEscapedString = esTemplateWithYield`
const ${/* temp var */ is.identifier} = ${/* string value */ is.expression};
switch (typeof ${0}) {
case 'string':
yield (${/* is isolated text node? */ is.literal} && ${0} === '') ? '\\u200D' : htmlEscape(${0});
break;
case 'number':
case 'boolean':
yield String(${0});
break;
default:
yield ${0} ? htmlEscape(${0}.toString()) : ${2} ? '\\u200D' : '';
{
const value = ${/* string value */ is.expression};
// Using non strict equality to align with original implementation (ex. undefined == null)
// See: https://github.com/salesforce/lwc/blob/348130f/packages/%40lwc/engine-core/src/framework/api.ts#L548
const massagedValue = value == null ? '' : String(value);
yield ${/* is isolated text node? */ is.literal} && massagedValue === '' ? '\\u200D' : htmlEscape(massagedValue);
}
`<EsStatement[]>;
`<EsBlockStatement>;

function isLiteral(node: IrLiteral | IrExpression | IrComplexExpression): node is IrLiteral {
return node.type === 'Literal';
Expand All @@ -61,8 +60,7 @@ export const Text: Transformer<IrText> = function Text(node, cxt): EsStatement[]
);

const valueToYield = expressionIrToEs(node.value, cxt);
const tempVariable = b.identifier(cxt.getUniqueVar());

cxt.import('htmlEscape');
return bYieldEscapedString(tempVariable, valueToYield, isIsolatedTextNode);
return [bYieldEscapedString(valueToYield, isIsolatedTextNode)];
};

0 comments on commit 33b7737

Please sign in to comment.