node_mutation 1.19.1 → 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: 07cffea51271875fe053793222f92ba3ed88fb977e90c875d7f6096b969de898
4
- data.tar.gz: f532736d3c384703dca1cfa389b4cad86fb810d8b063540ad50774af649786c8
3
+ metadata.gz: 8089525a411cf3e7400148cba0d3cd21b763d6ec034dd5a627c0faf2419d8ec3
4
+ data.tar.gz: c5e0137f1da08ac49ffa45124cc39cb095543e5b5d5cc788e32093c581de2e2d
5
5
  SHA512:
6
- metadata.gz: c900a66575d3c35780bf64f33aa3452a5cd558ba7e55cde46f0c322f5da8cf54f17b052683a71df74c51df7efda8e3626e451f987fd1ea5f3995afd3f72635f4
7
- data.tar.gz: f2335ad6e3ad0a5dcd1ec36d0f595c114d703dea847f36d6b1235a4ceb1cc92e6fe27522cd941da0e34e3864ccb9abd169f9f83e02a9306ab6075f8d2ea1c09d
6
+ metadata.gz: 0ecd574ae42a8cabdee2bdae86a14b905fdb4df3205af93753de47e9eb86f595371332f7b9dc5c560c3cd0b48db45d41680784b44fa4be9fba7ef36eefa8d9a7
7
+ data.tar.gz: c67d25471f221f0c9d7bd1bd6c40c95c2dfc995154dd9264ce4268eb4990c380ca38c7732a5da6c1b75518c68aeb8e3ea32c644259de12ce01559abac722d63a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.19.3 (2023-07-01)
4
+
5
+ * Rewrite `SyntaxTreeAdapter#child_node_range` to support Binary operator
6
+
7
+ ## 1.19.2 (2023-06-30)
8
+
9
+ * Support `operator` of `Binary` node in `child_node_range`
10
+
3
11
  ## 1.19.1 (2023-06-22)
4
12
 
5
13
  * Add `to_string` function
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.19.1)
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.3.0)
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
@@ -102,21 +102,66 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
102
102
  # @example
103
103
  # node = SyntaxTree::Parser.new('foo.bar(test)').parse.statements.body.first
104
104
  # child_node_range(node, 'receiver') => { start: 0, end: 'foo'.length }
105
- # node array
105
+ #
106
+ # # node array
106
107
  # node = SyntaxTree::Parser.new('foo.bar(a, b)').parse.statements.body.first
107
108
  # child_node_range(node, 'arguments.arguments') => { start: 'foo.bar('.length, end: 'foo.bar(a, b'.length }
108
- # index for node array
109
+ #
110
+ # # index for node array
109
111
  # node = SyntaxTree::Parser.new('foo.bar(a, b)').parse.statements.body.first
110
112
  # child_node_range(node, 'arguments.arguments.parts.-1') => { start: 'foo.bar(a, '.length, end: 'foo.bar(a, b'.length }
113
+ #
114
+ # # operator of Binary node
115
+ # node = SyntaxTree::Parser.new('foo | bar').parse.statements.body.first
116
+ # child_node_range(node, 'operator') => { start: 'foo '.length, end: 'foo |'.length }
111
117
  def child_node_range(node, child_name)
112
- child_node = child_node_by_name(node, child_name)
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
135
+
136
+ return NodeMutation::Struct::Range.new(child_node.location.start_char, child_node.location.end_char)
137
+ end
138
+
139
+ if node.is_a?(SyntaxTree::Binary) && child_name.to_sym == :operator
140
+ start_char = node.left.location.end_char
141
+ start_char += 1 while node.source[start_char] == ' '
142
+ end_char = node.right.location.start_char
143
+ end_char -= 1 while node.source[end_char - 1] == ' '
144
+ return NodeMutation::Struct::Range.new(start_char, end_char)
145
+ end
146
+
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
+
113
154
  return nil if child_node.nil?
114
155
 
115
- if child_node.is_a?(Array)
116
- return NodeMutation::Struct::Range.new(child_node.first.location.start_char, child_node.last.location.end_char)
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
+ )
117
160
  end
118
161
 
119
- return NodeMutation::Struct::Range.new(child_node.location.start_char, child_node.location.end_char)
162
+ return(
163
+ NodeMutation::Struct::Range.new(child_node.first.location.start_char, child_node.last.location.end_char)
164
+ )
120
165
  end
121
166
 
122
167
  def get_start(node, child_name = nil)
@@ -181,7 +226,7 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
181
226
  if node.block.block_var
182
227
  child_node = "->(#{node.block.block_var.params.to_source}) {#{node.block.bodystmt.to_source}}"
183
228
  else
184
- child_node = "-> {#{node.block.bodystmt.to_source }}"
229
+ child_node = "-> {#{node.block.bodystmt.to_source}}"
185
230
  end
186
231
  elsif direct_child_name == 'strip_curly_braces' && node.is_a?(SyntaxTree::HashLiteral)
187
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.1"
4
+ VERSION = "1.19.3"
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.19.1
4
+ version: 1.19.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-22 00:00:00.000000000 Z
11
+ date: 2023-06-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ast node mutation apis
14
14
  email: