node_mutation 1.19.2 → 1.19.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec537b5f02531d1a6134c09db1be0d0845226a231da01cbda1e9331103a04fb3
4
- data.tar.gz: f5cf6c624b25b08de8ea2f82c46401a3ce530c58720fa47cac11f04445c11894
3
+ metadata.gz: 8089525a411cf3e7400148cba0d3cd21b763d6ec034dd5a627c0faf2419d8ec3
4
+ data.tar.gz: c5e0137f1da08ac49ffa45124cc39cb095543e5b5d5cc788e32093c581de2e2d
5
5
  SHA512:
6
- metadata.gz: 7d491e39ed40c9392c0bd4b1ca47cb2d6078756526b85d2ff5a01b9b5c7ec4cf452fe14c502a1066c878a00d532d88152d026e367b67b8c2de6904577e0c3ef0
7
- data.tar.gz: d0946badaef12e2bc7701df604d7186174f72cc74d7dc797e38a82f20e05cfd6c07ea6d677c818bc16db583b735f4ae5652c799b7df2d664463977bd524d7b71
6
+ metadata.gz: 0ecd574ae42a8cabdee2bdae86a14b905fdb4df3205af93753de47e9eb86f595371332f7b9dc5c560c3cd0b48db45d41680784b44fa4be9fba7ef36eefa8d9a7
7
+ data.tar.gz: c67d25471f221f0c9d7bd1bd6c40c95c2dfc995154dd9264ce4268eb4990c380ca38c7732a5da6c1b75518c68aeb8e3ea32c644259de12ce01559abac722d63a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.19.3 (2023-07-01)
4
+
5
+ * Rewrite `SyntaxTreeAdapter#child_node_range` to support Binary operator
6
+
3
7
  ## 1.19.2 (2023-06-30)
4
8
 
5
9
  * Support `operator` of `Binary` node in `child_node_range`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.19.2)
