@herb-tools/node 0.8.9 → 0.9.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.
Files changed (174) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/binding.gyp +26 -8
  3. package/dist/herb-node.cjs +41 -12
  4. package/dist/herb-node.cjs.map +1 -1
  5. package/dist/herb-node.esm.js +8 -1
  6. package/dist/herb-node.esm.js.map +1 -1
  7. package/dist/types/node-backend.d.ts +3 -1
  8. package/extension/error_helpers.cpp +419 -71
  9. package/extension/error_helpers.h +14 -3
  10. package/extension/extension_helpers.cpp +38 -35
  11. package/extension/extension_helpers.h +2 -2
  12. package/extension/herb.cpp +183 -64
  13. package/extension/libherb/analyze/action_view/attribute_extraction_helpers.c +290 -0
  14. package/extension/libherb/analyze/action_view/attribute_extraction_helpers.h +36 -0
  15. package/extension/libherb/analyze/action_view/content_tag.c +70 -0
  16. package/extension/libherb/analyze/action_view/link_to.c +143 -0
  17. package/extension/libherb/analyze/action_view/registry.c +60 -0
  18. package/extension/libherb/analyze/action_view/tag.c +64 -0
  19. package/extension/libherb/analyze/action_view/tag_helper_handler.h +41 -0
  20. package/extension/libherb/analyze/action_view/tag_helper_node_builders.c +305 -0
  21. package/extension/libherb/analyze/action_view/tag_helper_node_builders.h +70 -0
  22. package/extension/libherb/analyze/action_view/tag_helpers.c +748 -0
  23. package/extension/libherb/analyze/action_view/tag_helpers.h +38 -0
  24. package/extension/libherb/analyze/action_view/turbo_frame_tag.c +88 -0
  25. package/extension/libherb/analyze/analyze.c +882 -0
  26. package/extension/libherb/{include → analyze}/analyze.h +14 -4
  27. package/extension/libherb/{analyzed_ruby.c → analyze/analyzed_ruby.c} +13 -11
  28. package/extension/libherb/{analyzed_ruby.h → analyze/analyzed_ruby.h} +3 -3
  29. package/extension/libherb/analyze/builders.c +343 -0
  30. package/extension/libherb/analyze/builders.h +27 -0
  31. package/extension/libherb/analyze/conditional_elements.c +594 -0
  32. package/extension/libherb/analyze/conditional_elements.h +9 -0
  33. package/extension/libherb/analyze/conditional_open_tags.c +640 -0
  34. package/extension/libherb/analyze/conditional_open_tags.h +9 -0
  35. package/extension/libherb/analyze/control_type.c +250 -0
  36. package/extension/libherb/analyze/control_type.h +14 -0
  37. package/extension/libherb/{analyze_helpers.c → analyze/helpers.c} +79 -31
  38. package/extension/libherb/{analyze_helpers.h → analyze/helpers.h} +22 -17
  39. package/extension/libherb/analyze/invalid_structures.c +193 -0
  40. package/extension/libherb/analyze/invalid_structures.h +11 -0
  41. package/extension/libherb/{analyze_missing_end.c → analyze/missing_end.c} +33 -22
  42. package/extension/libherb/analyze/parse_errors.c +84 -0
  43. package/extension/libherb/analyze/prism_annotate.c +397 -0
  44. package/extension/libherb/analyze/prism_annotate.h +16 -0
  45. package/extension/libherb/{analyze_transform.c → analyze/transform.c} +17 -3
  46. package/extension/libherb/ast_node.c +17 -7
  47. package/extension/libherb/ast_node.h +11 -5
  48. package/extension/libherb/ast_nodes.c +663 -388
  49. package/extension/libherb/ast_nodes.h +118 -39
  50. package/extension/libherb/ast_pretty_print.c +191 -7
  51. package/extension/libherb/ast_pretty_print.h +6 -1
  52. package/extension/libherb/element_source.h +3 -8
  53. package/extension/libherb/errors.c +1100 -507
  54. package/extension/libherb/errors.h +155 -54
  55. package/extension/libherb/extract.c +148 -49
  56. package/extension/libherb/extract.h +21 -5
  57. package/extension/libherb/herb.c +52 -34
  58. package/extension/libherb/herb.h +18 -6
  59. package/extension/libherb/herb_prism_node.h +13 -0
  60. package/extension/libherb/html_util.c +241 -12
  61. package/extension/libherb/html_util.h +7 -2
  62. package/extension/libherb/include/analyze/action_view/attribute_extraction_helpers.h +36 -0
  63. package/extension/libherb/include/analyze/action_view/tag_helper_handler.h +41 -0
  64. package/extension/libherb/include/analyze/action_view/tag_helper_node_builders.h +70 -0
  65. package/extension/libherb/include/analyze/action_view/tag_helpers.h +38 -0
  66. package/extension/libherb/{analyze.h → include/analyze/analyze.h} +14 -4
  67. package/extension/libherb/include/{analyzed_ruby.h → analyze/analyzed_ruby.h} +3 -3
  68. package/extension/libherb/include/analyze/builders.h +27 -0
  69. package/extension/libherb/include/analyze/conditional_elements.h +9 -0
  70. package/extension/libherb/include/analyze/conditional_open_tags.h +9 -0
  71. package/extension/libherb/include/analyze/control_type.h +14 -0
  72. package/extension/libherb/include/{analyze_helpers.h → analyze/helpers.h} +22 -17
  73. package/extension/libherb/include/analyze/invalid_structures.h +11 -0
  74. package/extension/libherb/include/analyze/prism_annotate.h +16 -0
  75. package/extension/libherb/include/ast_node.h +11 -5
  76. package/extension/libherb/include/ast_nodes.h +118 -39
  77. package/extension/libherb/include/ast_pretty_print.h +6 -1
  78. package/extension/libherb/include/element_source.h +3 -8
  79. package/extension/libherb/include/errors.h +155 -54
  80. package/extension/libherb/include/extract.h +21 -5
  81. package/extension/libherb/include/herb.h +18 -6
  82. package/extension/libherb/include/herb_prism_node.h +13 -0
  83. package/extension/libherb/include/html_util.h +7 -2
  84. package/extension/libherb/include/io.h +3 -1
  85. package/extension/libherb/include/lex_helpers.h +29 -0
  86. package/extension/libherb/include/lexer.h +1 -1
  87. package/extension/libherb/include/lexer_peek_helpers.h +87 -13
  88. package/extension/libherb/include/lexer_struct.h +2 -0
  89. package/extension/libherb/include/location.h +2 -1
  90. package/extension/libherb/include/parser.h +27 -2
  91. package/extension/libherb/include/parser_helpers.h +19 -3
  92. package/extension/libherb/include/pretty_print.h +10 -5
  93. package/extension/libherb/include/prism_context.h +45 -0
  94. package/extension/libherb/include/prism_helpers.h +10 -7
  95. package/extension/libherb/include/prism_serialized.h +12 -0
  96. package/extension/libherb/include/token.h +16 -4
  97. package/extension/libherb/include/token_struct.h +10 -3
  98. package/extension/libherb/include/utf8.h +2 -1
  99. package/extension/libherb/include/util/hb_allocator.h +78 -0
  100. package/extension/libherb/include/util/hb_arena.h +6 -1
  101. package/extension/libherb/include/util/hb_arena_debug.h +12 -1
  102. package/extension/libherb/include/util/hb_array.h +7 -3
  103. package/extension/libherb/include/util/hb_buffer.h +6 -4
  104. package/extension/libherb/include/util/hb_foreach.h +79 -0
  105. package/extension/libherb/include/util/hb_narray.h +8 -4
  106. package/extension/libherb/include/util/hb_string.h +56 -9
  107. package/extension/libherb/include/util/string.h +11 -0
  108. package/extension/libherb/include/util.h +6 -3
  109. package/extension/libherb/include/version.h +1 -1
  110. package/extension/libherb/io.c +3 -2
  111. package/extension/libherb/io.h +3 -1
  112. package/extension/libherb/lex_helpers.h +29 -0
  113. package/extension/libherb/lexer.c +42 -30
  114. package/extension/libherb/lexer.h +1 -1
  115. package/extension/libherb/lexer_peek_helpers.c +12 -74
  116. package/extension/libherb/lexer_peek_helpers.h +87 -13
  117. package/extension/libherb/lexer_struct.h +2 -0
  118. package/extension/libherb/location.c +2 -2
  119. package/extension/libherb/location.h +2 -1
  120. package/extension/libherb/main.c +79 -66
  121. package/extension/libherb/parser.c +784 -247
  122. package/extension/libherb/parser.h +27 -2
  123. package/extension/libherb/parser_helpers.c +110 -23
  124. package/extension/libherb/parser_helpers.h +19 -3
  125. package/extension/libherb/parser_match_tags.c +110 -49
  126. package/extension/libherb/pretty_print.c +29 -24
  127. package/extension/libherb/pretty_print.h +10 -5
  128. package/extension/libherb/prism_context.h +45 -0
  129. package/extension/libherb/prism_helpers.c +30 -27
  130. package/extension/libherb/prism_helpers.h +10 -7
  131. package/extension/libherb/prism_serialized.h +12 -0
  132. package/extension/libherb/ruby_parser.c +2 -0
  133. package/extension/libherb/token.c +151 -66
  134. package/extension/libherb/token.h +16 -4
  135. package/extension/libherb/token_matchers.c +0 -1
  136. package/extension/libherb/token_struct.h +10 -3
  137. package/extension/libherb/utf8.c +7 -6
  138. package/extension/libherb/utf8.h +2 -1
  139. package/extension/libherb/util/hb_allocator.c +341 -0
  140. package/extension/libherb/util/hb_allocator.h +78 -0
  141. package/extension/libherb/util/hb_arena.c +81 -56
  142. package/extension/libherb/util/hb_arena.h +6 -1
  143. package/extension/libherb/util/hb_arena_debug.c +32 -17
  144. package/extension/libherb/util/hb_arena_debug.h +12 -1
  145. package/extension/libherb/util/hb_array.c +30 -15
  146. package/extension/libherb/util/hb_array.h +7 -3
  147. package/extension/libherb/util/hb_buffer.c +17 -21
  148. package/extension/libherb/util/hb_buffer.h +6 -4
  149. package/extension/libherb/util/hb_foreach.h +79 -0
  150. package/extension/libherb/util/hb_narray.c +22 -7
  151. package/extension/libherb/util/hb_narray.h +8 -4
  152. package/extension/libherb/util/hb_string.c +49 -35
  153. package/extension/libherb/util/hb_string.h +56 -9
  154. package/extension/libherb/util/string.h +11 -0
  155. package/extension/libherb/util.c +21 -11
  156. package/extension/libherb/util.h +6 -3
  157. package/extension/libherb/version.h +1 -1
  158. package/extension/libherb/visitor.c +48 -1
  159. package/extension/nodes.cpp +451 -6
  160. package/extension/nodes.h +8 -1
  161. package/extension/prism/include/prism/ast.h +4 -4
  162. package/extension/prism/include/prism/version.h +2 -2
  163. package/extension/prism/src/prism.c +1 -1
  164. package/package.json +12 -8
  165. package/src/node-backend.ts +11 -1
  166. package/dist/types/index-cjs.d.cts +0 -1
  167. package/extension/libherb/analyze.c +0 -1594
  168. package/extension/libherb/element_source.c +0 -12
  169. package/extension/libherb/include/util/hb_system.h +0 -9
  170. package/extension/libherb/util/hb_system.c +0 -30
  171. package/extension/libherb/util/hb_system.h +0 -9
  172. package/src/index-cjs.cts +0 -22
  173. /package/dist/types/{index-esm.d.mts → index.d.ts} +0 -0
  174. /package/src/{index-esm.mts → index.ts} +0 -0
@@ -0,0 +1,594 @@
1
+ #include "../include/analyze/conditional_elements.h"
2
+ #include "../include/ast_nodes.h"
3
+ #include "../include/element_source.h"
4
+ #include "../include/errors.h"
5
+ #include "../include/token_struct.h"
6
+ #include "../include/util/hb_allocator.h"
7
+ #include "../include/util/hb_array.h"
8
+ #include "../include/util/hb_string.h"
9
+ #include "../include/visitor.h"
10
+
11
+ #include <stdbool.h>
12
+ #include <stdlib.h>
13
+ #include <string.h>
14
+
15
+ typedef struct {
16
+ hb_array_T* document_errors;
17
+ hb_allocator_T* allocator;
18
+ } conditional_elements_context_T;
19
+
20
+ static hb_string_T extract_condition_from_erb_content(AST_NODE_T* erb_node, bool* is_if) {
21
+ if (!erb_node) { return HB_STRING_NULL; }
22
+
23
+ token_T* content_token = NULL;
24
+
25
+ if (erb_node->type == AST_ERB_IF_NODE) {
26
+ AST_ERB_IF_NODE_T* if_node = (AST_ERB_IF_NODE_T*) erb_node;
27
+ content_token = if_node->content;
28
+ *is_if = true;
29
+ } else if (erb_node->type == AST_ERB_UNLESS_NODE) {
30
+ AST_ERB_UNLESS_NODE_T* unless_node = (AST_ERB_UNLESS_NODE_T*) erb_node;
31
+ content_token = unless_node->content;
32
+ *is_if = false;
33
+ } else {
34
+ return HB_STRING_NULL;
35
+ }
36
+
37
+ if (!content_token || hb_string_is_empty(content_token->value)) { return HB_STRING_NULL; }
38
+
39
+ hb_string_T value = hb_string_trim_start(content_token->value);
40
+
41
+ if (*is_if) {
42
+ if (hb_string_starts_with(value, hb_string("if "))) { value = hb_string_slice(value, 3); }
43
+ } else {
44
+ if (hb_string_starts_with(value, hb_string("unless "))) { value = hb_string_slice(value, 7); }
45
+ }
46
+
47
+ value = hb_string_trim(value);
48
+
49
+ if (hb_string_is_empty(value)) { return HB_STRING_NULL; }
50
+
51
+ return value;
52
+ }
53
+
54
+ static bool is_simple_erb_conditional(AST_NODE_T* node) {
55
+ if (node->type == AST_ERB_IF_NODE) {
56
+ AST_ERB_IF_NODE_T* if_node = (AST_ERB_IF_NODE_T*) node;
57
+ return if_node->subsequent == NULL;
58
+ } else if (node->type == AST_ERB_UNLESS_NODE) {
59
+ AST_ERB_UNLESS_NODE_T* unless_node = (AST_ERB_UNLESS_NODE_T*) node;
60
+ return unless_node->else_clause == NULL;
61
+ }
62
+
63
+ return false;
64
+ }
65
+
66
+ static hb_array_T* get_erb_conditional_statements(AST_NODE_T* node) {
67
+ if (node->type == AST_ERB_IF_NODE) {
68
+ return ((AST_ERB_IF_NODE_T*) node)->statements;
69
+ } else if (node->type == AST_ERB_UNLESS_NODE) {
70
+ return ((AST_ERB_UNLESS_NODE_T*) node)->statements;
71
+ }
72
+
73
+ return NULL;
74
+ }
75
+
76
+ static bool contains_single_open_tag(hb_array_T* statements, AST_HTML_OPEN_TAG_NODE_T** out_tag) {
77
+ if (!statements || hb_array_size(statements) == 0) { return false; }
78
+
79
+ *out_tag = NULL;
80
+ size_t tag_count = 0;
81
+
82
+ for (size_t statement_index = 0; statement_index < hb_array_size(statements); statement_index++) {
83
+ AST_NODE_T* child = (AST_NODE_T*) hb_array_get(statements, statement_index);
84
+
85
+ if (!child) { continue; }
86
+ if (child->type == AST_WHITESPACE_NODE) { continue; }
87
+
88
+ if (child->type == AST_HTML_TEXT_NODE) {
89
+ AST_HTML_TEXT_NODE_T* text = (AST_HTML_TEXT_NODE_T*) child;
90
+ if (hb_string_is_blank(text->content)) { continue; }
91
+
92
+ return false;
93
+ }
94
+
95
+ if (child->type == AST_HTML_OPEN_TAG_NODE) {
96
+ AST_HTML_OPEN_TAG_NODE_T* open_tag = (AST_HTML_OPEN_TAG_NODE_T*) child;
97
+
98
+ if (open_tag->is_void) { return false; }
99
+
100
+ *out_tag = open_tag;
101
+ tag_count++;
102
+
103
+ if (tag_count > 1) { return false; }
104
+
105
+ continue;
106
+ }
107
+
108
+ return false;
109
+ }
110
+
111
+ return tag_count == 1;
112
+ }
113
+
114
+ static bool contains_single_close_tag(hb_array_T* statements, AST_HTML_CLOSE_TAG_NODE_T** out_tag) {
115
+ if (!statements || hb_array_size(statements) == 0) { return false; }
116
+
117
+ *out_tag = NULL;
118
+ size_t tag_count = 0;
119
+
120
+ for (size_t statement_index = 0; statement_index < hb_array_size(statements); statement_index++) {
121
+ AST_NODE_T* child = (AST_NODE_T*) hb_array_get(statements, statement_index);
122
+ if (!child) { continue; }
123
+
124
+ if (child->type == AST_WHITESPACE_NODE) { continue; }
125
+
126
+ if (child->type == AST_HTML_TEXT_NODE) {
127
+ AST_HTML_TEXT_NODE_T* text = (AST_HTML_TEXT_NODE_T*) child;
128
+ if (hb_string_is_blank(text->content)) { continue; }
129
+
130
+ return false;
131
+ }
132
+
133
+ if (child->type == AST_HTML_CLOSE_TAG_NODE) {
134
+ *out_tag = (AST_HTML_CLOSE_TAG_NODE_T*) child;
135
+ tag_count++;
136
+
137
+ if (tag_count > 1) { return false; }
138
+
139
+ continue;
140
+ }
141
+
142
+ return false;
143
+ }
144
+
145
+ return tag_count == 1;
146
+ }
147
+
148
+ static size_t count_nodes_of_type(hb_array_T* array, ast_node_type_T type) {
149
+ if (!array || hb_array_size(array) == 0) { return 0; }
150
+
151
+ size_t count = 0;
152
+
153
+ for (size_t index = 0; index < hb_array_size(array); index++) {
154
+ AST_NODE_T* node = (AST_NODE_T*) hb_array_get(array, index);
155
+ if (!node) { continue; }
156
+
157
+ if (node->type == type) {
158
+ if (type == AST_HTML_OPEN_TAG_NODE) {
159
+ AST_HTML_OPEN_TAG_NODE_T* open_tag = (AST_HTML_OPEN_TAG_NODE_T*) node;
160
+ if (!open_tag->is_void) { count++; }
161
+ } else {
162
+ count++;
163
+ }
164
+ }
165
+ }
166
+
167
+ return count;
168
+ }
169
+
170
+ static bool conditions_are_equivalent(hb_string_T a, hb_string_T b) {
171
+ if (hb_string_is_empty(a) || hb_string_is_empty(b)) { return false; }
172
+
173
+ return hb_string_equals(a, b);
174
+ }
175
+
176
+ typedef struct {
177
+ size_t open_index;
178
+ AST_NODE_T* open_conditional;
179
+ AST_HTML_OPEN_TAG_NODE_T* open_tag;
180
+ hb_string_T tag_name;
181
+ hb_string_T condition;
182
+ bool is_if;
183
+ } conditional_open_tag_T;
184
+
185
+ static void rewrite_conditional_elements(hb_array_T* nodes, hb_array_T* document_errors, hb_allocator_T* allocator) {
186
+ if (!nodes || hb_array_size(nodes) == 0) { return; }
187
+ if (!document_errors) { return; }
188
+
189
+ for (size_t open_index = 0; open_index < hb_array_size(nodes); open_index++) {
190
+ AST_NODE_T* open_node = (AST_NODE_T*) hb_array_get(nodes, open_index);
191
+
192
+ if (!open_node) { continue; }
193
+ if (open_node->type != AST_ERB_IF_NODE && open_node->type != AST_ERB_UNLESS_NODE) { continue; }
194
+ if (!is_simple_erb_conditional(open_node)) { continue; }
195
+
196
+ hb_array_T* open_statements = get_erb_conditional_statements(open_node);
197
+
198
+ size_t open_tag_count = count_nodes_of_type(open_statements, AST_HTML_OPEN_TAG_NODE);
199
+ if (open_tag_count < 2) { continue; }
200
+
201
+ size_t open_close_tag_count = count_nodes_of_type(open_statements, AST_HTML_CLOSE_TAG_NODE);
202
+ if (open_tag_count <= open_close_tag_count) { continue; }
203
+
204
+ bool open_is_if;
205
+ hb_string_T open_condition = extract_condition_from_erb_content(open_node, &open_is_if);
206
+
207
+ if (hb_string_is_null(open_condition)) { continue; }
208
+
209
+ for (size_t close_index = open_index + 1; close_index < hb_array_size(nodes); close_index++) {
210
+ AST_NODE_T* close_node = (AST_NODE_T*) hb_array_get(nodes, close_index);
211
+
212
+ if (!close_node) { continue; }
213
+ if (close_node->type != AST_ERB_IF_NODE && close_node->type != AST_ERB_UNLESS_NODE) { continue; }
214
+ if (!is_simple_erb_conditional(close_node)) { continue; }
215
+
216
+ hb_array_T* close_statements = get_erb_conditional_statements(close_node);
217
+
218
+ size_t close_tag_count = count_nodes_of_type(close_statements, AST_HTML_CLOSE_TAG_NODE);
219
+ if (close_tag_count < 2) { continue; }
220
+
221
+ size_t close_open_tag_count = count_nodes_of_type(close_statements, AST_HTML_OPEN_TAG_NODE);
222
+ if (close_tag_count <= close_open_tag_count) { continue; }
223
+
224
+ bool close_is_if;
225
+ hb_string_T close_condition = extract_condition_from_erb_content(close_node, &close_is_if);
226
+
227
+ if (hb_string_is_null(close_condition)) { continue; }
228
+
229
+ if (open_is_if == close_is_if && conditions_are_equivalent(open_condition, close_condition)) {
230
+ CONDITIONAL_ELEMENT_MULTIPLE_TAGS_ERROR_T* multiple_tags_error = conditional_element_multiple_tags_error_init(
231
+ open_node->location.start.line,
232
+ open_node->location.start.column,
233
+ open_node->location.start,
234
+ open_node->location.end,
235
+ allocator
236
+ );
237
+
238
+ hb_array_append(document_errors, multiple_tags_error);
239
+ break;
240
+ }
241
+ }
242
+ }
243
+
244
+ hb_array_T* open_stack = hb_array_init(8, allocator);
245
+
246
+ for (size_t node_index = 0; node_index < hb_array_size(nodes); node_index++) {
247
+ AST_NODE_T* node = (AST_NODE_T*) hb_array_get(nodes, node_index);
248
+
249
+ if (!node) { continue; }
250
+ if (node->type != AST_ERB_IF_NODE && node->type != AST_ERB_UNLESS_NODE) { continue; }
251
+ if (!is_simple_erb_conditional(node)) { continue; }
252
+
253
+ hb_array_T* statements = get_erb_conditional_statements(node);
254
+ AST_HTML_OPEN_TAG_NODE_T* open_tag = NULL;
255
+
256
+ if (contains_single_open_tag(statements, &open_tag)) {
257
+ conditional_open_tag_T* entry = malloc(sizeof(conditional_open_tag_T));
258
+
259
+ if (!entry) { continue; }
260
+
261
+ entry->open_index = node_index;
262
+ entry->open_conditional = node;
263
+ entry->open_tag = open_tag;
264
+ entry->tag_name = open_tag->tag_name->value;
265
+
266
+ bool is_if;
267
+ entry->condition = extract_condition_from_erb_content(node, &is_if);
268
+ entry->is_if = is_if;
269
+
270
+ hb_array_append(open_stack, entry);
271
+ }
272
+ }
273
+
274
+ hb_array_T* consumed_indices = hb_array_init(8, allocator);
275
+
276
+ for (size_t node_index = 0; node_index < hb_array_size(nodes); node_index++) {
277
+ AST_NODE_T* node = (AST_NODE_T*) hb_array_get(nodes, node_index);
278
+
279
+ if (!node) { continue; }
280
+ if (node->type != AST_ERB_IF_NODE && node->type != AST_ERB_UNLESS_NODE) { continue; }
281
+ if (!is_simple_erb_conditional(node)) { continue; }
282
+
283
+ hb_array_T* statements = get_erb_conditional_statements(node);
284
+ AST_HTML_CLOSE_TAG_NODE_T* close_tag = NULL;
285
+
286
+ if (!contains_single_close_tag(statements, &close_tag)) { continue; }
287
+
288
+ conditional_open_tag_T* matched_open = NULL;
289
+ size_t matched_stack_index = (size_t) -1;
290
+
291
+ conditional_open_tag_T* mismatched_open = NULL;
292
+ hb_string_T mismatched_close_condition = HB_STRING_NULL;
293
+
294
+ for (size_t stack_index = hb_array_size(open_stack); stack_index > 0; stack_index--) {
295
+ conditional_open_tag_T* entry = (conditional_open_tag_T*) hb_array_get(open_stack, stack_index - 1);
296
+
297
+ if (!entry) { continue; }
298
+ if (!hb_string_equals_case_insensitive(entry->tag_name, close_tag->tag_name->value)) { continue; }
299
+
300
+ bool close_is_if;
301
+ hb_string_T close_condition = extract_condition_from_erb_content(node, &close_is_if);
302
+
303
+ if (entry->is_if != close_is_if) { continue; }
304
+
305
+ if (!conditions_are_equivalent(entry->condition, close_condition)) {
306
+ if (!mismatched_open && entry->open_index < node_index) {
307
+ mismatched_open = entry;
308
+ mismatched_close_condition = close_condition;
309
+ }
310
+
311
+ continue;
312
+ }
313
+
314
+ if (!hb_string_is_null(mismatched_close_condition)) {
315
+ mismatched_close_condition = HB_STRING_NULL;
316
+ mismatched_open = NULL;
317
+ }
318
+
319
+ if (entry->open_index >= node_index) { continue; }
320
+
321
+ matched_open = entry;
322
+ matched_stack_index = stack_index - 1;
323
+
324
+ break;
325
+ }
326
+
327
+ if (!matched_open && mismatched_open && !hb_string_is_null(mismatched_close_condition)) {
328
+ CONDITIONAL_ELEMENT_CONDITION_MISMATCH_ERROR_T* mismatch_error =
329
+ conditional_element_condition_mismatch_error_init(
330
+ mismatched_open->tag_name,
331
+ mismatched_open->condition,
332
+ mismatched_open->open_conditional->location.start.line,
333
+ mismatched_open->open_conditional->location.start.column,
334
+ mismatched_close_condition,
335
+ node->location.start.line,
336
+ node->location.start.column,
337
+ mismatched_open->open_conditional->location.start,
338
+ node->location.end,
339
+ allocator
340
+ );
341
+
342
+ hb_array_append(document_errors, mismatch_error);
343
+ continue;
344
+ }
345
+
346
+ if (!matched_open) { continue; }
347
+
348
+ hb_array_T* body = hb_array_init(8, allocator);
349
+
350
+ for (size_t body_index = matched_open->open_index + 1; body_index < node_index; body_index++) {
351
+ AST_NODE_T* body_node = (AST_NODE_T*) hb_array_get(nodes, body_index);
352
+
353
+ if (body_node) { hb_array_append(body, body_node); }
354
+ }
355
+
356
+ position_T start_position = matched_open->open_conditional->location.start;
357
+ position_T end_position = node->location.end;
358
+ hb_array_T* errors = hb_array_init(0, allocator);
359
+
360
+ AST_HTML_CONDITIONAL_ELEMENT_NODE_T* conditional_element = ast_html_conditional_element_node_init(
361
+ matched_open->condition,
362
+ matched_open->open_conditional,
363
+ matched_open->open_tag,
364
+ body,
365
+ (AST_NODE_T*) close_tag,
366
+ node,
367
+ matched_open->open_tag->tag_name,
368
+ ELEMENT_SOURCE_HTML,
369
+ start_position,
370
+ end_position,
371
+ errors,
372
+ allocator
373
+ );
374
+
375
+ for (size_t body_index = matched_open->open_index + 1; body_index < node_index; body_index++) {
376
+ size_t* consumed_index = malloc(sizeof(size_t));
377
+
378
+ if (consumed_index) {
379
+ *consumed_index = body_index;
380
+ hb_array_append(consumed_indices, consumed_index);
381
+ }
382
+
383
+ hb_array_set(nodes, body_index, NULL);
384
+ }
385
+
386
+ hb_array_set(nodes, matched_open->open_index, conditional_element);
387
+
388
+ size_t* close_index = malloc(sizeof(size_t));
389
+
390
+ if (close_index) {
391
+ *close_index = node_index;
392
+ hb_array_append(consumed_indices, close_index);
393
+ }
394
+ hb_array_set(nodes, node_index, NULL);
395
+
396
+ free(matched_open);
397
+ hb_array_set(open_stack, matched_stack_index, NULL);
398
+ }
399
+
400
+ for (size_t stack_index = 0; stack_index < hb_array_size(open_stack); stack_index++) {
401
+ conditional_open_tag_T* entry = (conditional_open_tag_T*) hb_array_get(open_stack, stack_index);
402
+
403
+ if (entry) { free(entry); }
404
+ }
405
+
406
+ hb_array_free(&open_stack);
407
+
408
+ if (hb_array_size(consumed_indices) > 0) {
409
+ hb_array_T* new_nodes = hb_array_init(hb_array_size(nodes), allocator);
410
+
411
+ for (size_t node_index = 0; node_index < hb_array_size(nodes); node_index++) {
412
+ bool consumed = false;
413
+
414
+ for (size_t consumed_index = 0; consumed_index < hb_array_size(consumed_indices); consumed_index++) {
415
+ size_t* stored_index = (size_t*) hb_array_get(consumed_indices, consumed_index);
416
+
417
+ if (stored_index && *stored_index == node_index) {
418
+ consumed = true;
419
+ break;
420
+ }
421
+ }
422
+
423
+ if (!consumed) {
424
+ AST_NODE_T* node = (AST_NODE_T*) hb_array_get(nodes, node_index);
425
+ if (node) { hb_array_append(new_nodes, node); }
426
+ }
427
+ }
428
+
429
+ nodes->size = 0;
430
+
431
+ for (size_t new_index = 0; new_index < hb_array_size(new_nodes); new_index++) {
432
+ hb_array_append(nodes, hb_array_get(new_nodes, new_index));
433
+ }
434
+
435
+ hb_array_free(&new_nodes);
436
+ }
437
+
438
+ for (size_t i = 0; i < hb_array_size(consumed_indices); i++) {
439
+ size_t* index = (size_t*) hb_array_get(consumed_indices, i);
440
+
441
+ if (index) { free(index); }
442
+ }
443
+
444
+ hb_array_free(&consumed_indices);
445
+ }
446
+
447
+ static bool transform_conditional_elements_visitor(const AST_NODE_T* node, void* data);
448
+
449
+ static void transform_conditional_elements_in_array(hb_array_T* array, conditional_elements_context_T* context) {
450
+ if (!array) { return; }
451
+
452
+ for (size_t i = 0; i < hb_array_size(array); i++) {
453
+ AST_NODE_T* child = (AST_NODE_T*) hb_array_get(array, i);
454
+
455
+ if (child) { herb_visit_node(child, transform_conditional_elements_visitor, context); }
456
+ }
457
+
458
+ rewrite_conditional_elements(array, context->document_errors, context->allocator);
459
+ }
460
+
461
+ static bool transform_conditional_elements_visitor(const AST_NODE_T* node, void* data) {
462
+ if (!node) { return false; }
463
+
464
+ conditional_elements_context_T* context = (conditional_elements_context_T*) data;
465
+
466
+ switch (node->type) {
467
+ case AST_DOCUMENT_NODE: {
468
+ AST_DOCUMENT_NODE_T* doc = (AST_DOCUMENT_NODE_T*) node;
469
+ transform_conditional_elements_in_array(doc->children, context);
470
+ return false;
471
+ }
472
+
473
+ case AST_HTML_ELEMENT_NODE: {
474
+ AST_HTML_ELEMENT_NODE_T* element = (AST_HTML_ELEMENT_NODE_T*) node;
475
+ transform_conditional_elements_in_array(element->body, context);
476
+ return false;
477
+ }
478
+
479
+ case AST_ERB_IF_NODE: {
480
+ AST_ERB_IF_NODE_T* if_node = (AST_ERB_IF_NODE_T*) node;
481
+ transform_conditional_elements_in_array(if_node->statements, context);
482
+
483
+ if (if_node->subsequent) { herb_visit_node(if_node->subsequent, transform_conditional_elements_visitor, data); }
484
+
485
+ return false;
486
+ }
487
+
488
+ case AST_ERB_ELSE_NODE: {
489
+ AST_ERB_ELSE_NODE_T* else_node = (AST_ERB_ELSE_NODE_T*) node;
490
+ transform_conditional_elements_in_array(else_node->statements, context);
491
+ return false;
492
+ }
493
+
494
+ case AST_ERB_UNLESS_NODE: {
495
+ AST_ERB_UNLESS_NODE_T* unless_node = (AST_ERB_UNLESS_NODE_T*) node;
496
+ transform_conditional_elements_in_array(unless_node->statements, context);
497
+
498
+ if (unless_node->else_clause) {
499
+ herb_visit_node((AST_NODE_T*) unless_node->else_clause, transform_conditional_elements_visitor, data);
500
+ }
501
+
502
+ return false;
503
+ }
504
+
505
+ case AST_ERB_BLOCK_NODE: {
506
+ AST_ERB_BLOCK_NODE_T* block_node = (AST_ERB_BLOCK_NODE_T*) node;
507
+ transform_conditional_elements_in_array(block_node->body, context);
508
+ return false;
509
+ }
510
+
511
+ case AST_ERB_WHILE_NODE: {
512
+ AST_ERB_WHILE_NODE_T* while_node = (AST_ERB_WHILE_NODE_T*) node;
513
+ transform_conditional_elements_in_array(while_node->statements, context);
514
+ return false;
515
+ }
516
+
517
+ case AST_ERB_UNTIL_NODE: {
518
+ AST_ERB_UNTIL_NODE_T* until_node = (AST_ERB_UNTIL_NODE_T*) node;
519
+ transform_conditional_elements_in_array(until_node->statements, context);
520
+ return false;
521
+ }
522
+
523
+ case AST_ERB_FOR_NODE: {
524
+ AST_ERB_FOR_NODE_T* for_node = (AST_ERB_FOR_NODE_T*) node;
525
+ transform_conditional_elements_in_array(for_node->statements, context);
526
+ return false;
527
+ }
528
+
529
+ case AST_ERB_CASE_NODE: {
530
+ AST_ERB_CASE_NODE_T* case_node = (AST_ERB_CASE_NODE_T*) node;
531
+ transform_conditional_elements_in_array(case_node->children, context);
532
+
533
+ for (size_t i = 0; i < hb_array_size(case_node->conditions); i++) {
534
+ AST_NODE_T* when = (AST_NODE_T*) hb_array_get(case_node->conditions, i);
535
+ if (when) { herb_visit_node(when, transform_conditional_elements_visitor, data); }
536
+ }
537
+
538
+ if (case_node->else_clause) {
539
+ herb_visit_node((AST_NODE_T*) case_node->else_clause, transform_conditional_elements_visitor, data);
540
+ }
541
+
542
+ return false;
543
+ }
544
+
545
+ case AST_ERB_WHEN_NODE: {
546
+ AST_ERB_WHEN_NODE_T* when_node = (AST_ERB_WHEN_NODE_T*) node;
547
+ transform_conditional_elements_in_array(when_node->statements, context);
548
+ return false;
549
+ }
550
+
551
+ case AST_ERB_BEGIN_NODE: {
552
+ AST_ERB_BEGIN_NODE_T* begin_node = (AST_ERB_BEGIN_NODE_T*) node;
553
+ transform_conditional_elements_in_array(begin_node->statements, context);
554
+
555
+ if (begin_node->rescue_clause) {
556
+ herb_visit_node((AST_NODE_T*) begin_node->rescue_clause, transform_conditional_elements_visitor, data);
557
+ }
558
+
559
+ if (begin_node->else_clause) {
560
+ herb_visit_node((AST_NODE_T*) begin_node->else_clause, transform_conditional_elements_visitor, data);
561
+ }
562
+
563
+ if (begin_node->ensure_clause) {
564
+ herb_visit_node((AST_NODE_T*) begin_node->ensure_clause, transform_conditional_elements_visitor, data);
565
+ }
566
+
567
+ return false;
568
+ }
569
+
570
+ case AST_ERB_RESCUE_NODE: {
571
+ AST_ERB_RESCUE_NODE_T* rescue_node = (AST_ERB_RESCUE_NODE_T*) node;
572
+ transform_conditional_elements_in_array(rescue_node->statements, context);
573
+
574
+ if (rescue_node->subsequent) {
575
+ herb_visit_node((AST_NODE_T*) rescue_node->subsequent, transform_conditional_elements_visitor, data);
576
+ }
577
+
578
+ return false;
579
+ }
580
+
581
+ case AST_ERB_ENSURE_NODE: {
582
+ AST_ERB_ENSURE_NODE_T* ensure_node = (AST_ERB_ENSURE_NODE_T*) node;
583
+ transform_conditional_elements_in_array(ensure_node->statements, context);
584
+ return false;
585
+ }
586
+
587
+ default: return true;
588
+ }
589
+ }
590
+
591
+ void herb_transform_conditional_elements(AST_DOCUMENT_NODE_T* document, hb_allocator_T* allocator) {
592
+ conditional_elements_context_T context = { .document_errors = document->base.errors, .allocator = allocator };
593
+ herb_visit_node((AST_NODE_T*) document, transform_conditional_elements_visitor, &context);
594
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef HERB_ANALYZE_CONDITIONAL_ELEMENTS_H
2
+ #define HERB_ANALYZE_CONDITIONAL_ELEMENTS_H
3
+
4
+ #include "../ast_nodes.h"
5
+ #include "../util/hb_allocator.h"
6
+
7
+ void herb_transform_conditional_elements(AST_DOCUMENT_NODE_T* document, hb_allocator_T* allocator);
8
+
9
+ #endif