node_mutation 1.5.0 → 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: b66c8223045972f4f91cdae0b61a5589c04e7f53f3eedf66b1c0eebee9d66b1c
4
- data.tar.gz: 64a47a10bce6886a876cd04b08ff37118e6094bcc40d1e909e861d6f25c37d43
3
+ metadata.gz: 42db0eff54a5fab0ba9fc766d59605e7afe0c3bcf1627e646f0ffeff71a540bf
4
+ data.tar.gz: 2db50e0b04dc537e51afa21930bdee0b968aecd0d57573da6497b2da740193c7
5
5
  SHA512:
6
- metadata.gz: 12f5cd0bc631511de6d7b250dec14869ce1e19d5c311ac59e5722578f2fa15f51b17be600d5fc59a3ef5ffaa1607e209ccc6e611e2046f1a9005cffb693367a9
7
- data.tar.gz: 7d3496c675ed29a8ddaa35d3fbf4b538677a44016696c40020e518afa62526fa5e7ad2abec2b7339732adabb820fa037795c3d8597b0149a2cd0502c23c95883
6
+ metadata.gz: eef2c42ddf5d778e1b3cac59f464ba0816377424a132de4d2d9e41a65a49c5ba2743c53ca4c7278d542a83e3de804d6596408d9b2c6d3281a848b9f911fe764f
7
+ data.tar.gz: f188c65ace72e059b5ee91d878389ebb95c34f9fe0d64a819dee355f7b4e2e855a3907ddf92bfd79ca459d618709dfdb2d7aaec73fae8d38a75834ca3907a512
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
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
+
7
+ ## 1.5.1 (2022-10-19)
8
+
9
+ * Better error message for unknown code
10
+
3
11
  ## 1.5.0 (2022-10-17)
4
12
 
5
13
  * Remove `insert_after`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.5.0)
4
+ node_mutation (1.6.0)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
 
@@ -2,7 +2,12 @@
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)
@@ -35,13 +40,11 @@ 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 Synvert::Core::MethodNotSupported, "rewritten_source is not handled for #{evaluated.inspect}"
44
+ raise "can not parse \"#{code}\""
42
45
  end
43
46
  else
44
- "{{#{old_code}}}"
47
+ raise "can not parse \"#{code}\""
45
48
  end
46
49
  end
47
50
  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.0"
4
+ VERSION = "1.6.0"
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.0
4
+ version: 1.6.0
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-17 00:00:00.000000000 Z
11
+ date: 2022-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport