node_mutation 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44339bec6d4624f5f7cf60b9253e701c7215f2b5bae0094b0532bf86e4c6cf91
4
- data.tar.gz: 4878c3c9f61830b728d6ea21424588d62c039e42aa5a029b1786844a1c7b74db
3
+ metadata.gz: 42db0eff54a5fab0ba9fc766d59605e7afe0c3bcf1627e646f0ffeff71a540bf
4
+ data.tar.gz: 2db50e0b04dc537e51afa21930bdee0b968aecd0d57573da6497b2da740193c7
5
5
  SHA512:
6
- metadata.gz: 1d0b12e168a2a8aa7a8019484125bd0edcdb159846216de1d1822e1a75f1b47e1f57f87b5de0cb94a0ebec45e950b2f32c9349c76294ac6156d0d382be85d52e
7
- data.tar.gz: deb03d485ddf7ca91797cafbe28526ea5e91b7f862c71173e801587d727ced13680f7ff4dc3d4f2d563e8830c52622e5ce44dc4f02d84ddc6f26f572941db6d5
6
+ metadata.gz: eef2c42ddf5d778e1b3cac59f464ba0816377424a132de4d2d9e41a65a49c5ba2743c53ca4c7278d542a83e3de804d6596408d9b2c6d3281a848b9f911fe764f
7
+ data.tar.gz: f188c65ace72e059b5ee91d878389ebb95c34f9fe0d64a819dee355f7b4e2e855a3907ddf92bfd79ca459d618709dfdb2d7aaec73fae8d38a75834ca3907a512
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.6.0 (2022-10-19)
4
+
5
+ * Raise `NodeMutation::MethodNotSupported` if not respond to child node name
6
+
3
7
  ## 1.5.1 (2022-10-19)
4
8
 
5
9
  * Better error message for unknown code
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.5.1)
4
+ node_mutation (1.6.0)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
 
@@ -2,13 +2,18 @@
2
2
 
3
3
  class NodeMutation::ParserAdapter < NodeMutation::Adapter
4
4
  def get_source(node)
5
- node.loc.expression.source
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
- begin
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 "rewritten_source is not handled for #{evaluated.inspect}"
44
+ raise "can not parse \"#{code}\""
42
45
  end
43
- rescue StandardError => e
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, nested_child_name = child_name.split('.', 2)
56
- child_node = direct_child_name =~ /\A\d+\z/ ? node[direct_child_name.to_i - 1] : node.send(direct_child_name)
57
- if nested_child_name
58
- return child_node_range(child_node, nested_child_name)
59
- elsif child_node
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
- direct_child_name, nested_child_name = child_name.to_s.split('.', 2)
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
- return nil if child_node.nil?
108
+ child_node = node.send(direct_child_name)
106
109
 
107
- if child_node.is_a?(Parser::AST::Node)
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
- # arguments
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.first.loc.expression.begin_pos,
122
- end: child_node.last.loc.expression.end_pos
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
- child_direct_child_node = direct_child_name =~ /\A\d+\z/ ? node[direct_child_name.to_i - 1] : node.send(direct_child_name)
158
- return child_node_by_name(child_direct_child_node, nested_child_name) if nested_child_name
159
- return child_direct_child_node if child_direct_child_node
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
- child_node = nil
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
- return nil if child_node.nil?
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.5.1"
4
+ VERSION = "1.6.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: node_mutation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang