node_mutation 1.5.1 → 1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/lib/node_mutation/parser_adapter.rb +54 -44
- data/lib/node_mutation/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42db0eff54a5fab0ba9fc766d59605e7afe0c3bcf1627e646f0ffeff71a540bf
|
4
|
+
data.tar.gz: 2db50e0b04dc537e51afa21930bdee0b968aecd0d57573da6497b2da740193c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eef2c42ddf5d778e1b3cac59f464ba0816377424a132de4d2d9e41a65a49c5ba2743c53ca4c7278d542a83e3de804d6596408d9b2c6d3281a848b9f911fe764f
|
7
|
+
data.tar.gz: f188c65ace72e059b5ee91d878389ebb95c34f9fe0d64a819dee355f7b4e2e855a3907ddf92bfd79ca459d618709dfdb2d7aaec73fae8d38a75834ca3907a512
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -2,13 +2,18 @@
|
|
2
2
|
|
3
3
|
class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
4
4
|
def get_source(node)
|
5
|
-
node.
|
5
|
+
if node.is_a?(Array)
|
6
|
+
source = file_content(node.first)
|
7
|
+
source[node.first.loc.expression.begin_pos...node.last.loc.expression.end_pos]
|
8
|
+
else
|
9
|
+
node.loc.expression.source
|
10
|
+
end
|
6
11
|
end
|
7
12
|
|
8
13
|
def rewritten_source(node, code)
|
9
14
|
code.gsub(/{{(.+?)}}/m) do
|
10
15
|
old_code = Regexp.last_match(1)
|
11
|
-
|
16
|
+
if node.respond_to?(old_code.split('.').first)
|
12
17
|
evaluated = child_node_by_name(node, old_code)
|
13
18
|
case evaluated
|
14
19
|
when Parser::AST::Node
|
@@ -35,12 +40,10 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
35
40
|
end
|
36
41
|
when String, Symbol, Integer, Float
|
37
42
|
evaluated
|
38
|
-
when NilClass
|
39
|
-
'nil'
|
40
43
|
else
|
41
|
-
raise "
|
44
|
+
raise "can not parse \"#{code}\""
|
42
45
|
end
|
43
|
-
|
46
|
+
else
|
44
47
|
raise "can not parse \"#{code}\""
|
45
48
|
end
|
46
49
|
end
|
@@ -51,19 +54,23 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
51
54
|
end
|
52
55
|
|
53
56
|
def child_node_range(node, child_name)
|
57
|
+
direct_child_name, nested_child_name = child_name.to_s.split('.', 2)
|
58
|
+
|
54
59
|
if node.is_a?(Array)
|
55
|
-
direct_child_name
|
56
|
-
|
57
|
-
|
58
|
-
return child_node_range(child_node, nested_child_name)
|
59
|
-
|
60
|
-
return OpenStruct.new(
|
61
|
-
start: child_node.loc.expression.begin_pos,
|
62
|
-
end: child_node.loc.expression.end_pos
|
63
|
-
)
|
64
|
-
else
|
65
|
-
raise MethodNotSupported, "child_node_range is not handled for #{get_source(node)}, child_name: #{child_name}"
|
60
|
+
if direct_child_name =~ /\A\d+\z/
|
61
|
+
child_node = node[direct_child_name.to_i - 1]
|
62
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless child_node
|
63
|
+
return child_node_range(child_node, nested_child_name) if nested_child_name
|
64
|
+
return OpenStruct.new(start: child_node.loc.expression.begin_pos, end: child_node.loc.expression.end_pos)
|
66
65
|
end
|
66
|
+
|
67
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
|
68
|
+
child_node = node.send(direct_child_name)
|
69
|
+
return child_node_range(child_node, nested_child_name) if nested_child_name
|
70
|
+
return OpenStruct.new(
|
71
|
+
start: child_node.loc.expression.begin_pos,
|
72
|
+
end: child_node.loc.expression.end_pos
|
73
|
+
)
|
67
74
|
end
|
68
75
|
|
69
76
|
case [node.type, child_name.to_sym]
|
@@ -96,33 +103,32 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
96
103
|
OpenStruct.new(start: node.loc.begin.begin_pos, end: node.loc.end.end_pos)
|
97
104
|
end
|
98
105
|
else
|
99
|
-
|
100
|
-
if node.respond_to?(direct_child_name)
|
101
|
-
child_node = node.send(direct_child_name)
|
102
|
-
|
103
|
-
return child_node_range(child_node, nested_child_name) if nested_child_name
|
106
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
|
104
107
|
|
105
|
-
|
108
|
+
child_node = node.send(direct_child_name)
|
106
109
|
|
107
|
-
|
108
|
-
return(
|
109
|
-
OpenStruct.new(
|
110
|
-
start: child_node.loc.expression.begin_pos,
|
111
|
-
end: child_node.loc.expression.end_pos
|
112
|
-
)
|
113
|
-
)
|
114
|
-
end
|
110
|
+
return child_node_range(child_node, nested_child_name) if nested_child_name
|
115
111
|
|
116
|
-
|
117
|
-
return nil if child_node.empty?
|
112
|
+
return nil if child_node.nil?
|
118
113
|
|
114
|
+
if child_node.is_a?(Parser::AST::Node)
|
119
115
|
return(
|
120
116
|
OpenStruct.new(
|
121
|
-
start: child_node.
|
122
|
-
end: child_node.
|
117
|
+
start: child_node.loc.expression.begin_pos,
|
118
|
+
end: child_node.loc.expression.end_pos
|
123
119
|
)
|
124
120
|
)
|
125
121
|
end
|
122
|
+
|
123
|
+
# arguments
|
124
|
+
return nil if child_node.empty?
|
125
|
+
|
126
|
+
return(
|
127
|
+
OpenStruct.new(
|
128
|
+
start: child_node.first.loc.expression.begin_pos,
|
129
|
+
end: child_node.last.loc.expression.end_pos
|
130
|
+
)
|
131
|
+
)
|
126
132
|
end
|
127
133
|
end
|
128
134
|
|
@@ -154,9 +160,17 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
154
160
|
direct_child_name, nested_child_name = child_name.to_s.split('.', 2)
|
155
161
|
|
156
162
|
if node.is_a?(Array)
|
157
|
-
|
158
|
-
|
159
|
-
|
163
|
+
if direct_child_name =~ /\A\d+\z/
|
164
|
+
child_node = node[direct_child_name.to_i - 1]
|
165
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless child_node
|
166
|
+
return child_node_by_name(child_node, nested_child_name) if nested_child_name
|
167
|
+
return child_node
|
168
|
+
end
|
169
|
+
|
170
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
|
171
|
+
child_node = node.send(direct_child_name)
|
172
|
+
return child_node_by_name(child_node, nested_child_name) if nested_child_name
|
173
|
+
return child_node
|
160
174
|
end
|
161
175
|
|
162
176
|
if node.respond_to?(direct_child_name)
|
@@ -164,15 +178,11 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
164
178
|
elsif direct_child_name.include?('(') && direct_child_name.include?(')')
|
165
179
|
child_node = eval("node.#{direct_child_name}")
|
166
180
|
else
|
167
|
-
|
181
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}"
|
168
182
|
end
|
169
183
|
|
170
184
|
return child_node_by_name(child_node, nested_child_name) if nested_child_name
|
171
185
|
|
172
|
-
|
173
|
-
|
174
|
-
return child_node if child_node.is_a?(Parser::AST::Node)
|
175
|
-
|
176
|
-
return child_node
|
186
|
+
child_node
|
177
187
|
end
|
178
188
|
end
|