-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathast-utils.ts
More file actions
875 lines (729 loc) · 27.7 KB
/
ast-utils.ts
File metadata and controls
875 lines (729 loc) · 27.7 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
import {
Node,
LiteralNode,
ERBNode,
ERBContentNode,
ERBIfNode,
ERBUnlessNode,
ERBBlockNode,
ERBCaseNode,
ERBCaseMatchNode,
ERBWhileNode,
ERBForNode,
ERBBeginNode,
HTMLElementNode,
HTMLOpenTagNode,
HTMLCloseTagNode,
HTMLAttributeNode,
HTMLAttributeNameNode,
HTMLAttributeValueNode,
HTMLCommentNode,
WhitespaceNode
} from "./nodes.js"
import {
isAnyOf,
isLiteralNode,
isERBNode,
isERBContentNode,
isHTMLCommentNode,
isHTMLElementNode,
isHTMLOpenTagNode,
isHTMLTextNode,
isWhitespaceNode,
isHTMLAttributeNameNode,
isHTMLAttributeValueNode,
areAllOfType,
filterLiteralNodes,
filterHTMLAttributeNodes
} from "./node-type-guards.js"
import { Location } from "./location.js"
import { Range } from "./range.js"
import { Token } from "./token.js"
import type { Position } from "./position.js"
export type ERBOutputNode = ERBNode & {
tag_opening: {
value: "<%=" | "<%=="
}
}
export type ERBCommentNode = ERBNode & {
tag_opening: {
value: "<%#"
}
}
/**
* Checks if a node is an ERB output node (generates content: <%= %> or <%== %>)
*/
export function isERBOutputNode(node: Node): node is ERBOutputNode {
if (!isERBNode(node)) return false
if (!node.tag_opening?.value) return false
return ["<%=", "<%=="].includes(node.tag_opening?.value)
}
/**
* Checks if a node is a ERB comment node (control flow: <%# %>)
*/
export function isERBCommentNode(node: Node): node is ERBCommentNode {
if (!isERBNode(node)) return false
if (!node.tag_opening?.value) return false
return node.tag_opening?.value === "<%#" || (node.tag_opening?.value !== "<%#" && (node.content?.value || "").trimStart().startsWith("#"))
}
/**
* Checks if a node is a non-output ERB node (control flow: <% %>)
*/
export function isERBControlFlowNode(node: Node): node is ERBContentNode {
return isAnyOf(node, ERBIfNode, ERBUnlessNode, ERBBlockNode, ERBCaseNode, ERBCaseMatchNode, ERBWhileNode, ERBForNode, ERBBeginNode)
}
/**
* Checks if an array of nodes contains any ERB content nodes
*/
export function hasERBContent(nodes: Node[]): boolean {
return nodes.some(isERBContentNode)
}
/**
* Checks if an array of nodes contains any ERB output nodes (dynamic content)
*/
export function hasERBOutput(nodes: Node[]): boolean {
return nodes.some(isERBOutputNode)
}
/**
* Extracts a static string from an array of literal nodes
* Returns null if any node is not a literal node
*/
export function getStaticStringFromNodes(nodes: Node[]): string | null {
if (!areAllOfType(nodes, LiteralNode)) {
return null
}
return nodes.map(node => node.content).join("")
}
/**
* Extracts static content from nodes, including mixed literal/ERB content
* Returns the concatenated literal content, or null if no literal nodes exist
*/
export function getStaticContentFromNodes(nodes: Node[]): string | null {
const literalNodes = filterLiteralNodes(nodes)
if (literalNodes.length === 0) {
return null
}
return literalNodes.map(node => node.content).join("")
}
/**
* Checks if nodes contain any literal content (for static validation)
*/
export function hasStaticContent(nodes: Node[]): boolean {
return nodes.some(isLiteralNode)
}
/**
* Checks if nodes are effectively static (only literals and non-output ERB)
* Non-output ERB like <% if %> doesn't affect static validation
*/
export function isEffectivelyStatic(nodes: Node[]): boolean {
return !hasERBOutput(nodes)
}
/**
* Gets static-validatable content from nodes (ignores control ERB, includes literals)
* Returns concatenated literal content for validation, or null if contains output ERB
*/
export function getValidatableStaticContent(nodes: Node[]): string | null {
if (hasERBOutput(nodes)) {
return null
}
return filterLiteralNodes(nodes).map(node => node.content).join("")
}
/**
* Extracts a combined string from nodes, including ERB content
* For ERB nodes, includes the full tag syntax (e.g., "<%= foo %>")
* This is useful for debugging or displaying the full attribute name
*/
export function getCombinedStringFromNodes(nodes: Node[]): string {
return nodes.map(node => {
if (isLiteralNode(node)) {
return node.content
} else if (isERBContentNode(node)) {
const opening = node.tag_opening?.value || ""
const content = node.content?.value || ""
const closing = node.tag_closing?.value || ""
return `${opening}${content}${closing}`
} else {
// For other node types, return a placeholder or empty string
return `[${node.type}]`
}
}).join("")
}
/**
* Checks if an HTML attribute name node has a static (literal-only) name
*/
export function hasStaticAttributeName(attributeNameNode: HTMLAttributeNameNode): boolean {
if (!attributeNameNode.children) {
return false
}
return areAllOfType(attributeNameNode.children, LiteralNode)
}
/**
* Checks if an HTML attribute name node has dynamic content (contains ERB)
*/
export function hasDynamicAttributeName(attributeNameNode: HTMLAttributeNameNode): boolean {
if (!attributeNameNode.children) {
return false
}
return hasERBContent(attributeNameNode.children)
}
/**
* Gets the static string value of an HTML attribute name node
* Returns null if the attribute name contains dynamic content (ERB)
*/
export function getStaticAttributeName(attributeNameNode: HTMLAttributeNameNode): string | null {
if (!attributeNameNode.children) {
return null
}
return getStaticStringFromNodes(attributeNameNode.children)
}
/**
* Gets the combined string representation of an HTML attribute name node
* This includes both static and dynamic content, useful for debugging
*/
export function getCombinedAttributeName(attributeNameNode: HTMLAttributeNameNode): string {
if (!attributeNameNode.children) {
return ""
}
return getCombinedStringFromNodes(attributeNameNode.children)
}
/**
* Gets the tag name of an HTML element, open tag, or close tag node.
* Returns null if the node is null/undefined.
*/
export function getTagName(node: HTMLElementNode | HTMLOpenTagNode | HTMLCloseTagNode): string
export function getTagName(node: HTMLElementNode | HTMLOpenTagNode | HTMLCloseTagNode | null | undefined): string | null
export function getTagName(node: HTMLElementNode | HTMLOpenTagNode | HTMLCloseTagNode | null | undefined): string | null {
if (!node) return null
return node.tag_name?.value ?? null
}
/**
* Gets the lowercased tag name of an HTML element, open tag, or close tag node.
* Similar to `Element.localName` in the DOM API.
* Returns null if the node is null/undefined.
*/
export function getTagLocalName(node: HTMLElementNode | HTMLOpenTagNode | HTMLCloseTagNode): string
export function getTagLocalName(node: HTMLElementNode | HTMLOpenTagNode | HTMLCloseTagNode | null | undefined): string | null
export function getTagLocalName(node: HTMLElementNode | HTMLOpenTagNode | HTMLCloseTagNode | null | undefined): string | null {
return getTagName(node)?.toLowerCase() ?? null
}
/**
* Check if a node is a comment (HTML comment or ERB comment)
*/
export function isCommentNode(node: Node): node is HTMLCommentNode | ERBCommentNode {
return isHTMLCommentNode(node) || isERBCommentNode(node)
}
/**
* Gets the open tag node from an HTMLElementNode, handling both regular and conditional open tags.
* For conditional open tags, returns null.
* If given an HTMLOpenTagNode directly, returns it as-is.
*/
export function getOpenTag(node: HTMLElementNode | HTMLOpenTagNode | null | undefined): HTMLOpenTagNode | null {
if (!node) return null
if (isHTMLOpenTagNode(node)) return node
if (isHTMLElementNode(node)) return isHTMLOpenTagNode(node.open_tag) ? node.open_tag : null
return null
}
/**
* Gets attributes from an HTMLElementNode or HTMLOpenTagNode
*/
export function getAttributes(node: HTMLElementNode | HTMLOpenTagNode | null | undefined): HTMLAttributeNode[] {
const openTag = getOpenTag(node)
return openTag ? filterHTMLAttributeNodes(openTag.children) : []
}
/**
* Gets the attribute name from an HTMLAttributeNode (lowercased)
* Returns null if the attribute name contains dynamic content (ERB)
*/
export function getAttributeName(attributeNode: HTMLAttributeNode, lowercase = true): string | null {
if (!isHTMLAttributeNameNode(attributeNode.name)) return null
const staticName = getStaticAttributeName(attributeNode.name)
if (!lowercase) return staticName
return staticName ? staticName.toLowerCase() : null
}
/**
* Checks if an attribute value contains only static content (no ERB).
* Accepts an HTMLAttributeNode directly, or an element/open tag + attribute name.
* Returns false for null/undefined input.
*/
export function hasStaticAttributeValue(attributeNode: HTMLAttributeNode | null | undefined): boolean
export function hasStaticAttributeValue(node: HTMLElementNode | HTMLOpenTagNode | null | undefined, attributeName: string): boolean
export function hasStaticAttributeValue(nodeOrAttribute: HTMLAttributeNode | HTMLElementNode | HTMLOpenTagNode | null | undefined, attributeName?: string): boolean {
const attributeNode = attributeName
? getAttribute(nodeOrAttribute as HTMLElementNode | HTMLOpenTagNode, attributeName)
: nodeOrAttribute as HTMLAttributeNode | null | undefined
if (!attributeNode?.value?.children) return false
return attributeNode.value.children.every(isLiteralNode)
}
/**
* Gets the static string value of an attribute (returns null if it contains ERB).
* Accepts an HTMLAttributeNode directly, or an element/open tag + attribute name.
* Returns null for null/undefined input.
*/
export function getStaticAttributeValue(attributeNode: HTMLAttributeNode | null | undefined): string | null
export function getStaticAttributeValue(node: HTMLElementNode | HTMLOpenTagNode | null | undefined, attributeName: string): string | null
export function getStaticAttributeValue(nodeOrAttribute: HTMLAttributeNode | HTMLElementNode | HTMLOpenTagNode | null | undefined, attributeName?: string): string | null {
const attributeNode = attributeName
? getAttribute(nodeOrAttribute as HTMLElementNode | HTMLOpenTagNode, attributeName)
: nodeOrAttribute as HTMLAttributeNode | null | undefined
if (!attributeNode) return null
if (!hasStaticAttributeValue(attributeNode)) return null
const valueNode = attributeNode.value
if (!valueNode) return null
return filterLiteralNodes(valueNode.children).map(child => child.content).join("") || ""
}
/**
* Attributes whose values are space-separated token lists.
*/
export const TOKEN_LIST_ATTRIBUTES = new Set([
"class", "data-controller", "data-action",
])
/**
* Splits a space-separated attribute value into individual tokens.
* Accepts a string, or an element/open tag + attribute name to look up.
* Returns an empty array for null/undefined/empty input.
*/
export function getTokenList(value: string | null | undefined): string[]
export function getTokenList(node: HTMLElementNode | HTMLOpenTagNode | null | undefined, attributeName: string): string[]
export function getTokenList(valueOrNode: string | HTMLElementNode | HTMLOpenTagNode | null | undefined, attributeName?: string): string[] {
const value = attributeName
? getStaticAttributeValue(valueOrNode as HTMLElementNode | HTMLOpenTagNode, attributeName)
: valueOrNode as string | null | undefined
if (!value) return []
return value.trim().split(/\s+/).filter(token => token.length > 0)
}
/**
* Finds an attribute by name in a list of attribute nodes
*/
export function findAttributeByName(attributes: Node[], attributeName: string): HTMLAttributeNode | null {
for (const attribute of filterHTMLAttributeNodes(attributes)) {
const name = getAttributeName(attribute)
if (name === attributeName.toLowerCase()) {
return attribute
}
}
return null
}
/**
* Gets a specific attribute from an HTMLElementNode or HTMLOpenTagNode by name
*/
export function getAttribute(node: HTMLElementNode | HTMLOpenTagNode | null | undefined, attributeName: string): HTMLAttributeNode | null {
const attributes = getAttributes(node)
return findAttributeByName(attributes, attributeName)
}
/**
* Checks if an element or open tag has a specific attribute
*/
export function hasAttribute(node: HTMLElementNode | HTMLOpenTagNode | null | undefined, attributeName: string): boolean {
if (!node) return false
return getAttribute(node, attributeName) !== null
}
/**
* Checks if an attribute has a dynamic (ERB-containing) name.
* Accepts an HTMLAttributeNode (wraps the core HTMLAttributeNameNode-level check).
*/
export function hasDynamicAttributeNameOnAttribute(attributeNode: HTMLAttributeNode): boolean {
if (!isHTMLAttributeNameNode(attributeNode.name)) return false
return hasDynamicAttributeName(attributeNode.name)
}
/**
* Gets the combined string representation of an attribute name (including ERB syntax).
* Accepts an HTMLAttributeNode (wraps the core HTMLAttributeNameNode-level check).
*/
export function getCombinedAttributeNameString(attributeNode: HTMLAttributeNode): string {
if (!isHTMLAttributeNameNode(attributeNode.name)) return ""
return getCombinedAttributeName(attributeNode.name)
}
/**
* Checks if an attribute value contains dynamic content (ERB)
*/
export function hasDynamicAttributeValue(attributeNode: HTMLAttributeNode): boolean {
if (!attributeNode.value?.children) return false
return attributeNode.value.children.some(isERBContentNode)
}
/**
* Gets the value nodes array from an attribute for dynamic inspection
*/
export function getAttributeValueNodes(attributeNode: HTMLAttributeNode): Node[] {
return attributeNode.value?.children || []
}
/**
* Checks if an attribute value contains any static content (for validation purposes)
*/
export function hasStaticAttributeValueContent(attributeNode: HTMLAttributeNode): boolean {
return hasStaticContent(getAttributeValueNodes(attributeNode))
}
/**
* Gets the static content of an attribute value (all literal parts combined).
* Unlike getStaticAttributeValue, this extracts only the static portions from mixed content.
* Returns the concatenated literal content, or null if no literal nodes exist.
*/
export function getStaticAttributeValueContent(attributeNode: HTMLAttributeNode): string | null {
return getStaticContentFromNodes(getAttributeValueNodes(attributeNode))
}
/**
* Gets the combined attribute value including both static text and ERB tag syntax.
* For ERB nodes, includes the full tag syntax (e.g., "<%= foo %>").
* Returns null if the attribute has no value.
*/
export function getAttributeValue(attributeNode: HTMLAttributeNode): string | null {
const valueNode = attributeNode.value
if (!valueNode) return null
if (valueNode.type !== "AST_HTML_ATTRIBUTE_VALUE_NODE" || !valueNode.children?.length) {
return null
}
let result = ""
for (const child of valueNode.children) {
if (isERBContentNode(child)) {
if (child.content) {
result += `${child.tag_opening?.value}${child.content.value}${child.tag_closing?.value}`
}
} else if (isLiteralNode(child)) {
result += child.content
}
}
return result
}
/**
* Checks if an attribute has a value node
*/
export function hasAttributeValue(attributeNode: HTMLAttributeNode): boolean {
return isHTMLAttributeValueNode(attributeNode.value)
}
/**
* Gets the quote type used for an attribute value
*/
export function getAttributeValueQuoteType(node: HTMLAttributeNode | HTMLAttributeValueNode): "single" | "double" | "none" | null {
const valueNode = isHTMLAttributeValueNode(node) ? node : node.value
if (!valueNode) return null
if (valueNode.quoted && valueNode.open_quote) {
return valueNode.open_quote.value === '"' ? "double" : "single"
}
return "none"
}
/**
* Checks if an attribute value is quoted
*/
export function isAttributeValueQuoted(attributeNode: HTMLAttributeNode): boolean {
if (!isHTMLAttributeValueNode(attributeNode.value)) return false
return !!attributeNode.value.quoted
}
/**
* Iterates over all attributes of an element or open tag node
*/
export function forEachAttribute(node: HTMLElementNode | HTMLOpenTagNode, callback: (attributeNode: HTMLAttributeNode) => void): void {
for (const attribute of getAttributes(node)) {
callback(attribute)
}
}
// --- Class Name Grouping Utilities ---
/**
* Checks if a node is a whitespace-only literal or text node (no visible content)
*/
export function isPureWhitespaceNode(node: Node): boolean {
if (isWhitespaceNode(node)) return true
if (isLiteralNode(node)) return !node.content.trim()
if (isHTMLTextNode(node)) return !(node.content ?? "").trim()
return false
}
/**
* Splits literal nodes at whitespace boundaries into separate nodes.
* Non-literal nodes are passed through unchanged.
*
* For example, a literal `"bg-blue-500 text-white"` becomes two literals:
* `"bg-blue-500"` and `" "` and `"text-white"`.
*/
export function splitLiteralsAtWhitespace(nodes: Node[]): Node[] {
const result: Node[] = []
for (const node of nodes) {
if (isLiteralNode(node)) {
const parts = node.content.match(/(\S+|\s+)/g) || []
for (const part of parts) {
result.push(new LiteralNode({
type: "AST_LITERAL_NODE",
content: part,
errors: [],
location: node.location
}))
}
} else {
result.push(node)
}
}
return result
}
/**
* Groups split nodes into "class groups" where each group represents a single
* class name (possibly spanning multiple nodes when ERB is interpolated).
*
* For example, `text-<%= color %>-500 bg-blue-500` produces two groups:
* - [`text-`, ERB, `-500`] (interpolated class)
* - [`bg-blue-500`] (static class)
*
* The key heuristic: a hyphen at a node boundary means the nodes are part of
* the same class name (e.g., `bg-` + ERB + `-500`), while whitespace means
* a new class name starts.
*/
export function groupNodesByClass(nodes: Node[]): Node[][] {
if (nodes.length === 0) return []
const groups: Node[][] = []
let currentGroup: Node[] = []
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
const previousNode = i > 0 ? nodes[i - 1] : null
let startNewGroup = false
if (currentGroup.length === 0) {
startNewGroup = false
} else if (isLiteralNode(node)) {
if (/^\s/.test(node.content)) {
startNewGroup = true
} else if (/^-/.test(node.content)) {
startNewGroup = false
} else if (previousNode && !isLiteralNode(previousNode)) {
startNewGroup = true
} else if (currentGroup.every(member => isPureWhitespaceNode(member))) {
startNewGroup = true
}
} else {
if (previousNode && isLiteralNode(previousNode)) {
if (/\s$/.test(previousNode.content)) {
startNewGroup = true
} else if (/-$/.test(previousNode.content)) {
startNewGroup = false
} else {
startNewGroup = true
}
} else if (previousNode && !isLiteralNode(previousNode)) {
startNewGroup = false
}
}
if (startNewGroup && currentGroup.length > 0) {
groups.push(currentGroup)
currentGroup = []
}
currentGroup.push(node)
}
if (currentGroup.length > 0) {
groups.push(currentGroup)
}
return groups
}
// --- Position Utilities ---
/**
* Compares two positions to determine if the first comes before the second
* Returns true if pos1 comes before pos2 in source order
* @param inclusive - If true, returns true when positions are equal
*/
function isPositionBefore(position1: Position, position2: Position, inclusive = false): boolean {
if (position1.line < position2.line) return true
if (position1.line > position2.line) return false
return inclusive ? position1.column <= position2.column : position1.column < position2.column
}
/**
* Compares two positions to determine if they are equal
* Returns true if pos1 and pos2 are at the same location
*/
export function isPositionEqual(position1: Position, position2: Position): boolean {
return position1.line === position2.line && position1.column === position2.column
}
/**
* Compares two positions to determine if the first comes after the second
* Returns true if pos1 comes after pos2 in source order
* @param inclusive - If true, returns true when positions are equal
*/
export function isPositionAfter(position1: Position, position2: Position, inclusive = false): boolean {
if (position1.line > position2.line) return true
if (position1.line < position2.line) return false
return inclusive ? position1.column >= position2.column : position1.column > position2.column
}
/**
* Gets nodes that appear before the specified location in source order
* Uses line and column positions to determine ordering
*/
export function getNodesBeforeLocation<T extends Node>(nodes: T[], location: Location): T[] {
return nodes.filter(node =>
node.location && isPositionBefore(node.location.end, location.start)
)
}
/**
* Gets nodes that appear after the specified location in source order
* Uses line and column positions to determine ordering
*/
export function getNodesAfterLocation<T extends Node>(nodes: T[], location: Location): T[] {
return nodes.filter(node =>
node.location && isPositionAfter(node.location.start, location.end)
)
}
/**
* Splits nodes into before and after the specified location
* Returns an object with `before` and `after` arrays
*/
export function splitNodesAroundLocation<T extends Node>(nodes: T[], location: Location): { before: T[], after: T[] } {
return {
before: getNodesBeforeLocation(nodes, location),
after: getNodesAfterLocation(nodes, location)
}
}
/**
* Splits nodes at a specific position
* Returns nodes that end before the position and nodes that start after the position
* More precise than splitNodesAroundLocation as it uses a single position point
* Uses the same defaults as the individual functions: before=exclusive, after=inclusive
*/
export function splitNodesAroundPosition<T extends Node>(nodes: T[], position: Position): { before: T[], after: T[] } {
return {
before: getNodesBeforePosition(nodes, position), // uses default: inclusive = false
after: getNodesAfterPosition(nodes, position) // uses default: inclusive = true
}
}
/**
* Gets nodes that end before the specified position
* @param inclusive - If true, includes nodes that end exactly at the position (default: false, matching half-open interval semantics)
*/
export function getNodesBeforePosition<T extends Node>(nodes: T[], position: Position, inclusive = false): T[] {
return nodes.filter(node =>
node.location && isPositionBefore(node.location.end, position, inclusive)
)
}
/**
* Gets nodes that start after the specified position
* @param inclusive - If true, includes nodes that start exactly at the position (default: true, matching typical boundary behavior)
*/
export function getNodesAfterPosition<T extends Node>(nodes: T[], position: Position, inclusive = true): T[] {
return nodes.filter(node =>
node.location && isPositionAfter(node.location.start, position, inclusive)
)
}
/**
* Checks if two attributes are structurally equivalent (same name and value),
* ignoring positional data like location and range.
*/
export function isEquivalentAttribute(first: HTMLAttributeNode, second: HTMLAttributeNode): boolean {
const firstName = getAttributeName(first)
const secondName = getAttributeName(second)
if (firstName !== secondName) return false
if (firstName && TOKEN_LIST_ATTRIBUTES.has(firstName)) {
const firstTokens = getTokenList(getAttributeValue(first))
const secondTokens = getTokenList(getAttributeValue(second))
if (firstTokens.length !== secondTokens.length) return false
const sortedFirst = [...firstTokens].sort()
const sortedSecond = [...secondTokens].sort()
return sortedFirst.every((token, index) => token === sortedSecond[index])
}
return getAttributeValue(first) === getAttributeValue(second)
}
/**
* Checks if two open tags are structurally equivalent (same tag name and attributes),
* ignoring positional data like location and range.
*/
export function isEquivalentOpenTag(first: HTMLOpenTagNode, second: HTMLOpenTagNode): boolean {
if (first.tag_name?.value !== second.tag_name?.value) return false
const firstAttributes = getAttributes(first)
const secondAttributes = getAttributes(second)
if (firstAttributes.length !== secondAttributes.length) return false
return firstAttributes.every((attribute) =>
secondAttributes.some((other) => isEquivalentAttribute(attribute, other))
)
}
/**
* Checks if two elements have structurally equivalent open tags (same tag name and attributes),
* ignoring positional data like location and range. Does not compare body or close tag.
*/
export function isEquivalentElement(first: HTMLElementNode, second: HTMLElementNode): boolean {
if (!isHTMLOpenTagNode(first.open_tag) || !isHTMLOpenTagNode(second.open_tag)) return false
return isEquivalentOpenTag(first.open_tag, second.open_tag)
}
// --- AST Mutation Utilities ---
const CHILD_ARRAY_PROPS = ["children", "body", "statements", "conditions"]
const LINKED_NODE_PROPS = ["subsequent", "else_clause"]
/**
* Finds the array containing a target node in the AST, along with its index.
* Traverses child arrays and linked node properties (e.g., `subsequent`, `else_clause`).
*
* Useful for autofix operations that need to splice nodes in/out of their parent array.
*
* @param root - The root node to search from
* @param target - The node to find
* @returns The containing array and the target's index, or null if not found
*/
export function findParentArray(root: Node, target: Node): { array: Node[], index: number } | null {
const search = (node: Node): { array: Node[], index: number } | null => {
const record = node as Record<string, any>
for (const prop of CHILD_ARRAY_PROPS) {
const array = record[prop]
if (Array.isArray(array)) {
const index = array.indexOf(target)
if (index !== -1) {
return { array, index }
}
}
}
for (const prop of CHILD_ARRAY_PROPS) {
const array = record[prop]
if (Array.isArray(array)) {
for (const child of array) {
if (child && typeof child === 'object' && 'type' in child) {
const result = search(child)
if (result) {
return result
}
}
}
}
}
for (const prop of LINKED_NODE_PROPS) {
const value = record[prop]
if (value && typeof value === 'object' && 'type' in value) {
const result = search(value)
if (result) {
return result
}
}
}
return null
}
return search(root)
}
/**
* Removes a node from an array, also removing an adjacent preceding
* whitespace-only literal if present.
*/
export function removeNodeFromArray(array: Node[], node: Node): void {
const index = array.indexOf(node)
if (index === -1) return
if (index > 0 && isPureWhitespaceNode(array[index - 1])) {
array.splice(index - 1, 2)
} else {
array.splice(index, 1)
}
}
/**
* Replaces an element in an array with its body (children), effectively unwrapping it.
*/
export function replaceNodeWithBody(array: Node[], element: HTMLElementNode): void {
const index = array.indexOf(element)
if (index === -1) return
array.splice(index, 1, ...element.body)
}
/**
* Creates a synthetic LiteralNode with the given content and zero location.
* Useful for inserting whitespace or newlines during AST mutations.
*/
export function createLiteral(content: string): LiteralNode {
return new LiteralNode({
type: "AST_LITERAL_NODE",
content,
location: Location.zero,
errors: [],
})
}
export function createSyntheticToken(value: string, type = "TOKEN_SYNTHETIC"): Token {
return new Token(value, Range.zero, Location.zero, type)
}
export function createWhitespaceNode(): WhitespaceNode {
return new WhitespaceNode({
type: "AST_WHITESPACE_NODE",
location: Location.zero,
errors: [],
value: createSyntheticToken(" "),
})
}