meldConsecutiveStringLiterals simplifies the AST output by combining a sequence of string literal tokens into a single string literal. This must be performed after any whitespace trimming operations.
(parts *templateParts)
| 759 | // sequence of string literal tokens into a single string literal. This must be |
| 760 | // performed after any whitespace trimming operations. |
| 761 | func meldConsecutiveStringLiterals(parts *templateParts) { |
| 762 | if len(parts.Tokens) == 0 { |
| 763 | return |
| 764 | } |
| 765 | |
| 766 | // Loop over all tokens starting at the second element, as we want to join |
| 767 | // pairs of consecutive string literals. |
| 768 | i := 1 |
| 769 | for i < len(parts.Tokens) { |
| 770 | if prevLiteral, ok := parts.Tokens[i-1].(*templateLiteralToken); ok { |
| 771 | if literal, ok := parts.Tokens[i].(*templateLiteralToken); ok { |
| 772 | // The current and previous tokens are both literals: combine |
| 773 | prevLiteral.Val = prevLiteral.Val + literal.Val |
| 774 | prevLiteral.SrcRange.End = literal.SrcRange.End |
| 775 | |
| 776 | // Remove the current token from the slice |
| 777 | parts.Tokens = append(parts.Tokens[:i], parts.Tokens[i+1:]...) |
| 778 | |
| 779 | // Continue without moving forward in the slice |
| 780 | continue |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // Try the next pair of tokens |
| 785 | i++ |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | type templateParts struct { |
| 790 | Tokens []templateToken |