node_mutation 1.19.0 → 1.19.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +2 -2
- data/lib/node_mutation/adapter/parser.rb +6 -0
- data/lib/node_mutation/adapter/syntax_tree.rb +22 -2
- data/lib/node_mutation/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec537b5f02531d1a6134c09db1be0d0845226a231da01cbda1e9331103a04fb3
|
4
|
+
data.tar.gz: f5cf6c624b25b08de8ea2f82c46401a3ce530c58720fa47cac11f04445c11894
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d491e39ed40c9392c0bd4b1ca47cb2d6078756526b85d2ff5a01b9b5c7ec4cf452fe14c502a1066c878a00d532d88152d026e367b67b8c2de6904577e0c3ef0
|
7
|
+
data.tar.gz: d0946badaef12e2bc7701df604d7186174f72cc74d7dc797e38a82f20e05cfd6c07ea6d677c818bc16db583b735f4ae5652c799b7df2d664463977bd524d7b71
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
node_mutation (1.19.
|
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.
|
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
|
-
#
|
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
|
-
#
|
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)
|
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.
|
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-
|
11
|
+
date: 2023-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ast node mutation apis
|
14
14
|
email:
|