synvert-core 0.56.1 → 0.57.0
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 +4 -4
- data/CHANGELOG.md +5 -1
- data/lib/synvert/core/node_ext.rb +24 -3
- data/lib/synvert/core/rewriter/action/append_action.rb +3 -16
- data/lib/synvert/core/rewriter/action/delete_action.rb +5 -13
- data/lib/synvert/core/rewriter/action/insert_action.rb +3 -16
- data/lib/synvert/core/rewriter/action/insert_after_action.rb +3 -12
- data/lib/synvert/core/rewriter/action/prepend_action.rb +13 -21
- data/lib/synvert/core/rewriter/action/remove_action.rb +9 -17
- data/lib/synvert/core/rewriter/action/replace_action.rb +3 -12
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +3 -11
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +3 -12
- data/lib/synvert/core/rewriter/action/wrap_action.rb +3 -12
- data/lib/synvert/core/rewriter/action.rb +31 -10
- data/lib/synvert/core/rewriter/instance.rb +10 -10
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +10 -0
- data/spec/synvert/core/rewriter/action/append_action_spec.rb +2 -2
- data/spec/synvert/core/rewriter/action/delete_action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +2 -2
- data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/action/prepend_action_spec.rb +4 -4
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/action/replace_action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +2 -2
- data/spec/synvert/core/rewriter/action/wrap_action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/instance_spec.rb +32 -12
- 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: '01935a8e5222f3058d6bcca0057cbe684ce0a43dabeaeac97dac0aa17c230b79'
|
4
|
+
data.tar.gz: 427851e9745c4c960b1f437c397e9332ebf6a8df4126c58652927c9810db7d05
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50a73df1cb5034afeb269776f5e4bf76873b236e490fa54e6bffd91dabfb2726260b99bca95230022314be890454645b8c032c4006c22271a7a460c50b77e3dc
|
7
|
+
data.tar.gz: c7ca6e209b229a3951b6846173268d254034e3ff9480e47add2e7649326ab004d806f09ec784fdf86154c8414c7e9e947e6ea72e6fbc5c2d65018f1909928e18
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.57.0 (2021-10-02)
|
4
4
|
|
5
5
|
* Compare ruby version in `.ruby-version` or `.rvmrc`
|
6
|
+
* Support `left_value` and `right_value` for `casgn` node
|
7
|
+
* Refactor - `calculate_position` to set `begin_pos` and `end_pos`
|
6
8
|
* Abstract `squeeze_spaces` and `squeeze_lines`
|
9
|
+
* Remove unused comma after delete/remove action
|
10
|
+
* Handle array child node in `childNodeRange`
|
7
11
|
|
8
12
|
## 0.56.0 (2021-09-14)
|
9
13
|
|
@@ -215,7 +215,7 @@ module Parser::AST
|
|
215
215
|
# @return [Parser::AST::Node] variable nodes.
|
216
216
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
217
217
|
def left_value
|
218
|
-
if %i[masgn lvasgn ivasgn].include? type
|
218
|
+
if %i[masgn lvasgn ivasgn cvasgn].include? type
|
219
219
|
children[0]
|
220
220
|
elsif :or_asgn == type
|
221
221
|
children[0].children[0]
|
@@ -229,7 +229,7 @@ module Parser::AST
|
|
229
229
|
# @return [Array<Parser::AST::Node>] variable nodes.
|
230
230
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
231
231
|
def right_value
|
232
|
-
if %i[masgn lvasgn ivasgn or_asgn].include? type
|
232
|
+
if %i[masgn lvasgn ivasgn cvasgn or_asgn].include? type
|
233
233
|
children[1]
|
234
234
|
else
|
235
235
|
raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{debug_info}"
|
@@ -373,7 +373,28 @@ module Parser::AST
|
|
373
373
|
if respond_to?(direct_child_name)
|
374
374
|
child_node = send(direct_child_name)
|
375
375
|
|
376
|
-
|
376
|
+
if nested_child_name
|
377
|
+
if child_node.is_a?(Array)
|
378
|
+
child_direct_child_name, *child_nested_child_name = nested_child_name
|
379
|
+
child_direct_child_node = child_direct_child_name =~ /\A\d+\z/ ? child_node[child_direct_child_name] : child_node.send(child_direct_child_name)
|
380
|
+
if child_nested_child_name.length > 0
|
381
|
+
return child_direct_child_node.child_node_range(child_nested_child_name.join('.'))
|
382
|
+
elsif child_direct_child_node
|
383
|
+
return (
|
384
|
+
Parser::Source::Range.new(
|
385
|
+
'(string)',
|
386
|
+
child_direct_child_node.loc.expression.begin_pos,
|
387
|
+
child_direct_child_node.loc.expression.end_pos
|
388
|
+
)
|
389
|
+
)
|
390
|
+
else
|
391
|
+
raise Synvert::Core::MethodNotSupported,
|
392
|
+
"child_node_range is not handled for #{debug_info}, child_name: #{child_name}"
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
return child_node.child_node_range(nested_child_name)
|
397
|
+
end
|
377
398
|
|
378
399
|
return nil if child_node.nil?
|
379
400
|
|
@@ -5,22 +5,9 @@ module Synvert::Core
|
|
5
5
|
class Rewriter::AppendAction < Rewriter::Action
|
6
6
|
END_LENGTH = "\nend".length
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def begin_pos
|
12
|
-
if :begin == @node.type
|
13
|
-
@node.loc.expression.end_pos
|
14
|
-
else
|
15
|
-
@node.loc.expression.end_pos - @node.column - END_LENGTH
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# End position, always same to begin position.
|
20
|
-
#
|
21
|
-
# @return [Integer] end position.
|
22
|
-
def end_pos
|
23
|
-
begin_pos
|
8
|
+
def calculate_position
|
9
|
+
@begin_pos = :begin == @node.type ? @node.loc.expression.end_pos : @node.loc.expression.end_pos - @node.column - END_LENGTH
|
10
|
+
@end_pos = @begin_pos
|
24
11
|
end
|
25
12
|
|
26
13
|
private
|
@@ -8,19 +8,11 @@ module Synvert::Core
|
|
8
8
|
@selectors = selectors
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
squeeze_spaces(pos, end_pos)
|
17
|
-
end
|
18
|
-
|
19
|
-
# End position of code to delete.
|
20
|
-
#
|
21
|
-
# @return [Integer] end position.
|
22
|
-
def end_pos
|
23
|
-
@selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:end_pos).max
|
11
|
+
def calculate_position
|
12
|
+
@begin_pos = @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:begin_pos).min
|
13
|
+
@end_pos = @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:end_pos).max
|
14
|
+
squeeze_spaces
|
15
|
+
remove_comma
|
24
16
|
end
|
25
17
|
|
26
18
|
# The rewritten code, always empty string.
|
@@ -8,22 +8,9 @@ module Synvert::Core
|
|
8
8
|
@at = at
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def begin_pos
|
15
|
-
if @at == 'end'
|
16
|
-
@node.loc.expression.end_pos
|
17
|
-
else
|
18
|
-
@node.loc.expression.begin_pos
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# End position, always same to begin position.
|
23
|
-
#
|
24
|
-
# @return [Integer] end position.
|
25
|
-
def end_pos
|
26
|
-
begin_pos
|
11
|
+
def calculate_position
|
12
|
+
@begin_pos = @at == 'end' ? @node.loc.expression.end_pos : @node.loc.expression.begin_pos
|
13
|
+
@end_pos = @begin_pos
|
27
14
|
end
|
28
15
|
|
29
16
|
# The rewritten source code.
|
@@ -3,18 +3,9 @@
|
|
3
3
|
module Synvert::Core
|
4
4
|
# InsertAfterAction to insert code next to the node.
|
5
5
|
class Rewriter::InsertAfterAction < Rewriter::Action
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def begin_pos
|
10
|
-
@node.loc.expression.end_pos
|
11
|
-
end
|
12
|
-
|
13
|
-
# End position, always same to begin position.
|
14
|
-
#
|
15
|
-
# @return [Integer] end position.
|
16
|
-
def end_pos
|
17
|
-
begin_pos
|
6
|
+
def calculate_position
|
7
|
+
@begin_pos = @node.loc.expression.end_pos
|
8
|
+
@end_pos = @begin_pos
|
18
9
|
end
|
19
10
|
|
20
11
|
private
|
@@ -5,29 +5,21 @@ module Synvert::Core
|
|
5
5
|
class Rewriter::PrependAction < Rewriter::Action
|
6
6
|
DO_LENGTH = ' do'.length
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
def calculate_position
|
9
|
+
@begin_pos =
|
10
|
+
case @node.type
|
11
|
+
when :block
|
12
|
+
if @node.children[1].children.empty?
|
13
|
+
@node.children[0].loc.expression.end_pos + DO_LENGTH
|
14
|
+
else
|
15
|
+
@node.children[1].loc.expression.end_pos
|
16
|
+
end
|
17
|
+
when :class
|
18
|
+
@node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
|
16
19
|
else
|
17
|
-
@node.children
|
20
|
+
@node.children.last.loc.expression.end_pos
|
18
21
|
end
|
19
|
-
|
20
|
-
@node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
|
21
|
-
else
|
22
|
-
@node.children.last.loc.expression.end_pos
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# End position, always same to begin position.
|
27
|
-
#
|
28
|
-
# @return [Integer] end position.
|
29
|
-
def end_pos
|
30
|
-
begin_pos
|
22
|
+
@end_pos = @begin_pos
|
31
23
|
end
|
32
24
|
|
33
25
|
private
|
@@ -10,23 +10,16 @@ module Synvert::Core
|
|
10
10
|
# Begin position of code to replace.
|
11
11
|
#
|
12
12
|
# @return [Integer] begin position.
|
13
|
-
def
|
13
|
+
def calculate_position
|
14
14
|
if take_whole_line?
|
15
|
-
start_index
|
15
|
+
@begin_pos = start_index
|
16
|
+
@end_pos = end_index
|
17
|
+
squeeze_lines
|
16
18
|
else
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
# End position of code to replace.
|
23
|
-
#
|
24
|
-
# @return [Integer] end position.
|
25
|
-
def end_pos
|
26
|
-
if take_whole_line?
|
27
|
-
end_index
|
28
|
-
else
|
29
|
-
@node.loc.expression.end_pos
|
19
|
+
@begin_pos = @node.loc.expression.begin_pos
|
20
|
+
@end_pos = @node.loc.expression.end_pos
|
21
|
+
squeeze_spaces
|
22
|
+
remove_comma
|
30
23
|
end
|
31
24
|
end
|
32
25
|
|
@@ -48,8 +41,7 @@ module Synvert::Core
|
|
48
41
|
|
49
42
|
def end_index
|
50
43
|
index = file_source[@node.loc.expression.end_pos..-1].index("\n")
|
51
|
-
|
52
|
-
squeeze_lines(pos, @node.loc.expression.first_line, @node.loc.expression.last_line)
|
44
|
+
index ? @node.loc.expression.end_pos + index + "\n".length : @node.loc.expression.end_pos
|
53
45
|
end
|
54
46
|
end
|
55
47
|
end
|
@@ -8,18 +8,9 @@ module Synvert::Core
|
|
8
8
|
@selectors = selectors
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def begin_pos
|
15
|
-
@selectors.map { |selector| @node.child_node_range(selector).begin_pos }.min
|
16
|
-
end
|
17
|
-
|
18
|
-
# End position of code to replace.
|
19
|
-
#
|
20
|
-
# @return [Integer] end position.
|
21
|
-
def end_pos
|
22
|
-
@selectors.map { |selector| @node.child_node_range(selector).end_pos }.max
|
11
|
+
def calculate_position
|
12
|
+
@begin_pos = @selectors.map { |selector| @node.child_node_range(selector).begin_pos }.min
|
13
|
+
@end_pos = @selectors.map { |selector| @node.child_node_range(selector).end_pos }.max
|
23
14
|
end
|
24
15
|
|
25
16
|
# The rewritten source code.
|
@@ -8,25 +8,17 @@ module Synvert::Core
|
|
8
8
|
super
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
#
|
13
|
-
# @return [Integer] begin position.
|
14
|
-
def begin_pos
|
11
|
+
def calculate_position
|
15
12
|
node_begin_pos = @node.loc.expression.begin_pos
|
16
13
|
while @node.loc.expression.source_buffer.source[node_begin_pos -= 1] == ' '
|
17
14
|
end
|
18
|
-
node_begin_pos - Engine::ERUBY_STMT_SPLITTER.length + 1
|
19
|
-
end
|
15
|
+
@begin_pos = node_begin_pos - Engine::ERUBY_STMT_SPLITTER.length + 1
|
20
16
|
|
21
|
-
# End position of code to replace.
|
22
|
-
#
|
23
|
-
# @return [Integer] end position.
|
24
|
-
def end_pos
|
25
17
|
node_begin_pos = @node.loc.expression.begin_pos
|
26
18
|
node_begin_pos += @node.loc.expression.source.index 'do'
|
27
19
|
while @node.loc.expression.source_buffer.source[node_begin_pos += 1] != '@'
|
28
20
|
end
|
29
|
-
node_begin_pos
|
21
|
+
@end_pos = node_begin_pos
|
30
22
|
end
|
31
23
|
|
32
24
|
# The rewritten erb expr code.
|
@@ -3,18 +3,9 @@
|
|
3
3
|
module Synvert::Core
|
4
4
|
# ReplaceWithAction to replace code.
|
5
5
|
class Rewriter::ReplaceWithAction < Rewriter::Action
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def begin_pos
|
10
|
-
@node.loc.expression.begin_pos
|
11
|
-
end
|
12
|
-
|
13
|
-
# End position of code to replace.
|
14
|
-
#
|
15
|
-
# @return [Integer] end position.
|
16
|
-
def end_pos
|
17
|
-
@node.loc.expression.end_pos
|
6
|
+
def calculate_position
|
7
|
+
@begin_pos = @node.loc.expression.begin_pos
|
8
|
+
@end_pos = @node.loc.expression.end_pos
|
18
9
|
end
|
19
10
|
|
20
11
|
# The rewritten source code with proper indent.
|
@@ -11,18 +11,9 @@ module Synvert::Core
|
|
11
11
|
@indent = indent || @node.column
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
def begin_pos
|
18
|
-
@node.loc.expression.begin_pos
|
19
|
-
end
|
20
|
-
|
21
|
-
# End position of code to wrap.
|
22
|
-
#
|
23
|
-
# @return [Integer] end position.
|
24
|
-
def end_pos
|
25
|
-
@node.loc.expression.end_pos
|
14
|
+
def calculate_position
|
15
|
+
@begin_pos = @node.loc.expression.begin_pos
|
16
|
+
@end_pos = @node.loc.expression.end_pos
|
26
17
|
end
|
27
18
|
|
28
19
|
# The rewritten source code.
|
@@ -5,6 +5,12 @@ module Synvert::Core
|
|
5
5
|
class Rewriter::Action
|
6
6
|
DEFAULT_INDENT = 2
|
7
7
|
|
8
|
+
# @!attribute [r] begin_pos
|
9
|
+
# @return [Integer] begin position
|
10
|
+
# @!attribute [r] end_pos
|
11
|
+
# @return [Integer] end position
|
12
|
+
attr_reader :begin_pos, :end_pos
|
13
|
+
|
8
14
|
# Initialize an action.
|
9
15
|
#
|
10
16
|
# @param instance [Synvert::Core::Rewriter::Instance]
|
@@ -15,6 +21,11 @@ module Synvert::Core
|
|
15
21
|
@node = @instance.current_node
|
16
22
|
end
|
17
23
|
|
24
|
+
def process
|
25
|
+
calculate_position
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
18
29
|
# Line number of current node.
|
19
30
|
#
|
20
31
|
# @return [Integer] line number.
|
@@ -42,23 +53,33 @@ module Synvert::Core
|
|
42
53
|
@rewritten_source ||= @node.rewritten_source(@code)
|
43
54
|
end
|
44
55
|
|
45
|
-
def squeeze_spaces
|
46
|
-
if file_source[begin_pos - 1] == ' ' && file_source[end_pos] == ' '
|
47
|
-
begin_pos - 1
|
48
|
-
else
|
49
|
-
begin_pos
|
56
|
+
def squeeze_spaces
|
57
|
+
if file_source[@begin_pos - 1] == ' ' && file_source[@end_pos] == ' '
|
58
|
+
@begin_pos = @begin_pos - 1
|
50
59
|
end
|
51
60
|
end
|
52
61
|
|
53
|
-
def squeeze_lines
|
62
|
+
def squeeze_lines
|
54
63
|
lines = file_source.split("\n")
|
64
|
+
begin_line = @node.loc.expression.first_line
|
65
|
+
end_line = @node.loc.expression.last_line
|
55
66
|
before_line_is_blank = begin_line == 1 || lines[begin_line - 2] == ''
|
56
67
|
after_line_is_blank = lines[end_line] == ''
|
57
68
|
|
58
|
-
if before_line_is_blank && after_line_is_blank
|
59
|
-
end_pos + "\n".length
|
60
|
-
|
61
|
-
|
69
|
+
if lines.length > 1 && before_line_is_blank && after_line_is_blank
|
70
|
+
@end_pos = @end_pos + "\n".length
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def remove_comma
|
75
|
+
if ',' == file_source[@begin_pos - 1]
|
76
|
+
@begin_pos = @begin_pos - 1
|
77
|
+
elsif ', ' == file_source[@begin_pos - 2, 2]
|
78
|
+
@begin_pos = @begin_pos - 2
|
79
|
+
elsif ', ' == file_source[@end_pos, 2]
|
80
|
+
@end_pos = @end_pos + 2
|
81
|
+
elsif ',' == file_source[@end_pos]
|
82
|
+
@end_pos = @end_pos + 1
|
62
83
|
end
|
63
84
|
end
|
64
85
|
|
@@ -221,7 +221,7 @@ module Synvert::Core
|
|
221
221
|
#
|
222
222
|
# @param code [String] code need to be appended.
|
223
223
|
def append(code)
|
224
|
-
@actions << Rewriter::AppendAction.new(self, code)
|
224
|
+
@actions << Rewriter::AppendAction.new(self, code).process
|
225
225
|
end
|
226
226
|
|
227
227
|
# Parse prepend dsl, it creates a [Synvert::Core::Rewriter::PrependAction] to
|
@@ -229,7 +229,7 @@ module Synvert::Core
|
|
229
229
|
#
|
230
230
|
# @param code [String] code need to be prepended.
|
231
231
|
def prepend(code)
|
232
|
-
@actions << Rewriter::PrependAction.new(self, code)
|
232
|
+
@actions << Rewriter::PrependAction.new(self, code).process
|
233
233
|
end
|
234
234
|
|
235
235
|
# Parse insert dsl, it creates a [Synvert::Core::Rewriter::InsertAction] to
|
@@ -238,7 +238,7 @@ module Synvert::Core
|
|
238
238
|
# @param code [String] code need to be inserted.
|
239
239
|
# @param at [String] insert position, beginning or end, end is the default.
|
240
240
|
def insert(code, at: 'end')
|
241
|
-
@actions << Rewriter::InsertAction.new(self, code, at: at)
|
241
|
+
@actions << Rewriter::InsertAction.new(self, code, at: at).process
|
242
242
|
end
|
243
243
|
|
244
244
|
# Parse insert_after dsl, it creates a [Synvert::Core::Rewriter::InsertAfterAction] to
|
@@ -246,7 +246,7 @@ module Synvert::Core
|
|
246
246
|
#
|
247
247
|
# @param code [String] code need to be inserted.
|
248
248
|
def insert_after(node)
|
249
|
-
@actions << Rewriter::InsertAfterAction.new(self, node)
|
249
|
+
@actions << Rewriter::InsertAfterAction.new(self, node).process
|
250
250
|
end
|
251
251
|
|
252
252
|
# Parse replace_with dsl, it creates a [Synvert::Core::Rewriter::ReplaceWithAction] to
|
@@ -254,7 +254,7 @@ module Synvert::Core
|
|
254
254
|
#
|
255
255
|
# @param code [String] code need to be replaced with.
|
256
256
|
def replace_with(code)
|
257
|
-
@actions << Rewriter::ReplaceWithAction.new(self, code)
|
257
|
+
@actions << Rewriter::ReplaceWithAction.new(self, code).process
|
258
258
|
end
|
259
259
|
|
260
260
|
# Parse replace with dsl, it creates a [Synvert::Core::Rewriter::ReplaceAction] to
|
@@ -263,25 +263,25 @@ module Synvert::Core
|
|
263
263
|
# @param selectors [Array<Symbol>] selector names of child node.
|
264
264
|
# @param with [String] code need to be replaced with.
|
265
265
|
def replace(*selectors, with:)
|
266
|
-
@actions << Rewriter::ReplaceAction.new(self, *selectors, with: with)
|
266
|
+
@actions << Rewriter::ReplaceAction.new(self, *selectors, with: with).process
|
267
267
|
end
|
268
268
|
|
269
269
|
# Parse replace_erb_stmt_with_expr dsl, it creates a [Synvert::Core::Rewriter::ReplaceErbStmtWithExprAction] to
|
270
270
|
# replace erb stmt code to expr code.
|
271
271
|
def replace_erb_stmt_with_expr
|
272
|
-
@actions << Rewriter::ReplaceErbStmtWithExprAction.new(self)
|
272
|
+
@actions << Rewriter::ReplaceErbStmtWithExprAction.new(self).process
|
273
273
|
end
|
274
274
|
|
275
275
|
# Parse remove dsl, it creates a [Synvert::Core::Rewriter::RemoveAction] to remove current node.
|
276
276
|
def remove
|
277
|
-
@actions << Rewriter::RemoveAction.new(self)
|
277
|
+
@actions << Rewriter::RemoveAction.new(self).process
|
278
278
|
end
|
279
279
|
|
280
280
|
# Parse delete dsl, it creates a [Synvert::Core::Rewriter::DeleteAction] to delete child nodes.
|
281
281
|
#
|
282
282
|
# @param selectors [Array<Symbol>] selector names of child node.
|
283
283
|
def delete(*selectors)
|
284
|
-
@actions << Rewriter::DeleteAction.new(self, *selectors)
|
284
|
+
@actions << Rewriter::DeleteAction.new(self, *selectors).process
|
285
285
|
end
|
286
286
|
|
287
287
|
# Parse wrap with dsl, it creates a [Synvert::Core::Rewriter::WrapAction] to
|
@@ -290,7 +290,7 @@ module Synvert::Core
|
|
290
290
|
# @param with [String] code need to be wrapped with.
|
291
291
|
# @param indent [Integer] number of whitespaces.
|
292
292
|
def wrap(with:, indent: nil)
|
293
|
-
@actions << Rewriter::WrapAction.new(self, with: with, indent: indent)
|
293
|
+
@actions << Rewriter::WrapAction.new(self, with: with, indent: indent).process
|
294
294
|
end
|
295
295
|
|
296
296
|
# Parse warn dsl, it creates a [Synvert::Core::Rewriter::Warning] to save warning message.
|
data/lib/synvert/core/version.rb
CHANGED
@@ -282,6 +282,11 @@ describe Parser::AST::Node do
|
|
282
282
|
expect(node.left_value).to eq :@a
|
283
283
|
end
|
284
284
|
|
285
|
+
it 'gets for cvasgn' do
|
286
|
+
node = parse('@@a = 1')
|
287
|
+
expect(node.left_value).to eq :@@a
|
288
|
+
end
|
289
|
+
|
285
290
|
it 'gets for or_asgn' do
|
286
291
|
node = parse('a ||= 1')
|
287
292
|
expect(node.left_value).to eq :a
|
@@ -309,6 +314,11 @@ describe Parser::AST::Node do
|
|
309
314
|
expect(node.right_value).to eq parse('1')
|
310
315
|
end
|
311
316
|
|
317
|
+
it 'gets for cvasgn' do
|
318
|
+
node = parse('@@a = 1')
|
319
|
+
expect(node.right_value).to eq parse('1')
|
320
|
+
end
|
321
|
+
|
312
322
|
it 'gets for or_asgn' do
|
313
323
|
node = parse('a ||= 1')
|
314
324
|
expect(node.right_value).to eq parse('1')
|
@@ -9,7 +9,7 @@ module Synvert::Core
|
|
9
9
|
source = "class User\n has_many :posts\nend"
|
10
10
|
class_node = Parser::CurrentRuby.parse(source)
|
11
11
|
instance = double(current_node: class_node)
|
12
|
-
Rewriter::AppendAction.new(instance, "def as_json\n super\nend")
|
12
|
+
Rewriter::AppendAction.new(instance, "def as_json\n super\nend").process
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'gets begin_pos' do
|
@@ -30,7 +30,7 @@ module Synvert::Core
|
|
30
30
|
source = "gem 'rails'\ngem 'mysql2'"
|
31
31
|
begin_node = Parser::CurrentRuby.parse(source)
|
32
32
|
instance = double(current_node: begin_node)
|
33
|
-
Rewriter::AppendAction.new(instance, "gem 'twitter'")
|
33
|
+
Rewriter::AppendAction.new(instance, "gem 'twitter'").process
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'gets begin_pos' do
|
@@ -8,7 +8,7 @@ module Synvert::Core
|
|
8
8
|
source = 'arr.map {}.flatten'
|
9
9
|
node = Parser::CurrentRuby.parse(source)
|
10
10
|
instance = double(current_node: node, file_source: source)
|
11
|
-
Rewriter::DeleteAction.new(instance, :dot, :message)
|
11
|
+
Rewriter::DeleteAction.new(instance, :dot, :message).process
|
12
12
|
}
|
13
13
|
|
14
14
|
it 'gets begin_pos' do
|
@@ -9,7 +9,7 @@ module Synvert::Core
|
|
9
9
|
source = " User.where(username: 'Richard')"
|
10
10
|
node = Parser::CurrentRuby.parse(source)
|
11
11
|
instance = double(current_node: node)
|
12
|
-
Rewriter::InsertAction.new(instance, '.first', at: 'end')
|
12
|
+
Rewriter::InsertAction.new(instance, '.first', at: 'end').process
|
13
13
|
}
|
14
14
|
|
15
15
|
it 'gets begin_pos' do
|
@@ -30,7 +30,7 @@ module Synvert::Core
|
|
30
30
|
source = " open('http://test.com')"
|
31
31
|
node = Parser::CurrentRuby.parse(source)
|
32
32
|
instance = double(current_node: node)
|
33
|
-
Rewriter::InsertAction.new(instance, 'URI.', at: 'beginning')
|
33
|
+
Rewriter::InsertAction.new(instance, 'URI.', at: 'beginning').process
|
34
34
|
}
|
35
35
|
|
36
36
|
it 'gets begin_pos' do
|
@@ -8,7 +8,7 @@ module Synvert::Core
|
|
8
8
|
source = ' include Foo'
|
9
9
|
node = Parser::CurrentRuby.parse(source)
|
10
10
|
instance = double(current_node: node)
|
11
|
-
Rewriter::InsertAfterAction.new(instance, 'include Bar')
|
11
|
+
Rewriter::InsertAfterAction.new(instance, 'include Bar').process
|
12
12
|
}
|
13
13
|
|
14
14
|
it 'gets begin_pos' do
|
@@ -9,7 +9,7 @@ module Synvert::Core
|
|
9
9
|
source = "Synvert::Application.configure do\nend"
|
10
10
|
block_node = Parser::CurrentRuby.parse(source)
|
11
11
|
instance = double(current_node: block_node)
|
12
|
-
Rewriter::PrependAction.new(instance, 'config.eager_load = true')
|
12
|
+
Rewriter::PrependAction.new(instance, 'config.eager_load = true').process
|
13
13
|
}
|
14
14
|
|
15
15
|
it 'gets begin_pos' do
|
@@ -30,7 +30,7 @@ module Synvert::Core
|
|
30
30
|
source = "RSpec.configure do |config|\nend"
|
31
31
|
block_node = Parser::CurrentRuby.parse(source)
|
32
32
|
instance = double(current_node: block_node)
|
33
|
-
Rewriter::PrependAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods')
|
33
|
+
Rewriter::PrependAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods').process
|
34
34
|
}
|
35
35
|
|
36
36
|
it 'gets begin_pos' do
|
@@ -51,7 +51,7 @@ module Synvert::Core
|
|
51
51
|
source = "class User\n has_many :posts\nend"
|
52
52
|
class_node = Parser::CurrentRuby.parse(source)
|
53
53
|
instance = double(current_node: class_node)
|
54
|
-
Rewriter::PrependAction.new(instance, 'include Deletable')
|
54
|
+
Rewriter::PrependAction.new(instance, 'include Deletable').process
|
55
55
|
}
|
56
56
|
|
57
57
|
it 'gets begin_pos' do
|
@@ -72,7 +72,7 @@ module Synvert::Core
|
|
72
72
|
source = "class User < ActiveRecord::Base\n has_many :posts\nend"
|
73
73
|
class_node = Parser::CurrentRuby.parse(source)
|
74
74
|
instance = double(current_node: class_node)
|
75
|
-
Rewriter::PrependAction.new(instance, 'include Deletable')
|
75
|
+
Rewriter::PrependAction.new(instance, 'include Deletable').process
|
76
76
|
}
|
77
77
|
|
78
78
|
it 'gets begin_pos' do
|
@@ -8,7 +8,7 @@ module Synvert::Core
|
|
8
8
|
source = "user = User.new params[:user]\nuser.save\nrender\n"
|
9
9
|
send_node = Parser::CurrentRuby.parse(source).children[1]
|
10
10
|
instance = double(current_node: send_node, file_source: source)
|
11
|
-
Rewriter::RemoveAction.new(instance)
|
11
|
+
Rewriter::RemoveAction.new(instance).process
|
12
12
|
}
|
13
13
|
|
14
14
|
it 'gets begin_pos' do
|
@@ -9,7 +9,7 @@ module Synvert::Core
|
|
9
9
|
source = 'FactoryBot.create(:user)'
|
10
10
|
node = Parser::CurrentRuby.parse(source)
|
11
11
|
instance = double(current_node: node)
|
12
|
-
Rewriter::ReplaceAction.new(instance, :receiver, :dot, :message, with: 'create')
|
12
|
+
Rewriter::ReplaceAction.new(instance, :receiver, :dot, :message, with: 'create').process
|
13
13
|
}
|
14
14
|
|
15
15
|
it 'gets begin_pos' do
|
@@ -9,7 +9,7 @@ module Synvert::Core
|
|
9
9
|
source = 'post = FactoryGirl.create_list :post, 2'
|
10
10
|
send_node = Parser::CurrentRuby.parse(source).children[1]
|
11
11
|
instance = double(current_node: send_node)
|
12
|
-
Rewriter::ReplaceWithAction.new(instance, 'create_list {{arguments}}')
|
12
|
+
Rewriter::ReplaceWithAction.new(instance, 'create_list {{arguments}}').process
|
13
13
|
}
|
14
14
|
|
15
15
|
it 'gets begin_pos' do
|
@@ -30,7 +30,7 @@ module Synvert::Core
|
|
30
30
|
source = ' its(:size) { should == 1 }'
|
31
31
|
send_node = Parser::CurrentRuby.parse(source)
|
32
32
|
instance = double(current_node: send_node)
|
33
|
-
Rewriter::ReplaceWithAction.new(instance, <<~EOS)
|
33
|
+
Rewriter::ReplaceWithAction.new(instance, <<~EOS).process
|
34
34
|
describe '#size' do
|
35
35
|
subject { super().size }
|
36
36
|
it { {{body}} }
|
@@ -8,7 +8,7 @@ module Synvert::Core
|
|
8
8
|
source = "class Bar\nend"
|
9
9
|
node = Parser::CurrentRuby.parse(source)
|
10
10
|
instance = double(current_node: node)
|
11
|
-
Rewriter::WrapAction.new(instance, with: 'module Foo')
|
11
|
+
Rewriter::WrapAction.new(instance, with: 'module Foo').process
|
12
12
|
}
|
13
13
|
|
14
14
|
it 'gets begin_pos' do
|
@@ -90,58 +90,78 @@ module Synvert::Core
|
|
90
90
|
end
|
91
91
|
|
92
92
|
it 'parses append' do
|
93
|
-
|
93
|
+
action = double
|
94
|
+
expect(Rewriter::AppendAction).to receive(:new).with(instance, 'include FactoryGirl::Syntax::Methods').and_return(action)
|
95
|
+
expect(action).to receive(:process)
|
94
96
|
instance.append 'include FactoryGirl::Syntax::Methods'
|
95
97
|
end
|
96
98
|
|
97
99
|
it 'parses prepend' do
|
100
|
+
action = double
|
98
101
|
expect(Rewriter::PrependAction).to receive(:new).with(
|
99
102
|
instance,
|
100
103
|
'{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
101
|
-
)
|
104
|
+
).and_return(action)
|
105
|
+
expect(action).to receive(:process)
|
102
106
|
instance.prepend '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
103
107
|
end
|
104
108
|
|
105
|
-
it 'parses insert' do
|
106
|
-
|
109
|
+
it 'parses insert at end' do
|
110
|
+
action = double
|
111
|
+
expect(Rewriter::InsertAction).to receive(:new).with(instance, '.first', at: 'end').and_return(action)
|
112
|
+
expect(action).to receive(:process)
|
107
113
|
instance.insert '.first'
|
108
114
|
end
|
109
115
|
|
110
|
-
it 'parses insert' do
|
111
|
-
|
116
|
+
it 'parses insert at beginning' do
|
117
|
+
action = double
|
118
|
+
expect(Rewriter::InsertAction).to receive(:new).with(instance, 'URI.', at: 'beginning').and_return(action)
|
119
|
+
expect(action).to receive(:process)
|
112
120
|
instance.insert 'URI.', at: 'beginning'
|
113
121
|
end
|
114
122
|
|
115
123
|
it 'parses insert_after' do
|
124
|
+
action = double
|
116
125
|
expect(Rewriter::InsertAfterAction).to receive(:new).with(
|
117
126
|
instance,
|
118
127
|
'{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
119
|
-
)
|
128
|
+
).and_return(action)
|
129
|
+
expect(action).to receive(:process)
|
120
130
|
instance.insert_after '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
121
131
|
end
|
122
132
|
|
123
133
|
it 'parses replace_with' do
|
124
|
-
|
134
|
+
action = double
|
135
|
+
expect(Rewriter::ReplaceWithAction).to receive(:new).with(instance, 'create {{arguments}}').and_return(action)
|
136
|
+
expect(action).to receive(:process)
|
125
137
|
instance.replace_with 'create {{arguments}}'
|
126
138
|
end
|
127
139
|
|
128
140
|
it 'parses replace with' do
|
129
|
-
|
141
|
+
action = double
|
142
|
+
expect(Rewriter::ReplaceAction).to receive(:new).with(instance, :message, with: 'test').and_return(action)
|
143
|
+
expect(action).to receive(:process)
|
130
144
|
instance.replace :message, with: 'test'
|
131
145
|
end
|
132
146
|
|
133
147
|
it 'parses remove' do
|
134
|
-
|
148
|
+
action = double
|
149
|
+
expect(Rewriter::RemoveAction).to receive(:new).with(instance).and_return(action)
|
150
|
+
expect(action).to receive(:process)
|
135
151
|
instance.remove
|
136
152
|
end
|
137
153
|
|
138
154
|
it 'parses remove' do
|
139
|
-
|
155
|
+
action = double
|
156
|
+
expect(Rewriter::DeleteAction).to receive(:new).with(instance, :dot, :message).and_return(action)
|
157
|
+
expect(action).to receive(:process)
|
140
158
|
instance.delete :dot, :message
|
141
159
|
end
|
142
160
|
|
143
161
|
it 'parses wrap with' do
|
144
|
-
|
162
|
+
action = double
|
163
|
+
expect(Rewriter::WrapAction).to receive(:new).with(instance, with: 'module Foo', indent: nil).and_return(action)
|
164
|
+
expect(action).to receive(:process)
|
145
165
|
instance.wrap with: 'module Foo'
|
146
166
|
end
|
147
167
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: synvert-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.57.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|