4
+ node_mutation (1.19.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -34,14 +34,16 @@ GEM
34
34
  notiffany (0.1.3)
35
35
  nenv (~> 0.1)
36
36
  shellany (~> 0.0)
37
- parser (3.2.2.1)
37
+ parser (3.2.2.3)
38
38
  ast (~> 2.4.1)
39
- parser_node_ext (1.1.0)
39
+ racc
40
+ parser_node_ext (1.2.0)
40
41
  parser
41
42
  prettier_print (1.2.1)
42
43
  pry (0.14.1)
43
44
  coderay (~> 1.1)
44
45
  method_source (~> 1.0)
46
+ racc (1.7.1)
45
47
  rake (13.0.6)
46
48
  rb-fsevent (0.11.1)
47
49
  rb-inotify (0.10.1)
@@ -62,7 +64,7 @@ GEM
62
64
  shellany (0.0.1)
63
65
  syntax_tree (6.1.1)
64
66
  prettier_print (>= 1.2.0)
65
- syntax_tree_ext (0.6.1)
67
+ syntax_tree_ext (0.6.3)
66
68
  syntax_tree
67
69
  thor (1.2.1)
68
70
 
@@ -177,6 +177,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
177
177
  end
178
178
  when %i[class name], %i[const name], %i[cvar name], %i[def name], %i[defs name],
179
179
  %i[gvar name], %i[ivar name], %i[lvar name]
180
+
180
181
  NodeMutation::Struct::Range.new(node.loc.name.begin_pos, node.loc.name.end_pos)
181
182
  when %i[const double_colon]
182
183
  NodeMutation::Struct::Range.new(node.loc.double_colon.begin_pos, node.loc.double_colon.end_pos)
@@ -281,7 +282,7 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
281
282
  elsif direct_child_name == 'to_symbol' && node.type == :str
282
283
  child_node = ":#{node.to_value}"
283
284
  elsif direct_child_name == 'to_string' && node.type == :sym
284
- child_node = node.to_value.to_s
285
+ child_node = node.to_value.to_s
285
286
  elsif direct_child_name == 'to_single_quote' && node.type == :str
286
287
  child_node = "'#{node.to_value}'"
287
288
  elsif direct_child_name == 'to_double_quote' && node.type == :str
@@ -115,14 +115,28 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
115
115
  # node = SyntaxTree::Parser.new('foo | bar').parse.statements.body.first
116
116
  # child_node_range(node, 'operator') => { start: 'foo '.length, end: 'foo |'.length }
117
117
  def child_node_range(node, child_name)
118
- child_node = child_node_by_name(node, child_name)
119
- return nil if child_node.nil?
118
+ direct_child_name, nested_child_name = child_name.to_s.split('.', 2)
119
+
120
+ if node.is_a?(Array)
121
+ if direct_child_name =~ INDEX_REGEXP
122
+ child_node = node[direct_child_name.to_i]
123
+ raise NodeMutation::MethodNotSupported,
124
+ "#{direct_child_name} is not supported for #{get_source(node)}" unless child_node
125
+ return child_node_range(child_node, nested_child_name) if nested_child_name
126
+
127
+ return NodeMutation::Struct::Range.new(child_node.location.start_char, child_node.location.end_char)
128
+ end
129
+
130
+ raise NodeMutation::MethodNotSupported,
131
+ "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
132
+
133
+ child_node = node.send(direct_child_name)
134
+ return child_node_range(child_node, nested_child_name) if nested_child_name
120
135
 
121
- if child_node.is_a?(Array)
122
- return NodeMutation::Struct::Range.new(child_node.first.location.start_char, child_node.last.location.end_char)
136
+ return NodeMutation::Struct::Range.new(child_node.location.start_char, child_node.location.end_char)
123
137
  end
124
138
 
125
- if node.is_a?(SyntaxTree::Binary) && child_name == 'operator'
139
+ if node.is_a?(SyntaxTree::Binary) && child_name.to_sym == :operator
126
140
  start_char = node.left.location.end_char
127
141
  start_char += 1 while node.source[start_char] == ' '
128
142
  end_char = node.right.location.start_char
@@ -130,7 +144,24 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
130
144
  return NodeMutation::Struct::Range.new(start_char, end_char)
131
145
  end
132
146
 
133
- return NodeMutation::Struct::Range.new(child_node.location.start_char, child_node.location.end_char)
147
+ raise NodeMutation::MethodNotSupported,
148
+ "#{direct_child_name} is not supported for #{get_source(node)}" unless node.respond_to?(direct_child_name)
149
+
150
+ child_node = node.send(direct_child_name)
151
+
152
+ return child_node_range(child_node, nested_child_name) if nested_child_name
153
+
154
+ return nil if child_node.nil?
155
+
156
+ if child_node.is_a?(SyntaxTree::Node)
157
+ return(
158
+ NodeMutation::Struct::Range.new(child_node.location.start_char, child_node.location.end_char)
159
+ )
160
+ end
161
+
162
+ return(
163
+ NodeMutation::Struct::Range.new(child_node.first.location.start_char, child_node.last.location.end_char)
164
+ )
134
165
  end
135
166
 
136
167
  def get_start(node, child_name = nil)
@@ -195,7 +226,7 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
195
226
  if node.block.block_var
196
227
  child_node = "->(#{node.block.block_var.params.to_source}) {#{node.block.bodystmt.to_source}}"
197
228
  else
198
- child_node = "-> {#{node.block.bodystmt.to_source }}"
229
+ child_node = "-> {#{node.block.bodystmt.to_source}}"
199
230
  end
200
231
  elsif direct_child_name == 'strip_curly_braces' && node.is_a?(SyntaxTree::HashLiteral)
201
232
  child_node = node.to_source.sub(/^{(.*)}$/) { Regexp.last_match(1).strip }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.19.2"
4
+ VERSION = "1.19.3"
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.19.2
4
+ version: 1.19.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang