@herb-tools/formatter 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/formatter.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Printer } from "./printer.js"
1
+ import { FormatPrinter } from "./format-printer.js"
2
2
  import { resolveFormatOptions } from "./options.js"
3
3
 
4
4
  import type { FormatOptions } from "./options.js"
@@ -26,7 +26,7 @@ export class Formatter {
26
26
 
27
27
  const resolvedOptions = resolveFormatOptions({ ...this.options, ...options })
28
28
 
29
- return new Printer(source, resolvedOptions).print(result.value)
29
+ return new FormatPrinter(source, resolvedOptions).print(result.value)
30
30
  }
31
31
 
32
32
  private parse(source: string): ParseResult {
package/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { Formatter } from "./formatter.js"
2
- export { defaultFormatOptions, resolveFormatOptions } from "./options.js"
3
- // export { CLI } from "./cli.js"
4
-
2
+ export { FormatPrinter } from "./format-printer.js"
5
3
  export type { FormatOptions } from "./options.js"
4
+ export { defaultFormatOptions, resolveFormatOptions } from "./options.js"
@@ -1,134 +0,0 @@
1
- import { Visitor } from "@herb-tools/core";
2
- import { Node, DocumentNode, HTMLOpenTagNode, HTMLCloseTagNode, HTMLSelfCloseTagNode, HTMLElementNode, HTMLAttributeNode, HTMLAttributeValueNode, HTMLAttributeNameNode, HTMLTextNode, HTMLCommentNode, HTMLDoctypeNode, ERBContentNode, ERBBlockNode, ERBEndNode, ERBElseNode, ERBIfNode, ERBWhenNode, ERBCaseNode, ERBCaseMatchNode, ERBWhileNode, ERBUntilNode, ERBForNode, ERBRescueNode, ERBEnsureNode, ERBBeginNode, ERBUnlessNode, ERBYieldNode, ERBInNode, Token } from "@herb-tools/core";
3
- import type { FormatOptions } from "./options.js";
4
- /**
5
- * Printer traverses the Herb AST using the Visitor pattern
6
- * and emits a formatted string with proper indentation, line breaks, and attribute wrapping.
7
- */
8
- export declare class Printer extends Visitor {
9
- private indentWidth;
10
- private maxLineLength;
11
- private source;
12
- private lines;
13
- private indentLevel;
14
- private inlineMode;
15
- private isInComplexNesting;
16
- private currentTagName;
17
- private static readonly INLINE_ELEMENTS;
18
- constructor(source: string, options: Required<FormatOptions>);
19
- print(object: Node | Token, indentLevel?: number): string;
20
- private push;
21
- private withIndent;
22
- private indent;
23
- /**
24
- * Format ERB content with proper spacing around the inner content.
25
- * Returns empty string if content is empty, otherwise wraps content with single spaces.
26
- */
27
- private formatERBContent;
28
- /**
29
- * Check if a node is an ERB control flow node (if, unless, block, case, while, for)
30
- */
31
- private isERBControlFlow;
32
- /**
33
- * Count total attributes including those inside ERB conditionals
34
- */
35
- private getTotalAttributeCount;
36
- /**
37
- * Extract HTML attributes from a list of nodes
38
- */
39
- private extractAttributes;
40
- /**
41
- * Extract inline nodes (non-attribute, non-whitespace) from a list of nodes
42
- */
43
- private extractInlineNodes;
44
- /**
45
- * Render attributes as a space-separated string
46
- */
47
- private renderAttributesString;
48
- /**
49
- * Determine if a tag should be rendered inline based on attribute count and other factors
50
- */
51
- private shouldRenderInline;
52
- private hasMultilineAttributes;
53
- private formatClassAttribute;
54
- private isFormattableAttribute;
55
- private formatMultilineAttribute;
56
- private formatMultilineAttributeValue;
57
- private breakTokensIntoLines;
58
- /**
59
- * Render multiline attributes for a tag
60
- */
61
- private renderMultilineAttributes;
62
- /**
63
- * Print an ERB tag (<% %> or <%= %>) with single spaces around inner content.
64
- */
65
- private printERBNode;
66
- visitDocumentNode(node: DocumentNode): void;
67
- visitHTMLElementNode(node: HTMLElementNode): void;
68
- visitHTMLOpenTagNode(node: HTMLOpenTagNode): void;
69
- visitHTMLSelfCloseTagNode(node: HTMLSelfCloseTagNode): void;
70
- visitHTMLCloseTagNode(node: HTMLCloseTagNode): void;
71
- visitHTMLTextNode(node: HTMLTextNode): void;
72
- visitHTMLAttributeNode(node: HTMLAttributeNode): void;
73
- visitHTMLAttributeNameNode(node: HTMLAttributeNameNode): void;
74
- visitHTMLAttributeValueNode(node: HTMLAttributeValueNode): void;
75
- visitHTMLCommentNode(node: HTMLCommentNode): void;
76
- visitERBCommentNode(node: ERBContentNode): void;
77
- visitHTMLDoctypeNode(node: HTMLDoctypeNode): void;
78
- visitERBContentNode(node: ERBContentNode): void;
79
- visitERBEndNode(node: ERBEndNode): void;
80
- visitERBYieldNode(node: ERBYieldNode): void;
81
- visitERBInNode(node: ERBInNode): void;
82
- visitERBCaseMatchNode(node: ERBCaseMatchNode): void;
83
- visitERBBlockNode(node: ERBBlockNode): void;
84
- visitERBIfNode(node: ERBIfNode): void;
85
- visitERBElseNode(node: ERBElseNode): void;
86
- visitERBWhenNode(node: ERBWhenNode): void;
87
- visitERBCaseNode(node: ERBCaseNode): void;
88
- visitERBBeginNode(node: ERBBeginNode): void;
89
- visitERBWhileNode(node: ERBWhileNode): void;
90
- visitERBUntilNode(node: ERBUntilNode): void;
91
- visitERBForNode(node: ERBForNode): void;
92
- visitERBRescueNode(node: ERBRescueNode): void;
93
- visitERBEnsureNode(node: ERBEnsureNode): void;
94
- visitERBUnlessNode(node: ERBUnlessNode): void;
95
- private visitERBGeneric;
96
- private isNonWhitespaceNode;
97
- /**
98
- * Check if an element should be treated as inline based on its tag name
99
- */
100
- private isInlineElement;
101
- /**
102
- * Check if we're in a text flow context (parent contains mixed text and inline elements)
103
- */
104
- private visitTextFlowChildren;
105
- private visitTextFlowChildrenMultiline;
106
- private isInTextFlowContext;
107
- private renderInlineOpen;
108
- renderAttribute(attribute: HTMLAttributeNode): string;
109
- /**
110
- * Try to render a complete element inline including opening tag, children, and closing tag
111
- */
112
- private tryRenderInlineFull;
113
- /**
114
- * Try to render just the children inline (without tags)
115
- */
116
- private tryRenderChildrenInline;
117
- /**
118
- * Try to render children inline if they are simple enough.
119
- * Returns the inline string if possible, null otherwise.
120
- */
121
- private tryRenderInline;
122
- /**
123
- * Estimate the total content length of children nodes for decision making.
124
- */
125
- private estimateContentLength;
126
- /**
127
- * Calculate the maximum nesting depth in a subtree of nodes.
128
- */
129
- private getMaxNestingDepth;
130
- /**
131
- * Render an HTML element's content inline (without the wrapping tags).
132
- */
133
- private renderElementInline;
134
- }