-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathadjacent-text-nodes.ts
More file actions
86 lines (75 loc) · 2.96 KB
/
adjacent-text-nodes.ts
File metadata and controls
86 lines (75 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright (c) 2024, Salesforce, 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 } from 'estree-toolkit/dist/builders';
import { is } from 'estree-toolkit';
import { esTemplate, esTemplateWithYield } from '../estemplate';
import { isLiteral } from './shared';
import { expressionIrToEs } from './expression';
import type {
CallExpression as EsCallExpression,
Expression as EsExpression,
ExpressionStatement as EsExpressionStatement,
} from 'estree';
import type { TransformerContext } from './types';
import type { Node as IrNode, Text as IrText } from '@lwc/template-compiler';
const bMassageTextContent = esTemplate`
massageTextContent(${/* string value */ is.expression});
`<EsCallExpression>;
const bYieldTextContent = esTemplateWithYield`
yield renderTextContent(${/* text concatenation, possibly as binary expression */ is.expression});
`<EsExpressionStatement>;
/**
* True if this is one of a series of text content nodes and/or comment node that are adjacent to one another as
* siblings. (Comment nodes are ignored when preserve-comments is turned off.) This allows for adjacent text
* node concatenation.
*/
const isConcatenatedNode = (node: IrNode, cxt: TransformerContext) => {
switch (node.type) {
case 'Text':
return true;
case 'Comment':
return !cxt.templateOptions.preserveComments;
default:
return false;
}
};
export const isLastConcatenatedNode = (cxt: TransformerContext) => {
const { nextSibling } = cxt;
if (!nextSibling) {
// we are the last sibling
return true;
}
return !isConcatenatedNode(nextSibling, cxt);
};
export function generateConcatenatedTextNodesExpressions(
cxt: TransformerContext,
lastValue?: EsExpression
) {
const values = [...cxt.bufferedTextNodeValues];
if (lastValue) {
values.push(lastValue);
}
if (!values.length) {
// Render nothing. This can occur if we hit a comment in non-preserveComments mode with no adjacent text nodes
return [];
}
cxt.import(['massageTextContent', 'renderTextContent']);
// Generate a binary expression to concatenate the text together. E.g.:
// renderTextContent(
// massageTextContent(a) +
// massageTextContent(b) +
// massageTextContent(c)
// )
const concatenatedExpression = values
.map((expression) => bMassageTextContent(expression) as EsExpression)
.reduce((accumulator, expression) => b.binaryExpression('+', accumulator, expression));
cxt.bufferedTextNodeValues.length = 0; // reset
return [bYieldTextContent(concatenatedExpression)];
}
export function generateExpressionFromTextNode(node: IrText, cxt: TransformerContext) {
return isLiteral(node.value) ? b.literal(node.value.value) : expressionIrToEs(node.value, cxt);
}