synvert-core 0.58.3 → 0.59.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c6266f727e24e5cd93cc1db656d84dc177dc0fceb0ab435a14164216d6dec80
4
- data.tar.gz: 53e06769f132ac67ee113af6fecdb327184adc034b00aa58c0af8eab376ef45f
3
+ metadata.gz: '093f0af943a24314d55691d222fb181ab0526c4e1e8c557a38824609773a17a4'
4
+ data.tar.gz: 96b272edcb8ed63b346bfc30c76210aee11e7cab618045b0fed27107edbe5bfd
5
5
  SHA512:
6
- metadata.gz: fb0db8ed1e6636145f25ec8ca3cc3653ce415e7381c8e5d2126e7da7174782c57a2a4d60a3693794ae93ac02bd178b40f55971ffbf721e0d86b7c73fd17b930a
7
- data.tar.gz: ee6222b098f64dcc809d0d54ed9c1bad84f54816e05602b601c7ae8b0211f26fb41c05e46fe9728dc362536971021cc2a6e232a5e9f8ac34eff3f6ef55e94cb3
6
+ metadata.gz: 6cd336f74ddcbd5342811670824cf0f821c693a3b331f82dfb1f756f7deed41ae2f6d32cbba208a138f96a240476182c674adb2a5ab4940acda70e7737024f09
7
+ data.tar.gz: 6e9f09306f30fbde3dc552df33d09e2bc1e0651dcda418640cbcdc648704e427e670b4d910ffdd7039f48262c7d4cd62197e18dd111f512d1591c31643746580
data/CHANGELOG.md CHANGED
@@ -1,14 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
- ## 0.58.3 (2021-11-16)
3
+ ## 0.59.0 (2021-11-17)
4
4
 
5
+ * Use option `stop_when_match` instead of `recursive`
6
+ * Add file pattern constants
7
+ * Instance supports array of file patterns
5
8
  * Return block value by `next`
6
9
  * Add `Node#filename` method
7
10
 
8
- ## 0.58.2 (2021-10-23)
9
-
10
- * Do not break the whole `recursive_children`
11
-
12
11
  ## 0.58.0 (2021-10-23)
13
12
 
14
13
  * Support `left_value` and `right_value` for `and` and `or` node
@@ -72,13 +72,13 @@ module Synvert::Core
72
72
  # Initialize an instance.
73
73
  #
74
74
  # @param rewriter [Synvert::Core::Rewriter]
75
- # @param file_pattern [String] pattern to find files, e.g. spec/**/*_spec.rb
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, file_pattern, &block)
78
+ def initialize(rewriter, file_patterns, &block)
79
79
  @rewriter = rewriter
80
80
  @actions = []
81
- @file_pattern = file_pattern
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
- file_pattern = File.join(Configuration.path, @file_pattern)
91
- Dir.glob(file_pattern).each do |file_path|
92
- next if Configuration.skip_files.include? file_path
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
- 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
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
- 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?
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,10 +162,10 @@ 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.
164
- # @param options [Hash] optional, set if recursive or not.
165
+ # @param options [Hash] optional, set if stop_when_match or not.
165
166
  # @param block [Block] block code to continue operating on the matching nodes.
166
167
  def within_node(rules, options = nil, &block)
167
- options ||= { recursive: true }
168
+ options ||= { stop_when_match: false }
168
169
  Rewriter::WithinScope.new(self, rules, options, &block).process
169
170
  end
170
171
 
@@ -26,10 +26,10 @@ module Synvert::Core
26
26
  matching_nodes =
27
27
  if @options[:direct]
28
28
  find_direct_matching_nodes(current_node)
29
- elsif @options[:recursive]
30
- find_recursive_matching_nodes(current_node)
31
- else
29
+ elsif @options[:stop_when_match]
32
30
  find_matching_nodes(current_node)
31
+ else
32
+ find_recursive_matching_nodes(current_node)
33
33
  end
34
34
  @instance.process_with_node current_node do
35
35
  matching_nodes.each do |matching_node|
@@ -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 file_pattern [String] pattern to find files, e.g. spec/**/*_spec.rb
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(file_pattern, &block)
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, file_pattern, &block).process
240
+ Rewriter::Instance.new(self, Array(file_patterns), &block).process
241
241
  end
242
242
  end
243
243
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.58.3'
5
+ VERSION = '0.59.0'
6
6
  end
7
7
  end
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
@@ -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' }, { recursive: true }, &block)
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,7 +25,7 @@ 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' }, { recursive: true }, &block)
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)
@@ -35,10 +35,10 @@ module Synvert::Core
35
35
  scope = double
36
36
  block = proc {}
37
37
  expect(Rewriter::WithinScope).to receive(:new)
38
- .with(instance, { type: 'send', message: 'create' }, { recursive: false }, &block)
38
+ .with(instance, { type: 'send', message: 'create' }, { stop_when_match: true }, &block)
39
39
  .and_return(scope)
40
40
  expect(scope).to receive(:process)
41
- instance.within_node({ type: 'send', message: 'create' }, { recursive: false }, &block)
41
+ instance.within_node({ type: 'send', message: 'create' }, { stop_when_match: true }, &block)
42
42
  end
43
43
 
44
44
  it 'parses within_direct_node' do
@@ -185,7 +185,7 @@ module Synvert::Core
185
185
 
186
186
  it 'writes new code to file' do
187
187
  instance =
188
- Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
188
+ Rewriter::Instance.new rewriter, ['spec/**/*_spec.rb'] do
189
189
  with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
190
190
  replace_with 'create {{arguments}}'
191
191
  end
@@ -212,7 +212,7 @@ module Synvert::Core
212
212
 
213
213
  it 'does not write if file content is not changed' do
214
214
  instance =
215
- Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
215
+ Rewriter::Instance.new rewriter, ['spec/spec_helper.rb'] do
216
216
  with_node type: 'block', caller: { receiver: 'RSpec', message: 'configure' } do
217
217
  unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
218
218
  insert '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
@@ -237,7 +237,7 @@ module Synvert::Core
237
237
 
238
238
  it 'does not read file if already read' do
239
239
  instance =
240
- Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
240
+ Rewriter::Instance.new rewriter, ['spec/spec_helper.rb'] do
241
241
  with_node type: 'block', caller: { receiver: 'RSpec', message: 'configure' } do
242
242
  unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
243
243
  insert '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
@@ -263,7 +263,7 @@ module Synvert::Core
263
263
 
264
264
  it 'updates file_source and file_ast when writing a file' do
265
265
  instance =
266
- Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
266
+ Rewriter::Instance.new rewriter, ['spec/**/*_spec.rb'] do
267
267
  with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
268
268
  replace_with 'create {{arguments}}'
269
269
  end
@@ -299,7 +299,7 @@ module Synvert::Core
299
299
  action1 = double(begin_pos: 10, end_pos: 20)
300
300
  action2 = double(begin_pos: 30, end_pos: 40)
301
301
  action3 = double(begin_pos: 50, end_pos: 60)
302
- instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
302
+ instance = Rewriter::Instance.new rewriter, ['spec/spec_helper.rb']
303
303
  instance.instance_variable_set :@actions, [action1, action2, action3]
304
304
  conflict_actions = instance.send(:get_conflict_actions)
305
305
  expect(conflict_actions).to eq []
@@ -310,7 +310,7 @@ module Synvert::Core
310
310
  action1 = double(begin_pos: 30, end_pos: 40)
311
311
  action2 = double(begin_pos: 50, end_pos: 60)
312
312
  action3 = double(begin_pos: 10, end_pos: 20)
313
- instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
313
+ instance = Rewriter::Instance.new rewriter, ['spec/spec_helper.rb']
314
314
  instance.instance_variable_set :@actions, [action1, action2, action3]
315
315
  conflict_actions = instance.send(:get_conflict_actions)
316
316
  expect(conflict_actions).to eq [action2, action1]
@@ -54,7 +54,7 @@ module Synvert::Core
54
54
 
55
55
  it 'matches multiple block nodes' do
56
56
  block_nodes = []
57
- scope = Rewriter::WithinScope.new(instance, { type: 'block' }, { recursive: true }) do
57
+ scope = Rewriter::WithinScope.new(instance, { type: 'block' }, { stop_when_match: false }) do
58
58
  block_nodes << node
59
59
  end
60
60
  scope.process
@@ -63,7 +63,7 @@ module Synvert::Core
63
63
 
64
64
  it 'matches only one block node if no recursive' do
65
65
  block_nodes = []
66
- scope = Rewriter::WithinScope.new(instance, { type: 'block' } , { recursive: false }) do
66
+ scope = Rewriter::WithinScope.new(instance, { type: 'block' } , { stop_when_match: true }) do
67
67
  block_nodes << node
68
68
  end
69
69
  scope.process
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.58.3
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-16 00:00:00.000000000 Z
11
+ date: 2021-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport