herb 0.8.10-arm-linux-gnu → 0.9.1-arm-linux-gnu
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.
- checksums.yaml +4 -4
- data/Makefile +11 -3
- data/README.md +64 -34
- data/Rakefile +48 -40
- data/config.yml +473 -34
- data/ext/herb/error_helpers.c +535 -140
- data/ext/herb/error_helpers.h +1 -0
- data/ext/herb/extconf.rb +67 -28
- data/ext/herb/extension.c +321 -51
- data/ext/herb/extension.h +1 -0
- data/ext/herb/extension_helpers.c +24 -14
- data/ext/herb/extension_helpers.h +2 -2
- data/ext/herb/nodes.c +647 -270
- data/ext/herb/nodes.h +1 -0
- data/herb.gemspec +3 -2
- data/lib/herb/3.0/herb.so +0 -0
- data/lib/herb/3.1/herb.so +0 -0
- data/lib/herb/3.2/herb.so +0 -0
- data/lib/herb/3.3/herb.so +0 -0
- data/lib/herb/3.4/herb.so +0 -0
- data/lib/herb/4.0/herb.so +0 -0
- data/lib/herb/ast/helpers.rb +3 -3
- data/lib/herb/ast/node.rb +15 -2
- data/lib/herb/ast/nodes.rb +1530 -179
- data/lib/herb/bootstrap.rb +87 -0
- data/lib/herb/cli.rb +341 -31
- data/lib/herb/configuration.rb +248 -0
- data/lib/herb/defaults.yml +32 -0
- data/lib/herb/engine/compiler.rb +78 -11
- data/lib/herb/engine/debug_visitor.rb +13 -3
- data/lib/herb/engine/error_formatter.rb +13 -9
- data/lib/herb/engine/parser_error_overlay.rb +10 -6
- data/lib/herb/engine/validator.rb +8 -3
- data/lib/herb/engine/validators/nesting_validator.rb +2 -2
- data/lib/herb/engine.rb +119 -43
- data/lib/herb/errors.rb +808 -88
- data/lib/herb/lex_result.rb +1 -0
- data/lib/herb/location.rb +7 -3
- data/lib/herb/parse_result.rb +12 -2
- data/lib/herb/parser_options.rb +62 -0
- data/lib/herb/position.rb +1 -0
- data/lib/herb/prism_inspect.rb +120 -0
- data/lib/herb/project.rb +923 -331
- data/lib/herb/range.rb +1 -0
- data/lib/herb/token.rb +7 -1
- data/lib/herb/version.rb +1 -1
- data/lib/herb/visitor.rb +47 -2
- data/lib/herb/warnings.rb +6 -1
- data/lib/herb.rb +35 -3
- data/sig/herb/ast/helpers.rbs +2 -2
- data/sig/herb/ast/node.rbs +12 -2
- data/sig/herb/ast/nodes.rbs +773 -128
- data/sig/herb/bootstrap.rbs +31 -0
- data/sig/herb/configuration.rbs +89 -0
- data/sig/herb/engine/compiler.rbs +9 -1
- data/sig/herb/engine/debug_visitor.rbs +2 -0
- data/sig/herb/engine/validator.rbs +5 -1
- data/sig/herb/engine.rbs +21 -3
- data/sig/herb/errors.rbs +372 -63
- data/sig/herb/location.rbs +4 -0
- data/sig/herb/parse_result.rbs +4 -2
- data/sig/herb/parser_options.rbs +46 -0
- data/sig/herb/position.rbs +1 -0
- data/sig/herb/prism_inspect.rbs +28 -0
- data/sig/herb/range.rbs +1 -0
- data/sig/herb/token.rbs +6 -0
- data/sig/herb/visitor.rbs +31 -4
- data/sig/herb/warnings.rbs +6 -1
- data/sig/herb.rbs +14 -0
- data/sig/herb_c_extension.rbs +5 -2
- data/sig/rubyvm.rbs +5 -0
- data/sig/serialized_ast_errors.rbs +82 -6
- data/sig/serialized_ast_nodes.rbs +91 -6
- data/src/analyze/action_view/attribute_extraction_helpers.c +303 -0
- data/src/analyze/action_view/content_tag.c +78 -0
- data/src/analyze/action_view/link_to.c +167 -0
- data/src/analyze/action_view/registry.c +83 -0
- data/src/analyze/action_view/tag.c +70 -0
- data/src/analyze/action_view/tag_helper_node_builders.c +305 -0
- data/src/analyze/action_view/tag_helpers.c +815 -0
- data/src/analyze/action_view/turbo_frame_tag.c +88 -0
- data/src/analyze/analyze.c +885 -0
- data/src/{analyzed_ruby.c → analyze/analyzed_ruby.c} +13 -11
- data/src/analyze/builders.c +343 -0
- data/src/analyze/conditional_elements.c +594 -0
- data/src/analyze/conditional_open_tags.c +640 -0
- data/src/analyze/control_type.c +250 -0
- data/src/{analyze_helpers.c → analyze/helpers.c} +48 -23
- data/src/analyze/invalid_structures.c +193 -0
- data/src/{analyze_missing_end.c → analyze/missing_end.c} +33 -22
- data/src/analyze/parse_errors.c +84 -0
- data/src/analyze/prism_annotate.c +399 -0
- data/src/analyze/render_nodes.c +761 -0
- data/src/{analyze_transform.c → analyze/transform.c} +24 -3
- data/src/ast_node.c +17 -7
- data/src/ast_nodes.c +759 -387
- data/src/ast_pretty_print.c +264 -6
- data/src/errors.c +1454 -519
- data/src/extract.c +145 -49
- data/src/herb.c +52 -34
- data/src/html_util.c +241 -12
- data/src/include/analyze/action_view/attribute_extraction_helpers.h +36 -0
- data/src/include/analyze/action_view/tag_helper_handler.h +43 -0
- data/src/include/analyze/action_view/tag_helper_node_builders.h +70 -0
- data/src/include/analyze/action_view/tag_helpers.h +38 -0
- data/src/include/{analyze.h → analyze/analyze.h} +14 -4
- data/src/include/{analyzed_ruby.h → analyze/analyzed_ruby.h} +3 -3
- data/src/include/analyze/builders.h +27 -0
- data/src/include/analyze/conditional_elements.h +9 -0
- data/src/include/analyze/conditional_open_tags.h +9 -0
- data/src/include/analyze/control_type.h +14 -0
- data/src/include/{analyze_helpers.h → analyze/helpers.h} +4 -2
- data/src/include/analyze/invalid_structures.h +11 -0
- data/src/include/analyze/prism_annotate.h +16 -0
- data/src/include/analyze/render_nodes.h +11 -0
- data/src/include/ast_node.h +11 -5
- data/src/include/ast_nodes.h +154 -38
- data/src/include/ast_pretty_print.h +5 -0
- data/src/include/element_source.h +3 -8
- data/src/include/errors.h +206 -55
- data/src/include/extract.h +21 -5
- data/src/include/herb.h +18 -6
- data/src/include/herb_prism_node.h +13 -0
- data/src/include/html_util.h +7 -2
- data/src/include/io.h +3 -1
- data/src/include/lex_helpers.h +29 -0
- data/src/include/lexer.h +1 -1
- data/src/include/lexer_peek_helpers.h +87 -13
- data/src/include/lexer_struct.h +2 -0
- data/src/include/location.h +2 -1
- data/src/include/parser.h +28 -2
- data/src/include/parser_helpers.h +19 -3
- data/src/include/pretty_print.h +10 -5
- data/src/include/prism_context.h +45 -0
- data/src/include/prism_helpers.h +10 -7
- data/src/include/prism_serialized.h +12 -0
- data/src/include/token.h +16 -4
- data/src/include/token_struct.h +10 -3
- data/src/include/utf8.h +2 -1
- data/src/include/util/hb_allocator.h +78 -0
- data/src/include/util/hb_arena.h +6 -1
- data/src/include/util/hb_arena_debug.h +12 -1
- data/src/include/util/hb_array.h +7 -3
- data/src/include/util/hb_buffer.h +6 -4
- data/src/include/util/hb_foreach.h +79 -0
- data/src/include/util/hb_narray.h +8 -4
- data/src/include/util/hb_string.h +56 -9
- data/src/include/util.h +6 -3
- data/src/include/version.h +1 -1
- data/src/io.c +3 -2
- data/src/lexer.c +42 -30
- data/src/lexer_peek_helpers.c +12 -74
- data/src/location.c +2 -2
- data/src/main.c +53 -28
- data/src/parser.c +784 -247
- data/src/parser_helpers.c +110 -23
- data/src/parser_match_tags.c +129 -48
- data/src/pretty_print.c +29 -24
- data/src/prism_helpers.c +30 -27
- data/src/ruby_parser.c +2 -0
- data/src/token.c +151 -66
- data/src/token_matchers.c +0 -1
- data/src/utf8.c +7 -6
- data/src/util/hb_allocator.c +341 -0
- data/src/util/hb_arena.c +81 -56
- data/src/util/hb_arena_debug.c +32 -17
- data/src/util/hb_array.c +30 -15
- data/src/util/hb_buffer.c +17 -21
- data/src/util/hb_narray.c +22 -7
- data/src/util/hb_string.c +49 -35
- data/src/util.c +21 -11
- data/src/visitor.c +67 -0
- data/templates/ext/herb/error_helpers.c.erb +24 -11
- data/templates/ext/herb/error_helpers.h.erb +1 -0
- data/templates/ext/herb/nodes.c.erb +50 -16
- data/templates/ext/herb/nodes.h.erb +1 -0
- data/templates/java/error_helpers.c.erb +1 -1
- data/templates/java/nodes.c.erb +30 -8
- data/templates/java/org/herb/ast/Errors.java.erb +24 -1
- data/templates/java/org/herb/ast/Nodes.java.erb +80 -21
- data/templates/javascript/packages/core/src/errors.ts.erb +16 -3
- data/templates/javascript/packages/core/src/node-type-guards.ts.erb +3 -1
- data/templates/javascript/packages/core/src/nodes.ts.erb +109 -32
- data/templates/javascript/packages/node/extension/error_helpers.cpp.erb +13 -4
- data/templates/javascript/packages/node/extension/nodes.cpp.erb +43 -4
- data/templates/lib/herb/ast/nodes.rb.erb +95 -32
- data/templates/lib/herb/errors.rb.erb +15 -3
- data/templates/lib/herb/visitor.rb.erb +2 -2
- data/templates/rust/src/ast/nodes.rs.erb +97 -44
- data/templates/rust/src/errors.rs.erb +2 -1
- data/templates/rust/src/nodes.rs.erb +168 -16
- data/templates/rust/src/union_types.rs.erb +60 -0
- data/templates/rust/src/visitor.rs.erb +81 -0
- data/templates/src/{analyze_missing_end.c.erb → analyze/missing_end.c.erb} +9 -6
- data/templates/src/{analyze_transform.c.erb → analyze/transform.c.erb} +2 -2
- data/templates/src/ast_nodes.c.erb +34 -26
- data/templates/src/ast_pretty_print.c.erb +24 -5
- data/templates/src/errors.c.erb +60 -54
- data/templates/src/include/ast_nodes.h.erb +6 -2
- data/templates/src/include/ast_pretty_print.h.erb +5 -0
- data/templates/src/include/errors.h.erb +15 -11
- data/templates/src/include/util/hb_foreach.h.erb +20 -0
- data/templates/src/parser_match_tags.c.erb +10 -4
- data/templates/src/visitor.c.erb +2 -2
- data/templates/template.rb +204 -29
- data/templates/wasm/error_helpers.cpp.erb +9 -5
- data/templates/wasm/nodes.cpp.erb +41 -4
- metadata +60 -16
- data/src/analyze.c +0 -1608
- data/src/element_source.c +0 -12
- data/src/include/util/hb_system.h +0 -9
- data/src/util/hb_system.c +0 -30
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
#include "../../include/analyze/action_view/attribute_extraction_helpers.h"
|
|
2
|
+
#include "../../include/analyze/action_view/tag_helper_node_builders.h"
|
|
3
|
+
#include "../../include/util.h"
|
|
4
|
+
#include "../../include/util/hb_allocator.h"
|
|
5
|
+
#include "../../include/util/hb_array.h"
|
|
6
|
+
#include "../../include/util/hb_string.h"
|
|
7
|
+
|
|
8
|
+
#include <prism.h>
|
|
9
|
+
#include <stdlib.h>
|
|
10
|
+
#include <string.h>
|
|
11
|
+
|
|
12
|
+
static char* extract_string_from_prism_node(pm_node_t* node, hb_allocator_T* allocator) {
|
|
13
|
+
const uint8_t* source = NULL;
|
|
14
|
+
size_t length = 0;
|
|
15
|
+
|
|
16
|
+
if (node->type == PM_SYMBOL_NODE) {
|
|
17
|
+
pm_symbol_node_t* symbol = (pm_symbol_node_t*) node;
|
|
18
|
+
source = pm_string_source(&symbol->unescaped);
|
|
19
|
+
length = pm_string_length(&symbol->unescaped);
|
|
20
|
+
} else if (node->type == PM_STRING_NODE) {
|
|
21
|
+
pm_string_node_t* string_node = (pm_string_node_t*) node;
|
|
22
|
+
source = pm_string_source(&string_node->unescaped);
|
|
23
|
+
length = pm_string_length(&string_node->unescaped);
|
|
24
|
+
} else {
|
|
25
|
+
return NULL;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return hb_allocator_strndup(allocator, (const char*) source, length);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static char* build_prefixed_key(const char* prefix, const char* raw_key, hb_allocator_T* allocator) {
|
|
32
|
+
char* dashed_key = convert_underscores_to_dashes(raw_key);
|
|
33
|
+
const char* key = dashed_key ? dashed_key : raw_key;
|
|
34
|
+
size_t prefix_len = strlen(prefix);
|
|
35
|
+
size_t key_len = strlen(key);
|
|
36
|
+
size_t total = prefix_len + 1 + key_len + 1;
|
|
37
|
+
char* result = hb_allocator_alloc(allocator, total);
|
|
38
|
+
|
|
39
|
+
if (result) { snprintf(result, total, "%s-%s", prefix, key); }
|
|
40
|
+
if (dashed_key) { free(dashed_key); }
|
|
41
|
+
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static AST_HTML_ATTRIBUTE_NODE_T* create_attribute_from_value(
|
|
46
|
+
const char* name_string,
|
|
47
|
+
pm_node_t* value_node,
|
|
48
|
+
position_T start_position,
|
|
49
|
+
position_T end_position,
|
|
50
|
+
hb_allocator_T* allocator
|
|
51
|
+
) {
|
|
52
|
+
if (value_node->type == PM_SYMBOL_NODE || value_node->type == PM_STRING_NODE) {
|
|
53
|
+
char* value_string = extract_string_from_prism_node(value_node, allocator);
|
|
54
|
+
if (!value_string) { return NULL; }
|
|
55
|
+
|
|
56
|
+
AST_HTML_ATTRIBUTE_NODE_T* attribute =
|
|
57
|
+
create_html_attribute_node(name_string, value_string, start_position, end_position, allocator);
|
|
58
|
+
hb_allocator_dealloc(allocator, value_string);
|
|
59
|
+
|
|
60
|
+
return attribute;
|
|
61
|
+
} else if (value_node->type == PM_INTERPOLATED_STRING_NODE) {
|
|
62
|
+
return create_html_attribute_with_interpolated_value(
|
|
63
|
+
name_string,
|
|
64
|
+
(pm_interpolated_string_node_t*) value_node,
|
|
65
|
+
start_position,
|
|
66
|
+
end_position,
|
|
67
|
+
allocator
|
|
68
|
+
);
|
|
69
|
+
} else {
|
|
70
|
+
size_t value_length = value_node->location.end - value_node->location.start;
|
|
71
|
+
char* ruby_content = hb_allocator_strndup(allocator, (const char*) value_node->location.start, value_length);
|
|
72
|
+
|
|
73
|
+
if (ruby_content && value_node->location.start) {
|
|
74
|
+
AST_HTML_ATTRIBUTE_NODE_T* attribute =
|
|
75
|
+
create_html_attribute_with_ruby_literal(name_string, ruby_content, start_position, end_position, allocator);
|
|
76
|
+
hb_allocator_dealloc(allocator, ruby_content);
|
|
77
|
+
return attribute;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return NULL;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
AST_HTML_ATTRIBUTE_NODE_T* extract_html_attribute_from_assoc(
|
|
85
|
+
pm_assoc_node_t* assoc,
|
|
86
|
+
const uint8_t* source,
|
|
87
|
+
const char* original_source,
|
|
88
|
+
size_t erb_content_offset,
|
|
89
|
+
hb_allocator_T* allocator
|
|
90
|
+
) {
|
|
91
|
+
if (!assoc) { return NULL; }
|
|
92
|
+
|
|
93
|
+
char* name_string = extract_string_from_prism_node(assoc->key, allocator);
|
|
94
|
+
if (!name_string) { return NULL; }
|
|
95
|
+
|
|
96
|
+
position_T start_position =
|
|
97
|
+
prism_location_to_position_with_offset(&assoc->key->location, original_source, erb_content_offset, source);
|
|
98
|
+
position_T end_position =
|
|
99
|
+
prism_location_to_position_with_offset(&assoc->value->location, original_source, erb_content_offset, source);
|
|
100
|
+
|
|
101
|
+
// Rails converts `method:` and `remote:` to `data-*` attributes
|
|
102
|
+
if (strcmp(name_string, "method") == 0 || strcmp(name_string, "remote") == 0) {
|
|
103
|
+
size_t name_len = strlen(name_string);
|
|
104
|
+
size_t prefixed_len = 5 + name_len + 1;
|
|
105
|
+
char* prefixed = hb_allocator_alloc(allocator, prefixed_len);
|
|
106
|
+
snprintf(prefixed, prefixed_len, "data-%s", name_string);
|
|
107
|
+
hb_allocator_dealloc(allocator, name_string);
|
|
108
|
+
name_string = prefixed;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if ((strcmp(name_string, "data") == 0 || strcmp(name_string, "aria") == 0) && assoc->value->type == PM_HASH_NODE) {
|
|
112
|
+
hb_allocator_dealloc(allocator, name_string);
|
|
113
|
+
return NULL;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
char* dashed_name = convert_underscores_to_dashes(name_string);
|
|
117
|
+
AST_HTML_ATTRIBUTE_NODE_T* attribute_node = create_attribute_from_value(
|
|
118
|
+
dashed_name ? dashed_name : name_string,
|
|
119
|
+
assoc->value,
|
|
120
|
+
start_position,
|
|
121
|
+
end_position,
|
|
122
|
+
allocator
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
if (dashed_name) { free(dashed_name); }
|
|
126
|
+
hb_allocator_dealloc(allocator, name_string);
|
|
127
|
+
|
|
128
|
+
return attribute_node;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
hb_array_T* extract_html_attributes_from_keyword_hash(
|
|
132
|
+
pm_keyword_hash_node_t* kw_hash,
|
|
133
|
+
const uint8_t* source,
|
|
134
|
+
const char* original_source,
|
|
135
|
+
size_t erb_content_offset,
|
|
136
|
+
hb_allocator_T* allocator
|
|
137
|
+
) {
|
|
138
|
+
if (!kw_hash) { return NULL; }
|
|
139
|
+
|
|
140
|
+
hb_array_T* attributes = hb_array_init(8, allocator);
|
|
141
|
+
if (!attributes) { return NULL; }
|
|
142
|
+
|
|
143
|
+
for (size_t i = 0; i < kw_hash->elements.size; i++) {
|
|
144
|
+
pm_node_t* element = kw_hash->elements.nodes[i];
|
|
145
|
+
|
|
146
|
+
if (element->type == PM_ASSOC_SPLAT_NODE) {
|
|
147
|
+
pm_assoc_splat_node_t* splat = (pm_assoc_splat_node_t*) element;
|
|
148
|
+
size_t splat_length = splat->base.location.end - splat->base.location.start;
|
|
149
|
+
char* splat_content = hb_allocator_strndup(allocator, (const char*) splat->base.location.start, splat_length);
|
|
150
|
+
|
|
151
|
+
if (splat_content) {
|
|
152
|
+
position_T splat_start =
|
|
153
|
+
prism_location_to_position_with_offset(&splat->base.location, original_source, erb_content_offset, source);
|
|
154
|
+
|
|
155
|
+
AST_RUBY_HTML_ATTRIBUTES_SPLAT_NODE_T* splat_node = ast_ruby_html_attributes_splat_node_init(
|
|
156
|
+
hb_string_from_c_string(splat_content),
|
|
157
|
+
HB_STRING_EMPTY,
|
|
158
|
+
splat_start,
|
|
159
|
+
splat_start,
|
|
160
|
+
hb_array_init(0, allocator),
|
|
161
|
+
allocator
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
if (splat_node) { hb_array_append(attributes, (AST_NODE_T*) splat_node); }
|
|
165
|
+
|
|
166
|
+
hb_allocator_dealloc(allocator, splat_content);
|
|
167
|
+
}
|
|
168
|
+
} else if (element->type == PM_ASSOC_NODE) {
|
|
169
|
+
pm_assoc_node_t* assoc = (pm_assoc_node_t*) element;
|
|
170
|
+
|
|
171
|
+
char* key_string = extract_string_from_prism_node(assoc->key, allocator);
|
|
172
|
+
|
|
173
|
+
if (key_string && (strcmp(key_string, "data") == 0 || strcmp(key_string, "aria") == 0)
|
|
174
|
+
&& assoc->value->type == PM_HASH_NODE) {
|
|
175
|
+
pm_hash_node_t* hash = (pm_hash_node_t*) assoc->value;
|
|
176
|
+
|
|
177
|
+
for (size_t j = 0; j < hash->elements.size; j++) {
|
|
178
|
+
pm_node_t* hash_element = hash->elements.nodes[j];
|
|
179
|
+
|
|
180
|
+
if (hash_element->type == PM_ASSOC_SPLAT_NODE) {
|
|
181
|
+
pm_assoc_splat_node_t* splat = (pm_assoc_splat_node_t*) hash_element;
|
|
182
|
+
size_t splat_length = splat->base.location.end - splat->base.location.start;
|
|
183
|
+
char* splat_content =
|
|
184
|
+
hb_allocator_strndup(allocator, (const char*) splat->base.location.start, splat_length);
|
|
185
|
+
|
|
186
|
+
if (splat_content) {
|
|
187
|
+
position_T splat_start = prism_location_to_position_with_offset(
|
|
188
|
+
&splat->base.location,
|
|
189
|
+
original_source,
|
|
190
|
+
erb_content_offset,
|
|
191
|
+
source
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
AST_RUBY_HTML_ATTRIBUTES_SPLAT_NODE_T* splat_node = ast_ruby_html_attributes_splat_node_init(
|
|
195
|
+
hb_string_from_c_string(splat_content),
|
|
196
|
+
hb_string_from_c_string(key_string),
|
|
197
|
+
splat_start,
|
|
198
|
+
splat_start,
|
|
199
|
+
hb_array_init(0, allocator),
|
|
200
|
+
allocator
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
if (splat_node) { hb_array_append(attributes, (AST_NODE_T*) splat_node); }
|
|
204
|
+
|
|
205
|
+
hb_allocator_dealloc(allocator, splat_content);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (hash_element->type != PM_ASSOC_NODE) { continue; }
|
|
212
|
+
|
|
213
|
+
pm_assoc_node_t* hash_assoc = (pm_assoc_node_t*) hash_element;
|
|
214
|
+
char* raw_key = extract_string_from_prism_node(hash_assoc->key, allocator);
|
|
215
|
+
if (!raw_key) { continue; }
|
|
216
|
+
|
|
217
|
+
char* attribute_key_string = build_prefixed_key(key_string, raw_key, allocator);
|
|
218
|
+
hb_allocator_dealloc(allocator, raw_key);
|
|
219
|
+
|
|
220
|
+
if (attribute_key_string) {
|
|
221
|
+
position_T attribute_start = prism_location_to_position_with_offset(
|
|
222
|
+
&hash_assoc->key->location,
|
|
223
|
+
original_source,
|
|
224
|
+
erb_content_offset,
|
|
225
|
+
source
|
|
226
|
+
);
|
|
227
|
+
position_T attribute_end = prism_location_to_position_with_offset(
|
|
228
|
+
&hash_assoc->value->location,
|
|
229
|
+
original_source,
|
|
230
|
+
erb_content_offset,
|
|
231
|
+
source
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
AST_HTML_ATTRIBUTE_NODE_T* attribute = create_attribute_from_value(
|
|
235
|
+
attribute_key_string,
|
|
236
|
+
hash_assoc->value,
|
|
237
|
+
attribute_start,
|
|
238
|
+
attribute_end,
|
|
239
|
+
allocator
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
if (attribute) { hb_array_append(attributes, attribute); }
|
|
243
|
+
hb_allocator_dealloc(allocator, attribute_key_string);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
AST_HTML_ATTRIBUTE_NODE_T* attribute =
|
|
248
|
+
extract_html_attribute_from_assoc(assoc, source, original_source, erb_content_offset, allocator);
|
|
249
|
+
|
|
250
|
+
if (attribute) { hb_array_append(attributes, attribute); }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (key_string) { hb_allocator_dealloc(allocator, key_string); }
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return attributes;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
bool has_html_attributes_in_call(pm_call_node_t* call_node) {
|
|
261
|
+
if (!call_node || !call_node->arguments) { return false; }
|
|
262
|
+
|
|
263
|
+
pm_arguments_node_t* arguments = call_node->arguments;
|
|
264
|
+
if (arguments->arguments.size == 0) { return false; }
|
|
265
|
+
|
|
266
|
+
pm_node_t* last_argument = arguments->arguments.nodes[arguments->arguments.size - 1];
|
|
267
|
+
|
|
268
|
+
return last_argument && (last_argument->type == PM_KEYWORD_HASH_NODE || last_argument->type == PM_HASH_NODE);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
hb_array_T* extract_html_attributes_from_call_node(
|
|
272
|
+
pm_call_node_t* call_node,
|
|
273
|
+
const uint8_t* source,
|
|
274
|
+
const char* original_source,
|
|
275
|
+
size_t erb_content_offset,
|
|
276
|
+
hb_allocator_T* allocator
|
|
277
|
+
) {
|
|
278
|
+
if (!has_html_attributes_in_call(call_node)) { return NULL; }
|
|
279
|
+
|
|
280
|
+
pm_arguments_node_t* arguments = call_node->arguments;
|
|
281
|
+
pm_node_t* last_argument = arguments->arguments.nodes[arguments->arguments.size - 1];
|
|
282
|
+
|
|
283
|
+
if (last_argument->type == PM_HASH_NODE) {
|
|
284
|
+
pm_hash_node_t* hash_node = (pm_hash_node_t*) last_argument;
|
|
285
|
+
pm_keyword_hash_node_t synthetic = { .base = hash_node->base, .elements = hash_node->elements };
|
|
286
|
+
|
|
287
|
+
return extract_html_attributes_from_keyword_hash(
|
|
288
|
+
&synthetic,
|
|
289
|
+
source,
|
|
290
|
+
original_source,
|
|
291
|
+
erb_content_offset,
|
|
292
|
+
allocator
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return extract_html_attributes_from_keyword_hash(
|
|
297
|
+
(pm_keyword_hash_node_t*) last_argument,
|
|
298
|
+
source,
|
|
299
|
+
original_source,
|
|
300
|
+
erb_content_offset,
|
|
301
|
+
allocator
|
|
302
|
+
);
|
|
303
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#include "../../include/analyze/action_view/tag_helper_handler.h"
|
|
2
|
+
|
|
3
|
+
#include <prism.h>
|
|
4
|
+
#include <stdbool.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
#include <string.h>
|
|
7
|
+
|
|
8
|
+
bool detect_content_tag(pm_call_node_t* call_node, pm_parser_t* parser) {
|
|
9
|
+
if (!call_node || !call_node->name) { return false; }
|
|
10
|
+
|
|
11
|
+
pm_constant_t* constant = pm_constant_pool_id_to_constant(&parser->constant_pool, call_node->name);
|
|
12
|
+
return constant && constant->length == 11 && strncmp((const char*) constant->start, "content_tag", 11) == 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
char* extract_content_tag_name(pm_call_node_t* call_node, pm_parser_t* parser, hb_allocator_T* allocator) {
|
|
16
|
+
(void) parser;
|
|
17
|
+
|
|
18
|
+
if (!call_node || !call_node->arguments) { return NULL; }
|
|
19
|
+
|
|
20
|
+
pm_arguments_node_t* arguments = call_node->arguments;
|
|
21
|
+
if (!arguments->arguments.size) { return NULL; }
|
|
22
|
+
|
|
23
|
+
pm_node_t* first_argument = arguments->arguments.nodes[0];
|
|
24
|
+
|
|
25
|
+
if (first_argument->type == PM_STRING_NODE) {
|
|
26
|
+
pm_string_node_t* string_node = (pm_string_node_t*) first_argument;
|
|
27
|
+
size_t length = pm_string_length(&string_node->unescaped);
|
|
28
|
+
return hb_allocator_strndup(allocator, (const char*) pm_string_source(&string_node->unescaped), length);
|
|
29
|
+
} else if (first_argument->type == PM_SYMBOL_NODE) {
|
|
30
|
+
pm_symbol_node_t* symbol_node = (pm_symbol_node_t*) first_argument;
|
|
31
|
+
size_t length = pm_string_length(&symbol_node->unescaped);
|
|
32
|
+
return hb_allocator_strndup(allocator, (const char*) pm_string_source(&symbol_node->unescaped), length);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return NULL;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
char* extract_content_tag_content(pm_call_node_t* call_node, pm_parser_t* parser, hb_allocator_T* allocator) {
|
|
39
|
+
(void) parser;
|
|
40
|
+
|
|
41
|
+
if (!call_node) { return NULL; }
|
|
42
|
+
|
|
43
|
+
char* block_content = extract_inline_block_content(call_node, allocator);
|
|
44
|
+
if (block_content) { return block_content; }
|
|
45
|
+
|
|
46
|
+
if (call_node->arguments) {
|
|
47
|
+
pm_arguments_node_t* arguments = call_node->arguments;
|
|
48
|
+
|
|
49
|
+
if (arguments->arguments.size >= 2) {
|
|
50
|
+
pm_node_t* second_argument = arguments->arguments.nodes[1];
|
|
51
|
+
|
|
52
|
+
if (second_argument->type != PM_KEYWORD_HASH_NODE) {
|
|
53
|
+
if (second_argument->type == PM_STRING_NODE) {
|
|
54
|
+
pm_string_node_t* string_node = (pm_string_node_t*) second_argument;
|
|
55
|
+
size_t length = pm_string_length(&string_node->unescaped);
|
|
56
|
+
return hb_allocator_strndup(allocator, (const char*) pm_string_source(&string_node->unescaped), length);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
size_t source_length = second_argument->location.end - second_argument->location.start;
|
|
60
|
+
return hb_allocator_strndup(allocator, (const char*) second_argument->location.start, source_length);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return NULL;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
bool content_tag_supports_block(void) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const tag_helper_handler_T content_tag_handler = { .name = "content_tag",
|
|
73
|
+
.source =
|
|
74
|
+
HB_STRING_LITERAL("ActionView::Helpers::TagHelper#content_tag"),
|
|
75
|
+
.detect = detect_content_tag,
|
|
76
|
+
.extract_tag_name = extract_content_tag_name,
|
|
77
|
+
.extract_content = extract_content_tag_content,
|
|
78
|
+
.supports_block = content_tag_supports_block };
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
#include "../../include/analyze/action_view/tag_helper_handler.h"
|
|
2
|
+
|
|
3
|
+
#include <prism.h>
|
|
4
|
+
#include <stdbool.h>
|
|
5
|
+
#include <stdio.h>
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
char* wrap_in_url_for(const char* source, size_t source_length, hb_allocator_T* allocator) {
|
|
10
|
+
const char* prefix = "url_for(";
|
|
11
|
+
const char* suffix = ")";
|
|
12
|
+
size_t prefix_len = strlen(prefix);
|
|
13
|
+
size_t suffix_len = strlen(suffix);
|
|
14
|
+
size_t total_length = prefix_len + source_length + suffix_len;
|
|
15
|
+
char* result = hb_allocator_alloc(allocator, total_length + 1);
|
|
16
|
+
|
|
17
|
+
memcpy(result, prefix, prefix_len);
|
|
18
|
+
memcpy(result + prefix_len, source, source_length);
|
|
19
|
+
memcpy(result + prefix_len + source_length, suffix, suffix_len);
|
|
20
|
+
result[total_length] = '\0';
|
|
21
|
+
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
bool detect_link_to(pm_call_node_t* call_node, pm_parser_t* parser) {
|
|
26
|
+
if (!call_node || !call_node->name) { return false; }
|
|
27
|
+
|
|
28
|
+
pm_constant_t* constant = pm_constant_pool_id_to_constant(&parser->constant_pool, call_node->name);
|
|
29
|
+
|
|
30
|
+
return constant && constant->length == 7 && strncmp((const char*) constant->start, "link_to", 7) == 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
bool is_route_helper_node(pm_node_t* node, pm_parser_t* parser) {
|
|
34
|
+
if (!node || node->type != PM_CALL_NODE) { return false; }
|
|
35
|
+
|
|
36
|
+
pm_call_node_t* call = (pm_call_node_t*) node;
|
|
37
|
+
if (!call->name) { return false; }
|
|
38
|
+
|
|
39
|
+
pm_constant_t* constant = pm_constant_pool_id_to_constant(&parser->constant_pool, call->name);
|
|
40
|
+
if (!constant || constant->length < 5) { return false; }
|
|
41
|
+
|
|
42
|
+
const char* name = (const char*) constant->start;
|
|
43
|
+
size_t length = constant->length;
|
|
44
|
+
|
|
45
|
+
return (length >= 5 && strncmp(name + length - 5, "_path", 5) == 0)
|
|
46
|
+
|| (length >= 4 && strncmp(name + length - 4, "_url", 4) == 0);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
char* extract_link_to_tag_name(pm_call_node_t* call_node, pm_parser_t* parser, hb_allocator_T* allocator) {
|
|
50
|
+
(void) call_node;
|
|
51
|
+
(void) parser;
|
|
52
|
+
|
|
53
|
+
return hb_allocator_strdup(allocator, "a");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
char* extract_link_to_content(pm_call_node_t* call_node, pm_parser_t* parser, hb_allocator_T* allocator) {
|
|
57
|
+
(void) parser;
|
|
58
|
+
|
|
59
|
+
if (!call_node) { return NULL; }
|
|
60
|
+
|
|
61
|
+
char* block_content = extract_inline_block_content(call_node, allocator);
|
|
62
|
+
if (block_content) { return block_content; }
|
|
63
|
+
|
|
64
|
+
if (!call_node->arguments) { return NULL; }
|
|
65
|
+
|
|
66
|
+
pm_arguments_node_t* arguments = call_node->arguments;
|
|
67
|
+
if (!arguments->arguments.size) { return NULL; }
|
|
68
|
+
|
|
69
|
+
pm_node_t* first_argument = arguments->arguments.nodes[0];
|
|
70
|
+
|
|
71
|
+
if (first_argument->type == PM_NIL_NODE) { return NULL; }
|
|
72
|
+
|
|
73
|
+
if (first_argument->type == PM_STRING_NODE) {
|
|
74
|
+
pm_string_node_t* string_node = (pm_string_node_t*) first_argument;
|
|
75
|
+
size_t length = pm_string_length(&string_node->unescaped);
|
|
76
|
+
return hb_allocator_strndup(allocator, (const char*) pm_string_source(&string_node->unescaped), length);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
size_t source_length = first_argument->location.end - first_argument->location.start;
|
|
80
|
+
|
|
81
|
+
// Format: "<expression>.to_s"
|
|
82
|
+
if (arguments->arguments.size == 1) {
|
|
83
|
+
const char* suffix = ".to_s";
|
|
84
|
+
size_t suffix_len = strlen(suffix);
|
|
85
|
+
size_t total_length = source_length + suffix_len;
|
|
86
|
+
char* ruby_expression = hb_allocator_alloc(allocator, total_length + 1);
|
|
87
|
+
|
|
88
|
+
memcpy(ruby_expression, (const char*) first_argument->location.start, source_length);
|
|
89
|
+
memcpy(ruby_expression + source_length, suffix, suffix_len);
|
|
90
|
+
ruby_expression[total_length] = '\0';
|
|
91
|
+
|
|
92
|
+
return ruby_expression;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return hb_allocator_strndup(allocator, (const char*) first_argument->location.start, source_length);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
char* extract_link_to_href(pm_call_node_t* call_node, pm_parser_t* parser, hb_allocator_T* allocator) {
|
|
99
|
+
(void) parser;
|
|
100
|
+
|
|
101
|
+
if (!call_node || !call_node->arguments) { return NULL; }
|
|
102
|
+
|
|
103
|
+
pm_arguments_node_t* arguments = call_node->arguments;
|
|
104
|
+
bool has_inline_block = call_node->block && call_node->block->type == PM_BLOCK_NODE;
|
|
105
|
+
|
|
106
|
+
if (has_inline_block && arguments->arguments.size >= 1) {
|
|
107
|
+
pm_node_t* first_argument = arguments->arguments.nodes[0];
|
|
108
|
+
|
|
109
|
+
if (first_argument->type == PM_STRING_NODE) {
|
|
110
|
+
pm_string_node_t* string_node = (pm_string_node_t*) first_argument;
|
|
111
|
+
size_t length = pm_string_length(&string_node->unescaped);
|
|
112
|
+
return hb_allocator_strndup(allocator, (const char*) pm_string_source(&string_node->unescaped), length);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
size_t source_length = first_argument->location.end - first_argument->location.start;
|
|
116
|
+
|
|
117
|
+
if (is_route_helper_node(first_argument, parser)) {
|
|
118
|
+
return hb_allocator_strndup(allocator, (const char*) first_argument->location.start, source_length);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return wrap_in_url_for((const char*) first_argument->location.start, source_length, allocator);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Format: "url_for(<expression>)"
|
|
125
|
+
if (arguments->arguments.size == 1) {
|
|
126
|
+
pm_node_t* first_argument = arguments->arguments.nodes[0];
|
|
127
|
+
|
|
128
|
+
if (first_argument->type == PM_STRING_NODE || first_argument->type == PM_NIL_NODE) { return NULL; }
|
|
129
|
+
|
|
130
|
+
size_t source_length = first_argument->location.end - first_argument->location.start;
|
|
131
|
+
|
|
132
|
+
if (is_route_helper_node(first_argument, parser)) {
|
|
133
|
+
return hb_allocator_strndup(allocator, (const char*) first_argument->location.start, source_length);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return wrap_in_url_for((const char*) first_argument->location.start, source_length, allocator);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (arguments->arguments.size < 2) { return NULL; }
|
|
140
|
+
|
|
141
|
+
pm_node_t* second_argument = arguments->arguments.nodes[1];
|
|
142
|
+
|
|
143
|
+
if (second_argument->type == PM_STRING_NODE) {
|
|
144
|
+
pm_string_node_t* string_node = (pm_string_node_t*) second_argument;
|
|
145
|
+
size_t length = pm_string_length(&string_node->unescaped);
|
|
146
|
+
return hb_allocator_strndup(allocator, (const char*) pm_string_source(&string_node->unescaped), length);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
size_t source_length = second_argument->location.end - second_argument->location.start;
|
|
150
|
+
|
|
151
|
+
if (!is_route_helper_node(second_argument, parser)) {
|
|
152
|
+
return wrap_in_url_for((const char*) second_argument->location.start, source_length, allocator);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return hb_allocator_strndup(allocator, (const char*) second_argument->location.start, source_length);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
bool link_to_supports_block(void) {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const tag_helper_handler_T link_to_handler = { .name = "link_to",
|
|
163
|
+
.source = HB_STRING_LITERAL("ActionView::Helpers::UrlHelper#link_to"),
|
|
164
|
+
.detect = detect_link_to,
|
|
165
|
+
.extract_tag_name = extract_link_to_tag_name,
|
|
166
|
+
.extract_content = extract_link_to_content,
|
|
167
|
+
.supports_block = link_to_supports_block };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#include "../../include/analyze/action_view/tag_helper_handler.h"
|
|
2
|
+
#include "../../include/util/hb_allocator.h"
|
|
3
|
+
|
|
4
|
+
#include <stdbool.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
|
|
7
|
+
extern const tag_helper_handler_T content_tag_handler;
|
|
8
|
+
extern const tag_helper_handler_T tag_dot_handler;
|
|
9
|
+
extern const tag_helper_handler_T link_to_handler;
|
|
10
|
+
extern const tag_helper_handler_T turbo_frame_tag_handler;
|
|
11
|
+
|
|
12
|
+
static size_t handlers_count = 4;
|
|
13
|
+
|
|
14
|
+
tag_helper_info_T* tag_helper_info_init(hb_allocator_T* allocator) {
|
|
15
|
+
tag_helper_info_T* info = hb_allocator_alloc(allocator, sizeof(tag_helper_info_T));
|
|
16
|
+
|
|
17
|
+
if (info) {
|
|
18
|
+
info->tag_name = NULL;
|
|
19
|
+
info->call_node = NULL;
|
|
20
|
+
info->attributes = NULL;
|
|
21
|
+
info->content = NULL;
|
|
22
|
+
info->has_block = false;
|
|
23
|
+
info->allocator = allocator;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return info;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
void tag_helper_info_free(tag_helper_info_T** info) {
|
|
30
|
+
if (!info || !*info) { return; }
|
|
31
|
+
|
|
32
|
+
hb_allocator_T* allocator = (*info)->allocator;
|
|
33
|
+
|
|
34
|
+
if ((*info)->tag_name) { hb_allocator_dealloc(allocator, (*info)->tag_name); }
|
|
35
|
+
if ((*info)->content) { hb_allocator_dealloc(allocator, (*info)->content); }
|
|
36
|
+
if ((*info)->attributes) { hb_array_free(&(*info)->attributes); }
|
|
37
|
+
|
|
38
|
+
hb_allocator_dealloc(allocator, *info);
|
|
39
|
+
|
|
40
|
+
*info = NULL;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
tag_helper_handler_T* get_tag_helper_handlers(void) {
|
|
44
|
+
static tag_helper_handler_T static_handlers[4];
|
|
45
|
+
static bool initialized = false;
|
|
46
|
+
|
|
47
|
+
if (!initialized) {
|
|
48
|
+
static_handlers[0] = content_tag_handler;
|
|
49
|
+
static_handlers[1] = tag_dot_handler;
|
|
50
|
+
static_handlers[2] = link_to_handler;
|
|
51
|
+
static_handlers[3] = turbo_frame_tag_handler;
|
|
52
|
+
initialized = true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return static_handlers;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
size_t get_tag_helper_handlers_count(void) {
|
|
59
|
+
return handlers_count;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
char* extract_inline_block_content(pm_call_node_t* call_node, hb_allocator_T* allocator) {
|
|
63
|
+
if (!call_node || !call_node->block || call_node->block->type != PM_BLOCK_NODE) { return NULL; }
|
|
64
|
+
|
|
65
|
+
pm_block_node_t* block_node = (pm_block_node_t*) call_node->block;
|
|
66
|
+
|
|
67
|
+
if (!block_node->body || block_node->body->type != PM_STATEMENTS_NODE) { return NULL; }
|
|
68
|
+
|
|
69
|
+
pm_statements_node_t* statements = (pm_statements_node_t*) block_node->body;
|
|
70
|
+
|
|
71
|
+
if (statements->body.size != 1) { return NULL; }
|
|
72
|
+
|
|
73
|
+
pm_node_t* statement = statements->body.nodes[0];
|
|
74
|
+
|
|
75
|
+
if (statement->type == PM_STRING_NODE) {
|
|
76
|
+
pm_string_node_t* string_node = (pm_string_node_t*) statement;
|
|
77
|
+
size_t length = pm_string_length(&string_node->unescaped);
|
|
78
|
+
return hb_allocator_strndup(allocator, (const char*) pm_string_source(&string_node->unescaped), length);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
size_t source_length = statement->location.end - statement->location.start;
|
|
82
|
+
return hb_allocator_strndup(allocator, (const char*) statement->location.start, source_length);
|
|
83
|
+
}
|