@herb-tools/formatter 0.4.0 → 0.4.1

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/dist/index.cjs CHANGED
@@ -131,7 +131,7 @@ class Token {
131
131
  }
132
132
 
133
133
  // NOTE: This file is generated by the templates/template.rb script and should not
134
- // be modified manually. See /Users/marcoroth/Development/herb-release/templates/javascript/packages/core/src/errors.ts.erb
134
+ // be modified manually. See /Users/marcoroth/Development/herb-release-4/templates/javascript/packages/core/src/errors.ts.erb
135
135
  class HerbError {
136
136
  type;
137
137
  message;
@@ -581,7 +581,7 @@ function convertToUTF8(string) {
581
581
  }
582
582
 
583
583
  // NOTE: This file is generated by the templates/template.rb script and should not
584
- // be modified manually. See /Users/marcoroth/Development/herb-release/templates/javascript/packages/core/src/nodes.ts.erb
584
+ // be modified manually. See /Users/marcoroth/Development/herb-release-4/templates/javascript/packages/core/src/nodes.ts.erb
585
585
  class Node {
586
586
  type;
587
587
  location;
@@ -2576,7 +2576,7 @@ function fromSerializedNode(node) {
2576
2576
  }
2577
2577
 
2578
2578
  // NOTE: This file is generated by the templates/template.rb script and should not
2579
- // be modified manually. See /Users/marcoroth/Development/herb-release/templates/javascript/packages/core/src/visitor.ts.erb
2579
+ // be modified manually. See /Users/marcoroth/Development/herb-release-4/templates/javascript/packages/core/src/visitor.ts.erb
2580
2580
  class Visitor {
2581
2581
  visit(node) {
2582
2582
  if (!node)
@@ -2691,6 +2691,7 @@ class Printer extends Visitor {
2691
2691
  source;
2692
2692
  lines = [];
2693
2693
  indentLevel = 0;
2694
+ inlineMode = false;
2694
2695
  constructor(source, options) {
2695
2696
  super();
2696
2697
  this.source = source;
@@ -2753,6 +2754,8 @@ class Printer extends Visitor {
2753
2754
  const tagName = open.tag_name?.value ?? "";
2754
2755
  const indent = this.indent();
2755
2756
  const attributes = open.children.filter((child) => child instanceof HTMLAttributeNode || child.type === 'AST_HTML_ATTRIBUTE_NODE');
2757
+ const inlineNodes = open.children.filter(child => !(child instanceof HTMLAttributeNode || child.type === 'AST_HTML_ATTRIBUTE_NODE') &&
2758
+ !(child instanceof WhitespaceNode || child.type === 'AST_WHITESPACE_NODE'));
2756
2759
  const children = node.body.filter(child => !(child instanceof WhitespaceNode || child.type === 'AST_WHITESPACE_NODE') &&
2757
2760
  !((child instanceof HTMLTextNode || child.type === 'AST_HTML_TEXT_NODE') && child?.content.trim() === ""));
2758
2761
  const hasClosing = open.tag_closing?.value === ">" || open.tag_closing?.value === "/>";
@@ -2761,7 +2764,7 @@ class Printer extends Visitor {
2761
2764
  this.push(indent + `<${tagName}`);
2762
2765
  return;
2763
2766
  }
2764
- if (attributes.length === 0) {
2767
+ if (attributes.length === 0 && inlineNodes.length === 0) {
2765
2768
  if (children.length === 0) {
2766
2769
  if (isSelfClosing) {
2767
2770
  this.push(indent + `<${tagName} />`);
@@ -2783,14 +2786,35 @@ class Printer extends Visitor {
2783
2786
  }
2784
2787
  return;
2785
2788
  }
2786
- const inline = this.renderInlineOpen(tagName, attributes, isSelfClosing);
2789
+ if (attributes.length === 0 && inlineNodes.length > 0) {
2790
+ const inline = this.renderInlineOpen(tagName, [], isSelfClosing, inlineNodes, open.children);
2791
+ if (children.length === 0) {
2792
+ if (isSelfClosing || node.is_void) {
2793
+ this.push(indent + inline);
2794
+ }
2795
+ else {
2796
+ this.push(indent + inline + `</${tagName}>`);
2797
+ }
2798
+ return;
2799
+ }
2800
+ this.push(indent + inline);
2801
+ this.withIndent(() => {
2802
+ children.forEach(child => this.visit(child));
2803
+ });
2804
+ if (!node.is_void && !isSelfClosing) {
2805
+ this.push(indent + `</${tagName}>`);
2806
+ }
2807
+ return;
2808
+ }
2809
+ const inline = this.renderInlineOpen(tagName, attributes, isSelfClosing, inlineNodes, open.children);
2787
2810
  const singleAttribute = attributes[0];
2788
2811
  const hasEmptyValue = singleAttribute &&
2789
2812
  (singleAttribute.value instanceof HTMLAttributeValueNode || singleAttribute.value?.type === 'AST_HTML_ATTRIBUTE_VALUE_NODE') &&
2790
2813
  singleAttribute.value?.children.length === 0;
2791
- const shouldKeepInline = attributes.length <= 3 &&
2814
+ const shouldKeepInline = (attributes.length <= 3 &&
2792
2815
  !hasEmptyValue &&
2793
- inline.length + indent.length <= this.maxLineLength;
2816
+ inline.length + indent.length <= this.maxLineLength) ||
2817
+ inlineNodes.length > 0;
2794
2818
  if (shouldKeepInline) {
2795
2819
  if (children.length === 0) {
2796
2820
  if (isSelfClosing) {
@@ -2818,27 +2842,38 @@ class Printer extends Visitor {
2818
2842
  }
2819
2843
  return;
2820
2844
  }
2821
- this.push(indent + `<${tagName}`);
2822
- this.withIndent(() => {
2823
- attributes.forEach(attribute => {
2824
- this.push(this.indent() + this.renderAttribute(attribute));
2825
- });
2826
- });
2827
- if (isSelfClosing) {
2828
- this.push(indent + "/>");
2829
- }
2830
- else if (node.is_void) {
2831
- this.push(indent + ">");
2832
- }
2833
- else if (children.length === 0) {
2834
- this.push(indent + ">" + `</${tagName}>`);
2845
+ if (inlineNodes.length > 0) {
2846
+ this.push(indent + this.renderInlineOpen(tagName, attributes, isSelfClosing, inlineNodes, open.children));
2847
+ if (!isSelfClosing && !node.is_void && children.length > 0) {
2848
+ this.withIndent(() => {
2849
+ children.forEach(child => this.visit(child));
2850
+ });
2851
+ this.push(indent + `</${tagName}>`);
2852
+ }
2835
2853
  }
2836
2854
  else {
2837
- this.push(indent + ">");
2855
+ this.push(indent + `<${tagName}`);
2838
2856
  this.withIndent(() => {
2839
- children.forEach(child => this.visit(child));
2857
+ attributes.forEach(attribute => {
2858
+ this.push(this.indent() + this.renderAttribute(attribute));
2859
+ });
2840
2860
  });
2841
- this.push(indent + `</${tagName}>`);
2861
+ if (isSelfClosing) {
2862
+ this.push(indent + "/>");
2863
+ }
2864
+ else if (node.is_void) {
2865
+ this.push(indent + ">");
2866
+ }
2867
+ else if (children.length === 0) {
2868
+ this.push(indent + ">" + `</${tagName}>`);
2869
+ }
2870
+ else {
2871
+ this.push(indent + ">");
2872
+ this.withIndent(() => {
2873
+ children.forEach(child => this.visit(child));
2874
+ });
2875
+ this.push(indent + `</${tagName}>`);
2876
+ }
2842
2877
  }
2843
2878
  }
2844
2879
  visitHTMLOpenTagNode(node) {
@@ -3046,15 +3081,38 @@ class Printer extends Visitor {
3046
3081
  }
3047
3082
  }
3048
3083
  visitERBIfNode(node) {
3049
- this.printERBNode(node);
3050
- this.withIndent(() => {
3051
- node.statements.forEach(child => this.visit(child));
3052
- });
3053
- if (node.subsequent) {
3054
- this.visit(node.subsequent);
3084
+ if (this.inlineMode) {
3085
+ const open = node.tag_opening?.value ?? "";
3086
+ const content = node.content?.value ?? "";
3087
+ const close = node.tag_closing?.value ?? "";
3088
+ this.lines.push(open + content + close);
3089
+ node.statements.forEach(child => {
3090
+ if (child instanceof HTMLAttributeNode || child.type === 'AST_HTML_ATTRIBUTE_NODE') {
3091
+ this.lines.push(" " + this.renderAttribute(child) + " ");
3092
+ }
3093
+ else {
3094
+ this.visit(child);
3095
+ }
3096
+ });
3097
+ if (node.end_node) {
3098
+ const endNode = node.end_node;
3099
+ const endOpen = endNode.tag_opening?.value ?? "";
3100
+ const endContent = endNode.content?.value ?? "";
3101
+ const endClose = endNode.tag_closing?.value ?? "";
3102
+ this.lines.push(endOpen + endContent + endClose);
3103
+ }
3055
3104
  }
3056
- if (node.end_node) {
3057
- this.printERBNode(node.end_node);
3105
+ else {
3106
+ this.printERBNode(node);
3107
+ this.withIndent(() => {
3108
+ node.statements.forEach(child => this.visit(child));
3109
+ });
3110
+ if (node.subsequent) {
3111
+ this.visit(node.subsequent);
3112
+ }
3113
+ if (node.end_node) {
3114
+ this.printERBNode(node.end_node);
3115
+ }
3058
3116
  }
3059
3117
  }
3060
3118
  visitERBElseNode(node) {
@@ -3134,8 +3192,54 @@ class Printer extends Visitor {
3134
3192
  this.visit(node.end_node);
3135
3193
  }
3136
3194
  // --- Utility methods ---
3137
- renderInlineOpen(name, attributes, selfClose) {
3195
+ renderInlineOpen(name, attributes, selfClose, inlineNodes = [], allChildren = []) {
3138
3196
  const parts = attributes.map(attribute => this.renderAttribute(attribute));
3197
+ if (inlineNodes.length > 0) {
3198
+ let result = `<${name}`;
3199
+ if (allChildren.length > 0) {
3200
+ const currentIndentLevel = this.indentLevel;
3201
+ this.indentLevel = 0;
3202
+ const tempLines = this.lines;
3203
+ this.lines = [];
3204
+ allChildren.forEach(child => {
3205
+ if (child instanceof HTMLAttributeNode || child.type === 'AST_HTML_ATTRIBUTE_NODE') {
3206
+ this.lines.push(" " + this.renderAttribute(child));
3207
+ }
3208
+ else if (!(child instanceof WhitespaceNode || child.type === 'AST_WHITESPACE_NODE')) {
3209
+ const wasInlineMode = this.inlineMode;
3210
+ this.inlineMode = true;
3211
+ this.lines.push(" ");
3212
+ this.visit(child);
3213
+ this.inlineMode = wasInlineMode;
3214
+ }
3215
+ });
3216
+ const inlineContent = this.lines.join("");
3217
+ this.lines = tempLines;
3218
+ this.indentLevel = currentIndentLevel;
3219
+ result += inlineContent;
3220
+ }
3221
+ else {
3222
+ if (parts.length > 0) {
3223
+ result += ` ${parts.join(" ")}`;
3224
+ }
3225
+ const currentIndentLevel = this.indentLevel;
3226
+ this.indentLevel = 0;
3227
+ const tempLines = this.lines;
3228
+ this.lines = [];
3229
+ inlineNodes.forEach(node => {
3230
+ const wasInlineMode = this.inlineMode;
3231
+ this.inlineMode = true;
3232
+ this.visit(node);
3233
+ this.inlineMode = wasInlineMode;
3234
+ });
3235
+ const inlineContent = this.lines.join("");
3236
+ this.lines = tempLines;
3237
+ this.indentLevel = currentIndentLevel;
3238
+ result += inlineContent;
3239
+ }
3240
+ result += selfClose ? " />" : ">";
3241
+ return result;
3242
+ }
3139
3243
  return `<${name}${parts.length ? " " + parts.join(" ") : ""}${selfClose ? " /" : ""}>`;
3140
3244
  }
3141
3245
  renderAttribute(attribute) {
@@ -3147,8 +3251,7 @@ class Printer extends Visitor {
3147
3251
  const open_quote = (attrValue.open_quote?.value ?? "");
3148
3252
  const close_quote = (attrValue.close_quote?.value ?? "");
3149
3253
  const attribute_value = attrValue.children.map((attr) => {
3150
- if (attr instanceof HTMLTextNode || attr.type === 'AST_HTML_TEXT_NODE' ||
3151
- attr instanceof LiteralNode || attr.type === 'AST_LITERAL_NODE') {
3254
+ if (attr instanceof HTMLTextNode || attr.type === 'AST_HTML_TEXT_NODE' || attr instanceof LiteralNode || attr.type === 'AST_LITERAL_NODE') {
3152
3255
  return attr.content;
3153
3256
  }
3154
3257
  else if (attr instanceof ERBContentNode || attr.type === 'AST_ERB_CONTENT_NODE') {