node_mutation 1.19.0 → 1.19.2

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: f0f3e24e2bcc4c8de744080aeccf3b82741534b6d5681c582d4ea91318b156b5
4
- data.tar.gz: 33a39316e45592619c5ad3d520c6cb42411e41b6704bc1c52d840e0414c46322
3
+ metadata.gz: ec537b5f02531d1a6134c09db1be0d0845226a231da01cbda1e9331103a04fb3
4
+ data.tar.gz: f5cf6c624b25b08de8ea2f82c46401a3ce530c58720fa47cac11f04445c11894
5
5
  SHA512:
6
- metadata.gz: 0064ba628586533f3687a2af1cdbfbd0af32a274bc127e7da97d72c5b4d7517c1f0a8658fba363762ede790b3ee1071fed6b702e75932f5e57719ff0a6f7c023
7
- data.tar.gz: 231809db15865e3c260b2aa67edf5335968968299b5e5135571a12631bb90ed563fdd1a9cd59996d909acee42b4770b1ca98c58734fcee26ea1255a2eb525371
6
+ metadata.gz: 7d491e39ed40c9392c0bd4b1ca47cb2d6078756526b85d2ff5a01b9b5c7ec4cf452fe14c502a1066c878a00d532d88152d026e367b67b8c2de6904577e0c3ef0
7
+ data.tar.gz: d0946badaef12e2bc7701df604d7186174f72cc74d7dc797e38a82f20e05cfd6c07ea6d677c818bc16db583b735f4ae5652c799b7df2d664463977bd524d7b71
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # NodeMutation
2
2
 
3
+ ## 1.19.2 (2023-06-30)
4
+
5
+ * Support `operator` of `Binary` node in `child_node_range`
6
+
7
+ ## 1.19.1 (2023-06-22)
8
+
9
+ * Add `to_string` function
10
+
3
11
  ## 1.19.0 (2023-06-22)
4
12
 
5
13
  * Drop support for function in `child_node_by_name`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_mutation (1.19.0)
4
+ node_mutation (1.19.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -62,7 +62,7 @@ GEM
62
62
  shellany (0.0.1)
63
63
  syntax_tree (6.1.1)
64
64
  prettier_print (>= 1.2.0)
65
- syntax_tree_ext (0.3.0)
65
+ syntax_tree_ext (0.6.1)
66
66
  syntax_tree
67
67
  thor (1.2.1)
68
68
 
@@ -47,6 +47,10 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
47
47
  # node = Parser::CurrentRuby.parse("'foo'")
48
48
  # rewritten_source(node, 'to_symbol') => ':foo'
49
49
  #
50
+ # # to_string for sym node
51
+ # node = Parser::CurrentRuby.parse(":foo")
52
+ # rewritten_source(node, 'to_string') => 'foo'
53
+ #
50
54
  # # to_lambda_literal for block node
51
55
  # node = Parser::CurrentRuby.parse('lambda { foobar }')
52
56
  # rewritten_source(node, 'to_lambda_literal') => '-> { foobar }'
@@ -276,6 +280,8 @@ class NodeMutation::ParserAdapter < NodeMutation::Adapter
276
280
  child_node = node.send(direct_child_name)
277
281
  elsif direct_child_name == 'to_symbol' && node.type == :str
278
282
  child_node = ":#{node.to_value}"
283
+ elsif direct_child_name == 'to_string' && node.type == :sym
284
+ child_node = node.to_value.to_s
279
285
  elsif direct_child_name == 'to_single_quote' && node.type == :str
280
286
  child_node = "'#{node.to_value}'"
281
287
  elsif direct_child_name == 'to_double_quote' && node.type == :str
@@ -44,6 +44,10 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
44
44
  # node = SyntaxTree::Parser.new("'foo'").parse.statements.body.first
45
45
  # rewritten_source(node, 'to_symbol') => ':foo'
46
46
  #
47
+ # # to_string for SymbolLiteral node
48
+ # node = SyntaxTree::Parser.new(":foo").parse.statements.body.first
49
+ # rewritten_source(node, 'to_string') => 'foo'
50
+ #
47
51
  # # to_lambda_literal for MethodAddBlock node
48
52
  # node = SyntaxTree::Parser.new('lambda { foobar }').parse.statements.body.first
49
53
  # rewritten_source(node, 'to_lambda_literal') => '-> { foobar }'
@@ -98,12 +102,18 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
98
102
  # @example
99
103
  # node = SyntaxTree::Parser.new('foo.bar(test)').parse.statements.body.first
100
104
  # child_node_range(node, 'receiver') => { start: 0, end: 'foo'.length }
101
- # node array
105
+ #
106
+ # # node array
102
107
  # node = SyntaxTree::Parser.new('foo.bar(a, b)').parse.statements.body.first
103
108
  # child_node_range(node, 'arguments.arguments') => { start: 'foo.bar('.length, end: 'foo.bar(a, b'.length }
104
- # index for node array
109
+ #
110
+ # # index for node array
105
111
  # node = SyntaxTree::Parser.new('foo.bar(a, b)').parse.statements.body.first
106
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 }
107
117
  def child_node_range(node, child_name)
108
118
  child_node = child_node_by_name(node, child_name)
109
119
  return nil if child_node.nil?
@@ -112,6 +122,14 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
112
122
  return NodeMutation::Struct::Range.new(child_node.first.location.start_char, child_node.last.location.end_char)
113
123
  end
114
124
 
125
+ if node.is_a?(SyntaxTree::Binary) && child_name == 'operator'
126
+ start_char = node.left.location.end_char
127
+ start_char += 1 while node.source[start_char] == ' '
128
+ end_char = node.right.location.start_char
129
+ end_char -= 1 while node.source[end_char - 1] == ' '
130
+ return NodeMutation::Struct::Range.new(start_char, end_char)
131
+ end
132
+
115
133
  return NodeMutation::Struct::Range.new(child_node.location.start_char, child_node.location.end_char)
116
134
  end
117
135
 
@@ -167,6 +185,8 @@ class NodeMutation::SyntaxTreeAdapter < NodeMutation::Adapter
167
185
  child_node = node.send(direct_child_name)
168
186
  elsif direct_child_name == 'to_symbol' && node.is_a?(SyntaxTree::StringLiteral)
169
187
  child_node = ":#{node.to_value}"
188
+ elsif direct_child_name == 'to_string' && node.is_a?(SyntaxTree::SymbolLiteral)
189
+ child_node = node.to_value.to_s
170
190
  elsif direct_child_name == 'to_single_quote' && node.is_a?(SyntaxTree::StringLiteral)
171
191
  child_node = "'#{node.to_value}'"
172
192
  elsif direct_child_name == 'to_double_quote' && node.is_a?(SyntaxTree::StringLiteral)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeMutation
4
- VERSION = "1.19.0"
4
+ VERSION = "1.19.2"
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.0
4
+ version: 1.19.2
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: