synvert-core 0.57.1 → 0.59.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 +10 -1
- data/lib/synvert/core/node_ext.rb +9 -2
- data/lib/synvert/core/rewriter/action.rb +6 -6
- data/lib/synvert/core/rewriter/instance.rb +41 -38
- data/lib/synvert/core/rewriter/ruby_version.rb +1 -1
- data/lib/synvert/core/rewriter/scope/within_scope.rb +66 -22
- data/lib/synvert/core/rewriter.rb +3 -3
- data/lib/synvert/core/version.rb +1 -1
- data/lib/synvert/core.rb +31 -0
- data/spec/synvert/core/node_ext_spec.rb +8 -0
- data/spec/synvert/core/rewriter/instance_spec.rb +21 -11
- data/spec/synvert/core/rewriter/scope/within_scope_spec.rb +27 -0
- 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: '093f0af943a24314d55691d222fb181ab0526c4e1e8c557a38824609773a17a4'
|
4
|
+
data.tar.gz: 96b272edcb8ed63b346bfc30c76210aee11e7cab618045b0fed27107edbe5bfd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cd336f74ddcbd5342811670824cf0f821c693a3b331f82dfb1f756f7deed41ae2f6d32cbba208a138f96a240476182c674adb2a5ab4940acda70e7737024f09
|
7
|
+
data.tar.gz: 6e9f09306f30fbde3dc552df33d09e2bc1e0651dcda418640cbcdc648704e427e670b4d910ffdd7039f48262c7d4cd62197e18dd111f512d1591c31643746580
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,17 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.59.0 (2021-11-17)
|
4
|
+
|
5
|
+
* Use option `stop_when_match` instead of `recursive`
|
6
|
+
* Add file pattern constants
|
7
|
+
* Instance supports array of file patterns
|
8
|
+
* Return block value by `next`
|
9
|
+
* Add `Node#filename` method
|
10
|
+
|
11
|
+
## 0.58.0 (2021-10-23)
|
4
12
|
|
5
13
|
* Support `left_value` and `right_value` for `and` and `or` node
|
14
|
+
* Rewrite `within_node` and `within_direct_node`, `WithinScope` accepts `recursive` and `direct` options
|
6
15
|
|
7
16
|
## 0.57.0 (2021-10-02)
|
8
17
|
|
@@ -315,6 +315,13 @@ module Parser::AST
|
|
315
315
|
].join("\n")
|
316
316
|
end
|
317
317
|
|
318
|
+
# Get the file name of the current node.
|
319
|
+
#
|
320
|
+
# @return [String] file name.
|
321
|
+
def filename
|
322
|
+
loc.expression&.source_buffer.name
|
323
|
+
end
|
324
|
+
|
318
325
|
# Get the source code of current node.
|
319
326
|
#
|
320
327
|
# @return [String] source code.
|
@@ -432,8 +439,8 @@ module Parser::AST
|
|
432
439
|
def recursive_children(&block)
|
433
440
|
children.each do |child|
|
434
441
|
if child.is_a?(Parser::AST::Node)
|
435
|
-
yield child
|
436
|
-
child.recursive_children(&block)
|
442
|
+
stop = yield child
|
443
|
+
child.recursive_children(&block) unless stop == :stop
|
437
444
|
end
|
438
445
|
end
|
439
446
|
end
|
@@ -55,7 +55,7 @@ module Synvert::Core
|
|
55
55
|
|
56
56
|
def squeeze_spaces
|
57
57
|
if file_source[@begin_pos - 1] == ' ' && file_source[@end_pos] == ' '
|
58
|
-
@begin_pos
|
58
|
+
@begin_pos -= 1
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
@@ -67,19 +67,19 @@ module Synvert::Core
|
|
67
67
|
after_line_is_blank = lines[end_line] == ''
|
68
68
|
|
69
69
|
if lines.length > 1 && before_line_is_blank && after_line_is_blank
|
70
|
-
@end_pos
|
70
|
+
@end_pos += "\n".length
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
74
|
def remove_comma
|
75
75
|
if ',' == file_source[@begin_pos - 1]
|
76
|
-
@begin_pos
|
76
|
+
@begin_pos -= 1
|
77
77
|
elsif ', ' == file_source[@begin_pos - 2, 2]
|
78
|
-
@begin_pos
|
78
|
+
@begin_pos -= 2
|
79
79
|
elsif ', ' == file_source[@end_pos, 2]
|
80
|
-
@end_pos
|
80
|
+
@end_pos += 2
|
81
81
|
elsif ',' == file_source[@end_pos]
|
82
|
-
@end_pos
|
82
|
+
@end_pos += 1
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
@@ -72,13 +72,13 @@ module Synvert::Core
|
|
72
72
|
# Initialize an instance.
|
73
73
|
#
|
74
74
|
# @param rewriter [Synvert::Core::Rewriter]
|
75
|
-
# @param
|
75
|
+
# @param file_patterns [Array<String>] pattern list to find files, e.g. ['spec/**/*_spec.rb']
|
76
76
|
# @param block [Block] block code to find nodes, match conditions and rewrite code.
|
77
77
|
# @return [Synvert::Core::Rewriter::Instance]
|
78
|
-
def initialize(rewriter,
|
78
|
+
def initialize(rewriter, file_patterns, &block)
|
79
79
|
@rewriter = rewriter
|
80
80
|
@actions = []
|
81
|
-
@
|
81
|
+
@file_patterns = file_patterns
|
82
82
|
@block = block
|
83
83
|
rewriter.helpers.each { |helper| singleton_class.send(:define_method, helper[:name], &helper[:block]) }
|
84
84
|
end
|
@@ -87,41 +87,42 @@ module Synvert::Core
|
|
87
87
|
# It finds all files, for each file, it executes the block code, gets all rewrite actions,
|
88
88
|
# and rewrite source code back to original file.
|
89
89
|
def process
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
begin
|
95
|
-
puts file_path if Configuration.show_run_process
|
96
|
-
conflict_actions = []
|
97
|
-
source = +self.class.file_source(file_path)
|
98
|
-
ast = self.class.file_ast(file_path)
|
99
|
-
|
100
|
-
@current_file = file_path
|
101
|
-
|
102
|
-
process_with_node ast do
|
103
|
-
begin
|
104
|
-
instance_eval(&@block)
|
105
|
-
rescue NoMethodError
|
106
|
-
puts @current_node.debug_info
|
107
|
-
raise
|
108
|
-
end
|
109
|
-
end
|
90
|
+
@file_patterns.each do |file_pattern|
|
91
|
+
Dir.glob(File.join(Configuration.path, file_pattern)).each do |file_path|
|
92
|
+
next if Configuration.skip_files.include? file_path
|
110
93
|
|
111
|
-
|
112
|
-
|
113
|
-
conflict_actions =
|
114
|
-
|
115
|
-
|
94
|
+
begin
|
95
|
+
puts file_path if Configuration.show_run_process
|
96
|
+
conflict_actions = []
|
97
|
+
source = +self.class.file_source(file_path)
|
98
|
+
ast = self.class.file_ast(file_path)
|
99
|
+
|
100
|
+
@current_file = file_path
|
101
|
+
|
102
|
+
process_with_node ast do
|
103
|
+
begin
|
104
|
+
instance_eval(&@block)
|
105
|
+
rescue NoMethodError
|
106
|
+
puts @current_node.debug_info
|
107
|
+
raise
|
108
|
+
end
|
116
109
|
end
|
117
|
-
@actions = []
|
118
110
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
111
|
+
if @actions.length > 0
|
112
|
+
@actions.sort_by! { |action| [action.begin_pos, action.end_pos] }
|
113
|
+
conflict_actions = get_conflict_actions
|
114
|
+
@actions.reverse_each do |action|
|
115
|
+
source[action.begin_pos...action.end_pos] = action.rewritten_code
|
116
|
+
end
|
117
|
+
@actions = []
|
118
|
+
|
119
|
+
update_file(file_path, source)
|
120
|
+
end
|
121
|
+
rescue Parser::SyntaxError
|
122
|
+
puts "[Warn] file #{file_path} was not parsed correctly."
|
123
|
+
# do nothing, iterate next file
|
124
|
+
end while !conflict_actions.empty?
|
125
|
+
end
|
125
126
|
end
|
126
127
|
end
|
127
128
|
|
@@ -161,9 +162,11 @@ module Synvert::Core
|
|
161
162
|
# then continue operating on each matching ast node.
|
162
163
|
#
|
163
164
|
# @param rules [Hash] rules to find mathing ast nodes.
|
165
|
+
# @param options [Hash] optional, set if stop_when_match or not.
|
164
166
|
# @param block [Block] block code to continue operating on the matching nodes.
|
165
|
-
def within_node(rules, &block)
|
166
|
-
|
167
|
+
def within_node(rules, options = nil, &block)
|
168
|
+
options ||= { stop_when_match: false }
|
169
|
+
Rewriter::WithinScope.new(self, rules, options, &block).process
|
167
170
|
end
|
168
171
|
|
169
172
|
alias with_node within_node
|
@@ -174,7 +177,7 @@ module Synvert::Core
|
|
174
177
|
# @param rules [Hash] rules to find mathing ast nodes.
|
175
178
|
# @param block [Block] block code to continue operating on the matching nodes.
|
176
179
|
def within_direct_node(rules, &block)
|
177
|
-
Rewriter::WithinScope.new(self, rules, {
|
180
|
+
Rewriter::WithinScope.new(self, rules, { direct: true }, &block).process
|
178
181
|
end
|
179
182
|
|
180
183
|
alias with_direct_node within_direct_node
|
@@ -21,7 +21,7 @@ module Synvert::Core
|
|
21
21
|
elsif File.exist?(File.join(Configuration.path, '.rvmrc'))
|
22
22
|
versionFile = '.rvmrc'
|
23
23
|
end
|
24
|
-
return true
|
24
|
+
return true unless versionFile
|
25
25
|
|
26
26
|
version = File.read(File.join(Configuration.path, versionFile))
|
27
27
|
Gem::Version.new(version) >= Gem::Version.new(@version)
|
@@ -9,7 +9,7 @@ module Synvert::Core
|
|
9
9
|
# @param rules [Hash]
|
10
10
|
# @param options [Hash]
|
11
11
|
# @param block [Block]
|
12
|
-
def initialize(instance, rules, options = {
|
12
|
+
def initialize(instance, rules, options = {}, &block)
|
13
13
|
@instance = instance
|
14
14
|
@rules = rules
|
15
15
|
@options = options
|
@@ -23,7 +23,14 @@ module Synvert::Core
|
|
23
23
|
current_node = @instance.current_node
|
24
24
|
return unless current_node
|
25
25
|
|
26
|
-
matching_nodes =
|
26
|
+
matching_nodes =
|
27
|
+
if @options[:direct]
|
28
|
+
find_direct_matching_nodes(current_node)
|
29
|
+
elsif @options[:stop_when_match]
|
30
|
+
find_matching_nodes(current_node)
|
31
|
+
else
|
32
|
+
find_recursive_matching_nodes(current_node)
|
33
|
+
end
|
27
34
|
@instance.process_with_node current_node do
|
28
35
|
matching_nodes.each do |matching_node|
|
29
36
|
@instance.process_with_node matching_node do
|
@@ -35,36 +42,73 @@ module Synvert::Core
|
|
35
42
|
|
36
43
|
private
|
37
44
|
|
38
|
-
|
45
|
+
# Find the matching nodes only in current or direct children.
|
46
|
+
def find_direct_matching_nodes(current_node)
|
39
47
|
matching_nodes = []
|
40
|
-
if
|
41
|
-
if current_node.is_a?(Parser::AST::Node)
|
42
|
-
matching_nodes << current_node if current_node.match? @rules
|
43
|
-
current_node.recursive_children do |child_node|
|
44
|
-
matching_nodes << child_node if child_node.match? @rules
|
45
|
-
end
|
46
|
-
else
|
47
|
-
current_node.each do |node|
|
48
|
-
matching_nodes << node if node.match? @rules
|
49
|
-
node.recursive_children do |child_node|
|
50
|
-
matching_nodes << child_node if child_node.match? @rules
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
elsif current_node.is_a?(Parser::AST::Node)
|
48
|
+
if current_node.is_a?(Parser::AST::Node)
|
55
49
|
if current_node.type == :begin
|
56
50
|
current_node.children.each do |child_node|
|
57
|
-
matching_nodes << child_node if child_node.match?
|
51
|
+
matching_nodes << child_node if child_node.match?(@rules)
|
58
52
|
end
|
59
|
-
elsif current_node.match?
|
53
|
+
elsif current_node.match?(@rules)
|
60
54
|
matching_nodes << current_node
|
61
55
|
end
|
62
56
|
else
|
63
57
|
current_node.each do |child_node|
|
64
|
-
matching_nodes << child_node if child_node.match?
|
58
|
+
matching_nodes << child_node if child_node.match?(@rules)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
matching_nodes
|
62
|
+
end
|
63
|
+
|
64
|
+
# Find matching nodes in all recursive children.
|
65
|
+
def find_recursive_matching_nodes(current_node)
|
66
|
+
matching_nodes = []
|
67
|
+
if current_node.is_a?(Parser::AST::Node)
|
68
|
+
matching_nodes << current_node if current_node.match?(@rules)
|
69
|
+
current_node.recursive_children do |child_node|
|
70
|
+
matching_nodes << child_node if child_node.match?(@rules)
|
71
|
+
end
|
72
|
+
else
|
73
|
+
current_node.each do |node|
|
74
|
+
matching_nodes << node if node.match?(@rules)
|
75
|
+
node.recursive_children do |child_node|
|
76
|
+
matching_nodes << child_node if child_node.match?(@rules)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
matching_nodes
|
81
|
+
end
|
82
|
+
|
83
|
+
# Find matching nodes in recursive children but do not continue on matching nodes.
|
84
|
+
def find_matching_nodes(current_node)
|
85
|
+
matching_nodes = []
|
86
|
+
if current_node.is_a?(Parser::AST::Node)
|
87
|
+
if current_node.match?(@rules)
|
88
|
+
matching_nodes << current_node
|
89
|
+
return matching_nodes
|
90
|
+
end
|
91
|
+
current_node.recursive_children do |child_node|
|
92
|
+
if child_node.match?(@rules)
|
93
|
+
matching_nodes << child_node
|
94
|
+
next :stop
|
95
|
+
end
|
96
|
+
end
|
97
|
+
else
|
98
|
+
current_node.each do |node|
|
99
|
+
if node.match?(@rules)
|
100
|
+
matching_nodes << node
|
101
|
+
next
|
102
|
+
end
|
103
|
+
node.recursive_children do |child_node|
|
104
|
+
if child_node.match?(@rules)
|
105
|
+
matching_nodes << child_node
|
106
|
+
next :stop
|
107
|
+
end
|
108
|
+
end
|
65
109
|
end
|
66
110
|
end
|
67
111
|
matching_nodes
|
68
112
|
end
|
69
113
|
end
|
70
|
-
end
|
114
|
+
end
|
@@ -231,13 +231,13 @@ module Synvert::Core
|
|
231
231
|
# Parse within_files dsl, it finds specified files.
|
232
232
|
# It creates a [Synvert::Core::Rewriter::Instance] to rewrite code.
|
233
233
|
#
|
234
|
-
# @param
|
234
|
+
# @param file_patterns [String|Array<String>] string pattern or list of string pattern to find files, e.g. ['spec/**/*_spec.rb']
|
235
235
|
# @param block [Block] the block to rewrite code in the matching files.
|
236
|
-
def within_files(
|
236
|
+
def within_files(file_patterns, &block)
|
237
237
|
return if @sandbox
|
238
238
|
|
239
239
|
if (!@ruby_version || @ruby_version.match?) && (!@gem_spec || @gem_spec.match?)
|
240
|
-
Rewriter::Instance.new(self,
|
240
|
+
Rewriter::Instance.new(self, Array(file_patterns), &block).process
|
241
241
|
end
|
242
242
|
end
|
243
243
|
|
data/lib/synvert/core/version.rb
CHANGED
data/lib/synvert/core.rb
CHANGED
@@ -24,4 +24,35 @@ end
|
|
24
24
|
|
25
25
|
module Synvert
|
26
26
|
Rewriter = Core::Rewriter
|
27
|
+
|
28
|
+
ALL_RUBY_FILES = %w[**/*.rb]
|
29
|
+
ALL_RAKE_FILES = %w[**/*.rake]
|
30
|
+
|
31
|
+
RAILS_APP_FILES = %w[app/**/*.rb engines/*/app/**/*.rb]
|
32
|
+
RAILS_CONTROLLER_FILES = %w[app/controllers/**/*.rb engines/*/app/controllers/**/*.rb]
|
33
|
+
RAILS_JOB_FILES = %w[app/jobs/**/*.rb engines/*/app/jobs/**/*.rb]
|
34
|
+
RAILS_OBSERVER_FILES = %w[app/observers/**/*.rb engines/*/app/observers/**/*.rb]
|
35
|
+
RAILS_HELPER_FILES = %w[app/helpers/**/*.rb]
|
36
|
+
RAILS_LIB_FILES = %w[lib/**/*.rb engines/*/lib/**/*.rb]
|
37
|
+
RAILS_MAILER_FILES = %w[app/mailers/**/*.rb engines/*/app/mailers/**/*.rb]
|
38
|
+
RAILS_MIGRATION_FILES = %w[db/migrate/**/*.rb engines/*/db/migrate/**/*.rb]
|
39
|
+
RAILS_MODEL_FILES = %w[app/models/**/*.rb engines/*/app/models/**/*.rb]
|
40
|
+
RAILS_ROUTE_FILES = %w[config/routes.rb config/routes/**/*.rb engines/*/config/routes.rb engines/*/config/routes/**/*.rb]
|
41
|
+
RAILS_VIEW_FILES = %w[app/views/**/*.html.{erb,haml,slim}]
|
42
|
+
|
43
|
+
RAILS_CONTROLLER_TEST_FILES = %w[
|
44
|
+
test/functional/**/*.rb test/controllers/**/*.rb engines/*/test/functional/**/*.rb engines/*/test/controllers/**/*.rb
|
45
|
+
spec/functional/**/*.rb spec/controllers/**/*.rb engines/*/spec/functional/**/*.rb engines/*/spec/controllers/**/*.rb
|
46
|
+
]
|
47
|
+
RAILS_INTEGRATION_TEST_FILES = %w[test/integration/**/*.rb spec/integration/**/*.rb]
|
48
|
+
RAILS_MODEL_TEST_FILES = %w[
|
49
|
+
test/unit/**/*.rb engines/*/test/unit/**/*.rb test/models/**/*.rb engines/*/test/models/**/*.rb
|
50
|
+
spec/models/**/*.rb engines/*/spec/models/**/*.rb
|
51
|
+
]
|
52
|
+
|
53
|
+
RAILS_FACTORY_FILES = %w[test/factories/**/*.rb spec/factories/**/*.rb]
|
54
|
+
RAILS_RSPEC_FILES = %w[spec/**/*.rb engines/*/spec/**/*.rb]
|
55
|
+
RAILS_MINITEST_FILES = %w[test/**/*.rb engines/*/test/**/*.rb]
|
56
|
+
RAILS_CUCUMBER_FILES = %w[features/**/*.rb]
|
57
|
+
RAILS_TEST_FILES = RAILS_MINITEST_FILES + RAILS_RSPEC_FILES + RAILS_CUCUMBER_FILES
|
27
58
|
end
|
@@ -396,6 +396,14 @@ describe Parser::AST::Node do
|
|
396
396
|
end
|
397
397
|
end
|
398
398
|
|
399
|
+
describe '#filename' do
|
400
|
+
it 'gets file name' do
|
401
|
+
source = 'foobar'
|
402
|
+
node = parse(source)
|
403
|
+
expect(node.filename).to eq '(string)'
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
399
407
|
describe '#to_source' do
|
400
408
|
it 'gets for node' do
|
401
409
|
source = 'params[:user][:email]'
|
@@ -8,14 +8,14 @@ module Synvert::Core
|
|
8
8
|
|
9
9
|
let(:instance) {
|
10
10
|
rewriter = Rewriter.new('foo', 'bar')
|
11
|
-
Rewriter::Instance.new(rewriter, 'file pattern')
|
11
|
+
Rewriter::Instance.new(rewriter, ['file pattern'])
|
12
12
|
}
|
13
13
|
|
14
14
|
it 'parses within_node' do
|
15
15
|
scope = double
|
16
16
|
block = proc {}
|
17
17
|
expect(Rewriter::WithinScope).to receive(:new)
|
18
|
-
.with(instance, { type: 'send', message: 'create' }, {
|
18
|
+
.with(instance, { type: 'send', message: 'create' }, { stop_when_match: false }, &block)
|
19
19
|
.and_return(scope)
|
20
20
|
expect(scope).to receive(:process)
|
21
21
|
instance.within_node(type: 'send', message: 'create', &block)
|
@@ -25,17 +25,27 @@ module Synvert::Core
|
|
25
25
|
scope = double
|
26
26
|
block = proc {}
|
27
27
|
expect(Rewriter::WithinScope).to receive(:new)
|
28
|
-
.with(instance, { type: 'send', message: 'create' }, {
|
28
|
+
.with(instance, { type: 'send', message: 'create' }, { stop_when_match: false }, &block)
|
29
29
|
.and_return(scope)
|
30
30
|
expect(scope).to receive(:process)
|
31
31
|
instance.with_node(type: 'send', message: 'create', &block)
|
32
32
|
end
|
33
33
|
|
34
|
+
it 'parses within_node with recursive false' do
|
35
|
+
scope = double
|
36
|
+
block = proc {}
|
37
|
+
expect(Rewriter::WithinScope).to receive(:new)
|
38
|
+
.with(instance, { type: 'send', message: 'create' }, { stop_when_match: true }, &block)
|
39
|
+
.and_return(scope)
|
40
|
+
expect(scope).to receive(:process)
|
41
|
+
instance.within_node({ type: 'send', message: 'create' }, { stop_when_match: true }, &block)
|
42
|
+
end
|
43
|
+
|
34
44
|
it 'parses within_direct_node' do
|
35
45
|
scope = double
|
36
46
|
block = proc {}
|
37
47
|
expect(Rewriter::WithinScope).to receive(:new)
|
38
|
-
.with(instance, { type: 'send', message: 'create' }, {
|
48
|
+
.with(instance, { type: 'send', message: 'create' }, { direct: true }, &block)
|
39
49
|
.and_return(scope)
|
40
50
|
expect(scope).to receive(:process)
|
41
51
|
instance.within_direct_node(type: 'send', message: 'create', &block)
|
@@ -45,7 +55,7 @@ module Synvert::Core
|
|
45
55
|
scope = double
|
46
56
|
block = proc {}
|
47
57
|
expect(Rewriter::WithinScope).to receive(:new)
|
48
|
-
.with(instance, { type: 'send', message: 'create' }, {
|
58
|
+
.with(instance, { type: 'send', message: 'create' }, { direct: true }, &block)
|
49
59
|
.and_return(scope)
|
50
60
|
expect(scope).to receive(:process)
|
51
61
|
instance.with_direct_node(type: 'send', message: 'create', &block)
|
@@ -175,7 +185,7 @@ module Synvert::Core
|
|
175
185
|
|
176
186
|
it 'writes new code to file' do
|
177
187
|
instance =
|
178
|
-
Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
|
188
|
+
Rewriter::Instance.new rewriter, ['spec/**/*_spec.rb'] do
|
179
189
|
with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
|
180
190
|
replace_with 'create {{arguments}}'
|
181
191
|
end
|
@@ -202,7 +212,7 @@ module Synvert::Core
|
|
202
212
|
|
203
213
|
it 'does not write if file content is not changed' do
|
204
214
|
instance =
|
205
|
-
Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
|
215
|
+
Rewriter::Instance.new rewriter, ['spec/spec_helper.rb'] do
|
206
216
|
with_node type: 'block', caller: { receiver: 'RSpec', message: 'configure' } do
|
207
217
|
unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
|
208
218
|
insert '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
@@ -227,7 +237,7 @@ module Synvert::Core
|
|
227
237
|
|
228
238
|
it 'does not read file if already read' do
|
229
239
|
instance =
|
230
|
-
Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
|
240
|
+
Rewriter::Instance.new rewriter, ['spec/spec_helper.rb'] do
|
231
241
|
with_node type: 'block', caller: { receiver: 'RSpec', message: 'configure' } do
|
232
242
|
unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
|
233
243
|
insert '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
@@ -253,7 +263,7 @@ module Synvert::Core
|
|
253
263
|
|
254
264
|
it 'updates file_source and file_ast when writing a file' do
|
255
265
|
instance =
|
256
|
-
Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
|
266
|
+
Rewriter::Instance.new rewriter, ['spec/**/*_spec.rb'] do
|
257
267
|
with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
|
258
268
|
replace_with 'create {{arguments}}'
|
259
269
|
end
|
@@ -289,7 +299,7 @@ module Synvert::Core
|
|
289
299
|
action1 = double(begin_pos: 10, end_pos: 20)
|
290
300
|
action2 = double(begin_pos: 30, end_pos: 40)
|
291
301
|
action3 = double(begin_pos: 50, end_pos: 60)
|
292
|
-
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
|
302
|
+
instance = Rewriter::Instance.new rewriter, ['spec/spec_helper.rb']
|
293
303
|
instance.instance_variable_set :@actions, [action1, action2, action3]
|
294
304
|
conflict_actions = instance.send(:get_conflict_actions)
|
295
305
|
expect(conflict_actions).to eq []
|
@@ -300,7 +310,7 @@ module Synvert::Core
|
|
300
310
|
action1 = double(begin_pos: 30, end_pos: 40)
|
301
311
|
action2 = double(begin_pos: 50, end_pos: 60)
|
302
312
|
action3 = double(begin_pos: 10, end_pos: 20)
|
303
|
-
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
|
313
|
+
instance = Rewriter::Instance.new rewriter, ['spec/spec_helper.rb']
|
304
314
|
instance.instance_variable_set :@actions, [action1, action2, action3]
|
305
315
|
conflict_actions = instance.send(:get_conflict_actions)
|
306
316
|
expect(conflict_actions).to eq [action2, action1]
|
@@ -51,6 +51,33 @@ module Synvert::Core
|
|
51
51
|
expect(type_in_scope).to eq :send
|
52
52
|
expect(instance.current_node.type).to eq :block
|
53
53
|
end
|
54
|
+
|
55
|
+
it 'matches multiple block nodes' do
|
56
|
+
block_nodes = []
|
57
|
+
scope = Rewriter::WithinScope.new(instance, { type: 'block' }, { stop_when_match: false }) do
|
58
|
+
block_nodes << node
|
59
|
+
end
|
60
|
+
scope.process
|
61
|
+
expect(block_nodes.size).to eq 2
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'matches only one block node if no recursive' do
|
65
|
+
block_nodes = []
|
66
|
+
scope = Rewriter::WithinScope.new(instance, { type: 'block' } , { stop_when_match: true }) do
|
67
|
+
block_nodes << node
|
68
|
+
end
|
69
|
+
scope.process
|
70
|
+
expect(block_nodes.size).to eq 1
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'matches only one direct node' do
|
74
|
+
block_nodes = []
|
75
|
+
scope = Rewriter::WithinScope.new(instance, { type: 'block' } , { direct: true }) do
|
76
|
+
block_nodes << node
|
77
|
+
end
|
78
|
+
scope.process
|
79
|
+
expect(block_nodes.size).to eq 1
|
80
|
+
end
|
54
81
|
end
|
55
82
|
end
|
56
83
|
end
|
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.59.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-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|