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