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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44339bec6d4624f5f7cf60b9253e701c7215f2b5bae0094b0532bf86e4c6cf91
4
- data.tar.gz: 4878c3c9f61830b728d6ea21424588d62c039e42aa5a029b1786844a1c7b74db
3
+ metadata.gz: 1a919cd77334f1c935135667972c64670f1513a31c63dde2a71405dcb165077f
4
+ data.tar.gz: ce7faaade885a41905b1371b04cb2c94962cd044404f754a6bd1d0537b6c871a
5
5
  SHA512:
6
- metadata.gz: 1d0b12e168a2a8aa7a8019484125bd0edcdb159846216de1d1822e1a75f1b47e1f57f87b5de0cb94a0ebec45e950b2f32c9349c76294ac6156d0d382be85d52e
7
- data.tar.gz: deb03d485ddf7ca91797cafbe28526ea5e91b7f862c71173e801587d727ced13680f7ff4dc3d4f2d563e8830c52622e5ce44dc4f02d84ddc6f26f572941db6d5
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.5.1)
4
+ node_mutation (1.6.1)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
 
@@ -2,13 +2,19 @@
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
+ 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 "rewritten_source is not handled for #{evaluated.inspect}"
45
+ raise "can not parse \"#{code}\""
42
46
  end
43
- rescue StandardError => e
44
- raise "can not parse \"#{code}\""
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, 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}"
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
- 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
107
+ raise NodeMutation::MethodNotSupported, "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
104
108
 
105
- return nil if child_node.nil?
109
+ child_node = node.send(direct_child_name)
106
110
 
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
111
+ return child_node_range(child_node, nested_child_name) if nested_child_name
115
112
 
116
- # arguments
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.first.loc.expression.begin_pos,
122
- end: child_node.last.loc.expression.end_pos
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
- 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
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
- child_node = nil
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
- return nil if child_node.nil?
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
@@ -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.1"
5
5
  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.5.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-19 00:00:00.000000000 Z
11
+ date: 2022-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport