Skip to content

Commit c885fb6

Browse files
author
Erlend Oftedal
committed
Template literals
1 parent 4b43ff0 commit c885fb6

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

src/utils/astTreeBuilder.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ function formatNodeLabel(node: ASTNode): string {
124124
'name' in node.id
125125
) {
126126
label += `: ${node.id.name}`
127+
} else if (node.type === 'TemplateElement' && 'value' in node) {
128+
const value = (node as { value: { raw: string; cooked: string | null } }).value
129+
label += `: \`${value.raw}\``
127130
}
128131

129132
return label
@@ -142,18 +145,30 @@ export function buildTree(
142145
// Get child property names for this node type
143146
const childKeys = VISITOR_KEYS[node.type as keyof typeof VISITOR_KEYS] || []
144147

145-
for (const key of childKeys) {
146-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
147-
const child = (node as any)[key]
148+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
149+
const anyNode = node as any
148150

149-
if (Array.isArray(child)) {
150-
for (const item of child) {
151-
if (item && typeof item === 'object' && 'type' in item) {
152-
children.push(buildTree(item, matchedNodes, primitiveResults))
151+
if (node.type === 'TemplateLiteral' && Array.isArray(anyNode.quasis) && Array.isArray(anyNode.expressions)) {
152+
// Interleave quasis and expressions by source position: quasi[0], expr[0], quasi[1], expr[1], ..., quasi[n]
153+
for (let i = 0; i < anyNode.quasis.length; i++) {
154+
children.push(buildTree(anyNode.quasis[i], matchedNodes, primitiveResults))
155+
if (i < anyNode.expressions.length) {
156+
children.push(buildTree(anyNode.expressions[i], matchedNodes, primitiveResults))
157+
}
158+
}
159+
} else {
160+
for (const key of childKeys) {
161+
const child = anyNode[key]
162+
163+
if (Array.isArray(child)) {
164+
for (const item of child) {
165+
if (item && typeof item === 'object' && 'type' in item) {
166+
children.push(buildTree(item, matchedNodes, primitiveResults))
167+
}
153168
}
169+
} else if (child && typeof child === 'object' && 'type' in child) {
170+
children.push(buildTree(child, matchedNodes, primitiveResults))
154171
}
155-
} else if (child && typeof child === 'object' && 'type' in child) {
156-
children.push(buildTree(child, matchedNodes, primitiveResults))
157172
}
158173
}
159174

0 commit comments

Comments
 (0)