node_mutation 1.5.1 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/lib/node_mutation/parser_adapter.rb +56 -45
- data/lib/node_mutation/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a919cd77334f1c935135667972c64670f1513a31c63dde2a71405dcb165077f
|
4
|
+
data.tar.gz: ce7faaade885a41905b1371b04cb2c94962cd044404f754a6bd1d0537b6c871a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee3c31054d31ba1b997e366f3c2ac0fd4d6b3587f9f49e59d12d72a23cb8b8d4b1f12dcfb7f5d72440cbd867b4ed5ef01e19cfcdfbaa11a9c3d4b7db53c047e6
|
7
|
+
data.tar.gz: 2295e27670dbaa6be22ef320cd3e13cbb69e86fcf2f8824f8be4ada1607deb98fa7d6d1891bb3a4360debd37abf5876f155276ddd7bf45b0aac6667df67920af
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# NodeMutation
|
2
2
|
|
3
|
+
## 1.6.1 (2022-10-24)
|
4
|
+
|
5
|
+
* Better error message when node does not respond to a key
|
6
|
+
|
7
|
+
## 1.6.0 (2022-10-19)
|
8
|
+
|
9
|
+
* Raise `NodeMutation::MethodNotSupported` if not respond to child node name
|
10
|
+
|
3
11
|
## 1.5.1 (2022-10-19)
|
4
12
|
|
5
13
|
* Better error message for unknown code
|
data/Gemfile.lock
CHANGED
@@ -2,13 +2,19 @@
|
|
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
|
+
first_key = old_code.split('.').first
|
17
|
+
if node.respond_to?(first_key)
|
12
18
|
evaluated = child_node_by_name(node, old_code)
|
13
19
|
case evaluated
|
14
20
|
when Parser::AST::Node
|
@@ -35,13 +41,11 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
35
41
|
end
|
36
42
|
when String, Symbol, Integer, Float
|
37
43
|
evaluated
|
38
|
-
when NilClass
|
39
|
-
'nil'
|
40
44
|
else
|
41
|
-
raise "
|
45
|
+
raise "can not parse \"#{code}\""
|
42
46
|
end
|
43
|
-
|
44
|
-
raise "
|
47
|
+
else
|
48
|
+
raise NodeMutation::MethodNotSupported, "#{first_key} is not supported for #{get_source(node)}"
|
45
49
|
end
|
46
50
|
end
|
47
51
|
end
|
@@ -51,19 +55,23 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
51
55
|
end
|
52
56
|
|
53
57
|
def child_node_range(node, child_name)
|
58
|
+
direct_child_name, nested_child_name = child_name.to_s.split('.', 2)
|
59
|
+
|
54
60
|
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}"
|
61
|
+
if direct_child_name =~ /\A\d+\z/
|
62
|
+
child_node = node[direct_child_name.to_i - 1]
|
63
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless child_node
|
64
|
+
return child_node_range(child_node, nested_child_name) if nested_child_name
|
65
|
+
return OpenStruct.new(start: child_node.loc.expression.begin_pos, end: child_node.loc.expression.end_pos)
|
66
66
|
end
|
67
|
+
|
68
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
|
69
|
+
child_node = node.send(direct_child_name)
|
70
|
+
return child_node_range(child_node, nested_child_name) if nested_child_name
|
71
|
+
return OpenStruct.new(
|
72
|
+
start: child_node.loc.expression.begin_pos,
|
73
|
+
end: child_node.loc.expression.end_pos
|
74
|
+
)
|
67
75
|
end
|
68
76
|
|
69
77
|
case [node.type, child_name.to_sym]
|
@@ -96,33 +104,32 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
96
104
|
OpenStruct.new(start: node.loc.begin.begin_pos, end: node.loc.end.end_pos)
|
97
105
|
end
|
98
106
|
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
|
107
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
|
104
108
|
|
105
|
-
|
109
|
+
child_node = node.send(direct_child_name)
|
106
110
|
|
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
|
111
|
+
return child_node_range(child_node, nested_child_name) if nested_child_name
|
115
112
|
|
116
|
-
|
117
|
-
return nil if child_node.empty?
|
113
|
+
return nil if child_node.nil?
|
118
114
|
|
115
|
+
if child_node.is_a?(Parser::AST::Node)
|
119
116
|
return(
|
120
117
|
OpenStruct.new(
|
121
|
-
start: child_node.
|
122
|
-
end: child_node.
|
118
|
+
start: child_node.loc.expression.begin_pos,
|
119
|
+
end: child_node.loc.expression.end_pos
|
123
120
|
)
|
124
121
|
)
|
125
122
|
end
|
123
|
+
|
124
|
+
# arguments
|
125
|
+
return nil if child_node.empty?
|
126
|
+
|
127
|
+
return(
|
128
|
+
OpenStruct.new(
|
129
|
+
start: child_node.first.loc.expression.begin_pos,
|
130
|
+
end: child_node.last.loc.expression.end_pos
|
131
|
+
)
|
132
|
+
)
|
126
133
|
end
|
127
134
|
end
|
128
135
|
|
@@ -154,9 +161,17 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
154
161
|
direct_child_name, nested_child_name = child_name.to_s.split('.', 2)
|
155
162
|
|
156
163
|
if node.is_a?(Array)
|
157
|
-
|
158
|
-
|
159
|
-
|
164
|
+
if direct_child_name =~ /\A\d+\z/
|
165
|
+
child_node = node[direct_child_name.to_i - 1]
|
166
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless child_node
|
167
|
+
return child_node_by_name(child_node, nested_child_name) if nested_child_name
|
168
|
+
return child_node
|
169
|
+
end
|
170
|
+
|
171
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
|
172
|
+
child_node = node.send(direct_child_name)
|
173
|
+
return child_node_by_name(child_node, nested_child_name) if nested_child_name
|
174
|
+
return child_node
|
160
175
|
end
|
161
176
|
|
162
177
|
if node.respond_to?(direct_child_name)
|
@@ -164,15 +179,11 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
|
|
164
179
|
elsif direct_child_name.include?('(') && direct_child_name.include?(')')
|
165
180
|
child_node = eval("node.#{direct_child_name}")
|
166
181
|
else
|
167
|
-
|
182
|
+
raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}"
|
168
183
|
end
|
169
184
|
|
170
185
|
return child_node_by_name(child_node, nested_child_name) if nested_child_name
|
171
186
|
|
172
|
-
|
173
|
-
|
174
|
-
return child_node if child_node.is_a?(Parser::AST::Node)
|
175
|
-
|
176
|
-
return child_node
|
187
|
+
child_node
|
177
188
|
end
|
178
189
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: node_mutation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|