synvert-core 1.13.1 → 1.14.1

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: 27baedce6ee22b8bac638cfab9caceb27f8fef82dd4af238b3b239ff64d9d19a
4
- data.tar.gz: 8e0ef98086e9a0e74d87ea62899628bb0271fcf1a011b380bc51e224061606e4
3
+ metadata.gz: bf1187899f9eb743b73e0295ebac08014131dae4acd6e4d17475f9eb3cc71657
4
+ data.tar.gz: fcafcb32987582fc9f52e2735ee3001fc9bc7110aeea9da3b7f91cd307ddb064
5
5
  SHA512:
6
- metadata.gz: 0aa91008955762f7a8f6362d0e23372651430e85eb6804b59c2cf9ea1577deda113c6dddf1ae92d44623bbd1bb3328f8414c0c8c8a910df9d575c294301e3c9a
7
- data.tar.gz: 3ec198c9d26fd0e8166dbaf102f606e89f52751f1152a50aa6283d525e848970765cf04d67bcb5f7ca6008c029c3c7f876a8a087d9d5ffbce688b0b22f4b4be6
6
+ metadata.gz: 623cc353b1d7e4963de14dfee389ad6044711c8f9284e55e24c83e7d652dc65c236cf32c1acc2cb40a615b549cd418fcf5f8b290bb3f87bd6f90bbc9b2341161
7
+ data.tar.gz: 98ba68a0638b04d2d21405c194117c7cc98ca8159d362cf847e6f39fe78a865939f179798db6f76305571dd01fd53253ee8ffd29d4f822e9cb56d239ad86408e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.14.1 (2022-10-26)
4
+
5
+ * Abstract AnyValue to NodeQuery
6
+
7
+ ## 1.14.0 (2022-10-25)
8
+
9
+ * `insert_after` and `insert_before` accepts `to` option
10
+ * Add `configure` dsl to configure the strategy
11
+
3
12
  ## 1.13.1 (2022-10-17)
4
13
 
5
14
  * Do not send hash to keyword arguments
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.13.1)
4
+ synvert-core (1.14.1)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
  node_mutation
@@ -50,10 +50,10 @@ GEM
50
50
  method_source (1.0.0)
51
51
  minitest (5.16.3)
52
52
  nenv (0.3.0)
53
- node_mutation (1.5.0)
53
+ node_mutation (1.7.1)
54
54
  activesupport (< 7.0.0)
55
55
  erubis
56
- node_query (1.8.1)
56
+ node_query (1.10.0)
57
57
  activesupport (< 7.0.0)
58
58
  notiffany (0.1.3)
59
59
  nenv (~> 0.1)
@@ -61,7 +61,7 @@ GEM
61
61
  parallel (1.22.1)
62
62
  parser (3.1.2.1)
63
63
  ast (~> 2.4.1)
64
- parser_node_ext (0.4.0)
64
+ parser_node_ext (0.4.1)
65
65
  parser
66
66
  pry (0.14.1)
67
67
  coderay (~> 1.1)
data/README.md CHANGED
@@ -59,6 +59,7 @@ Want to use the CLI, check out [synvert-ruby](https://github.com/xinminlabs/synv
59
59
 
60
60
  DSLs are as follows
61
61
 
62
+ * [configure](./Synvert/Core/Rewriter.html#configure-instance_method) - configure the rewriter
62
63
  * [description](./Synvert/Core/Rewriter.html#description-instance_method) - set description of the rewriter
63
64
  * [if_ruby](./Synvert/Core/Rewriter.html#if_ruby-instance_method) - check if ruby version is greater than or equal to the specified ruby version
64
65
  * [if_gem](./Synvert/Core/Rewriter.html#if_gem-instance_method) - compare version of specified gem
@@ -8,6 +8,7 @@ module Synvert::Core
8
8
  # One instance can contain one or many {Synvert::Core::Rewriter::Scope} and {Synvert::Rewriter::Condition}.
9
9
  class Rewriter::Instance
10
10
  include Rewriter::Helper
11
+
11
12
  # Initialize an Instance.
12
13
  #
13
14
  # @param rewriter [Synvert::Core::Rewriter]
@@ -18,6 +19,11 @@ module Synvert::Core
18
19
  @actions = []
19
20
  @file_patterns = file_patterns
20
21
  @block = block
22
+ strategy = NodeMutation::Strategy::KEEP_RUNNING
23
+ if rewriter.options[:strategy] == Strategy::ALLOW_INSERT_AT_SAME_POSITION
24
+ strategy |= NodeMutation::Strategy::ALLOW_INSERT_AT_SAME_POSITION
25
+ end
26
+ NodeMutation.configure({ strategy: strategy })
21
27
  rewriter.helpers.each { |helper| singleton_class.send(:define_method, helper[:name], &helper[:block]) }
22
28
  end
23
29
 
@@ -85,7 +91,7 @@ module Synvert::Core
85
91
  # DSL #
86
92
  #######
87
93
 
88
- # Parse +within_node+ dsl, it creates a {Synvert::Core::Rewriter::WithinScope} to recursively find matching ast nodes,
94
+ # It creates a {Synvert::Core::Rewriter::WithinScope} to recursively find matching ast nodes,
89
95
  # then continue operating on each matching ast node.
90
96
  # @example
91
97
  # # matches User.find_by_login('test')
@@ -109,7 +115,7 @@ module Synvert::Core
109
115
  alias with_node within_node
110
116
  alias find_node within_node
111
117
 
112
- # Parse +goto_node+ dsl, it creates a {Synvert::Core::Rewriter::GotoScope} to go to a child node,
118
+ # It creates a {Synvert::Core::Rewriter::GotoScope} to go to a child node,
113
119
  # then continue operating on the child node.
114
120
  # @example
115
121
  # # head status: 406
@@ -123,7 +129,7 @@ module Synvert::Core
123
129
  Rewriter::GotoScope.new(self, child_node_name, &block).process
124
130
  end
125
131
 
126
- # Parse +if_exist_node+ dsl, it creates a {Synvert::Core::Rewriter::IfExistCondition} to check
132
+ # It creates a {Synvert::Core::Rewriter::IfExistCondition} to check
127
133
  # if matching nodes exist in the child nodes, if so, then continue operating on each matching ast node.
128
134
  # @example
129
135
  # # Klass.any_instance.stub(:message)
@@ -137,7 +143,7 @@ module Synvert::Core
137
143
  Rewriter::IfExistCondition.new(self, nql_or_rules, &block).process
138
144
  end
139
145
 
140
- # Parse +unless_exist_node+ dsl, it creates a {Synvert::Core::Rewriter::UnlessExistCondition} to check
146
+ # It creates a {Synvert::Core::Rewriter::UnlessExistCondition} to check
141
147
  # if matching nodes doesn't exist in the child nodes, if so, then continue operating on each matching ast node.
142
148
  # @example
143
149
  # # obj.stub(:message)
@@ -151,7 +157,7 @@ module Synvert::Core
151
157
  Rewriter::UnlessExistCondition.new(self, nql_or_rules, &block).process
152
158
  end
153
159
 
154
- # Parse +if_only_exist_node+ dsl, it creates a {Synvert::Core::Rewriter::IfOnlyExistCondition} to check
160
+ # It creates a {Synvert::Core::Rewriter::IfOnlyExistCondition} to check
155
161
  # if current node has only one child node and the child node matches,
156
162
  # if so, then continue operating on each matching ast node.
157
163
  # @example
@@ -166,7 +172,7 @@ module Synvert::Core
166
172
  Rewriter::IfOnlyExistCondition.new(self, nql_or_rules, &block).process
167
173
  end
168
174
 
169
- # Parse +append+ dsl, it appends the code to the bottom of current node body.
175
+ # It appends the code to the bottom of current node body.
170
176
  # @example
171
177
  # # def teardown
172
178
  # # clean_something
@@ -184,7 +190,7 @@ module Synvert::Core
184
190
  @current_mutation.append(@current_node, code)
185
191
  end
186
192
 
187
- # Parse +prepend+ dsl, it prepends the code to the top of current node body.
193
+ # It prepends the code to the top of current node body.
188
194
  # @example
189
195
  # # def setup
190
196
  # # do_something
@@ -202,7 +208,7 @@ module Synvert::Core
202
208
  @current_mutation.prepend(@current_node, code)
203
209
  end
204
210
 
205
- # Parse +insert+ dsl, it inserts code.
211
+ # It inserts code.
206
212
  # @example
207
213
  # # open('http://test.com')
208
214
  # # =>
@@ -217,7 +223,7 @@ module Synvert::Core
217
223
  @current_mutation.insert(@current_node, code, at: at, to: to)
218
224
  end
219
225
 
220
- # Parse +insert_after+ dsl, it inserts the code next to the current node.
226
+ # It inserts the code next to the current node.
221
227
  # @example
222
228
  # # Synvert::Application.config.secret_token = "0447aa931d42918bfb934750bb78257088fb671186b5d1b6f9fddf126fc8a14d34f1d045cefab3900751c3da121a8dd929aec9bafe975f1cabb48232b4002e4e"
223
229
  # # =>
@@ -227,12 +233,12 @@ module Synvert::Core
227
233
  # insert_after "{{receiver}}.secret_key_base = \"#{SecureRandom.hex(64)}\""
228
234
  # end
229
235
  # @param code [String] code need to be inserted.
230
- def insert_after(code)
236
+ def insert_after(code, to: nil)
231
237
  column = ' ' * NodeMutation.adapter.get_start_loc(@current_node).column
232
- @current_mutation.insert(@current_node, "\n#{column}#{code}", at: 'end')
238
+ @current_mutation.insert(@current_node, "\n#{column}#{code}", at: 'end', to: to)
233
239
  end
234
240
 
235
- # Parse +insert_before+ dsl, it inserts the code previous to the current node.
241
+ # It inserts the code previous to the current node.
236
242
  # @example
237
243
  # # Synvert::Application.config.secret_token = "0447aa931d42918bfb934750bb78257088fb671186b5d1b6f9fddf126fc8a14d34f1d045cefab3900751c3da121a8dd929aec9bafe975f1cabb48232b4002e4e"
238
244
  # # =>
@@ -242,12 +248,12 @@ module Synvert::Core
242
248
  # insert_before "{{receiver}}.secret_key_base = \"#{SecureRandom.hex(64)}\""
243
249
  # end
244
250
  # @param code [String] code need to be inserted.
245
- def insert_before(code)
251
+ def insert_before(code, to: nil)
246
252
  column = ' ' * NodeMutation.adapter.get_start_loc(@current_node).column
247
- @current_mutation.insert(@current_node, "#{code}\n#{column}", at: 'beginning')
253
+ @current_mutation.insert(@current_node, "#{code}\n#{column}", at: 'beginning', to: to)
248
254
  end
249
255
 
250
- # Parse +replace_erb_stmt_with_expr+ dsl, it replaces erb stmt code to expr code.
256
+ # It replaces erb stmt code to expr code.
251
257
  # @example
252
258
  # # <% form_for post do |f| %>
253
259
  # # <% end %>
@@ -261,7 +267,7 @@ module Synvert::Core
261
267
  @current_mutation.actions << Rewriter::ReplaceErbStmtWithExprAction.new(@current_node).process
262
268
  end
263
269
 
264
- # Parse +replace_with+ dsl, it replaces the whole code of current node.
270
+ # It replaces the whole code of current node.
265
271
  # @example
266
272
  # # obj.stub(:foo => 1, :bar => 2)
267
273
  # # =>
@@ -274,7 +280,7 @@ module Synvert::Core
274
280
  @current_mutation.replace_with(@current_node, code)
275
281
  end
276
282
 
277
- # Parse +replace+ dsl, it replaces the code of specified child nodes.
283
+ # It replaces the code of specified child nodes.
278
284
  # @example
279
285
  # # assert(object.empty?)
280
286
  # # =>
@@ -289,7 +295,7 @@ module Synvert::Core
289
295
  @current_mutation.replace(@current_node, *selectors, with: with)
290
296
  end
291
297
 
292
- # Parse +remove+ dsl, it removes current node.
298
+ # It removes current node.
293
299
  # @example
294
300
  # with_node type: 'send', message: { in: %w[puts p] } do
295
301
  # remove
@@ -300,7 +306,7 @@ module Synvert::Core
300
306
  @current_mutation.remove(@current_node, **options)
301
307
  end
302
308
 
303
- # Parse +delete+ dsl, it deletes child nodes.
309
+ # It deletes child nodes.
304
310
  # @example
305
311
  # # FactoryBot.create(...)
306
312
  # # =>
@@ -315,7 +321,7 @@ module Synvert::Core
315
321
  @current_mutation.delete(@current_node, *selectors, **options)
316
322
  end
317
323
 
318
- # Parse +wrap+ dsl, it wraps current node with code.
324
+ # It wraps current node with code.
319
325
  # @example
320
326
  # # class Foobar
321
327
  # # end
@@ -332,12 +338,12 @@ module Synvert::Core
332
338
  @current_mutation.wrap(@current_node, with: with)
333
339
  end
334
340
 
335
- # Parse +noop dsl.
341
+ # No operation.
336
342
  def noop
337
343
  @current_mutation.noop(@current_node)
338
344
  end
339
345
 
340
- # Parse +warn+ dsl, it creates a {Synvert::Core::Rewriter::Warning} to save warning message.
346
+ # It creates a {Synvert::Core::Rewriter::Warning} to save warning message.
341
347
  # @example
342
348
  # within_files 'vendor/plugins' do
343
349
  # warn 'Rails::Plugin is deprecated and will be removed in Rails 4.0. Instead of adding plugins to vendor/plugins use gems or bundler with path or git dependencies.'
@@ -350,9 +356,9 @@ module Synvert::Core
350
356
  # Match any value but nil.
351
357
  # @example
352
358
  # type: 'hash', nothing_value: 'true', status_value: any_value
353
- # @return [Synvert::Core::Rewriter::AnyValue]
359
+ # @return [NodeQuery::AnyValue]
354
360
  def any_value
355
- Rewriter::AnyValue.new
361
+ NodeQuery::AnyValue.new
356
362
  end
357
363
 
358
364
  private
@@ -30,8 +30,6 @@ module Synvert::Core
30
30
  autoload :RubyVersion, 'synvert/core/rewriter/ruby_version'
31
31
  autoload :GemSpec, 'synvert/core/rewriter/gem_spec'
32
32
 
33
- autoload :AnyValue, 'synvert/core/rewriter/any_value'
34
-
35
33
  class << self
36
34
  # Register a rewriter with its group and name.
37
35
  #
@@ -163,8 +161,18 @@ module Synvert::Core
163
161
  # DSL #
164
162
  #######
165
163
 
166
- # Parse +description+ dsl, it sets description of the rewrite.
167
- # Or get description.
164
+ # Configure the rewriter
165
+ # @example
166
+ # configure({ strategy: 'allow_insert_at_same_position' })
167
+ # @param options [Hash]
168
+ # @option strategy [String] allow_insert_at_same_position
169
+ def configure(options)
170
+ if options[:strategy]
171
+ @options[:strategy] = options[:strategy]
172
+ end
173
+ end
174
+
175
+ # It sets description of the rewrite or get description.
168
176
  # @example
169
177
  # Synvert::Rewriter.new 'rspec', 'use_new_syntax' do
170
178
  # description 'It converts rspec code to new syntax, it calls all rspec sub snippets.'
@@ -179,7 +187,7 @@ module Synvert::Core
179
187
  end
180
188
  end
181
189
 
182
- # Parse +if_ruby+ dsl, it checks if ruby version is greater than or equal to the specified ruby version.
190
+ # It checks if ruby version is greater than or equal to the specified ruby version.
183
191
  # @example
184
192
  # Synvert::Rewriter.new 'ruby', 'new_safe_navigation_operator' do
185
193
  # if_ruby '2.3.0'
@@ -189,7 +197,7 @@ module Synvert::Core
189
197
  @ruby_version = Rewriter::RubyVersion.new(version)
190
198
  end
191
199
 
192
- # Parse +if_gem+ dsl, it compares version of the specified gem.
200
+ # It compares version of the specified gem.
193
201
  # @example
194
202
  # Synvert::Rewriter.new 'rails', 'upgrade_5_2_to_6_0' do
195
203
  # if_gem 'rails', '>= 6.0'
@@ -200,7 +208,7 @@ module Synvert::Core
200
208
  @gem_spec = Rewriter::GemSpec.new(name, version)
201
209
  end
202
210
 
203
- # Parse +within_files+ dsl, it finds specified files.
211
+ # It finds specified files.
204
212
  # It creates a {Synvert::Core::Rewriter::Instance} to rewrite code.
205
213
  # @example
206
214
  # Synvert::Rewriter.new 'rspec', 'be_close_to_be_within' do
@@ -224,10 +232,10 @@ module Synvert::Core
224
232
  end
225
233
  end
226
234
 
227
- # Parse +within_file+ dsl, it finds a specifiled file.
235
+ # It finds a specifiled file.
228
236
  alias within_file within_files
229
237
 
230
- # Parses +add_file+ dsl, it adds a new file.
238
+ # It adds a new file.
231
239
  # @example
232
240
  # Synvert::Rewriter.new 'rails', 'add_application_record' do
233
241
  # add_file 'app/models/application_record.rb', <<~EOS
@@ -251,7 +259,7 @@ module Synvert::Core
251
259
  File.write(filepath, content)
252
260
  end
253
261
 
254
- # Parses +remove_file+ dsl, it removes a file.
262
+ # It removes a file.
255
263
  # @example
256
264
  # Synvert::Rewriter.new 'rails', 'upgrade_4_0_to_4_1' do
257
265
  # remove_file 'config/initializers/secret_token.rb'
@@ -264,7 +272,7 @@ module Synvert::Core
264
272
  File.delete(file_path) if File.exist?(file_path)
265
273
  end
266
274
 
267
- # Parse +add_snippet+ dsl, it calls anther rewriter.
275
+ # It calls anther rewriter.
268
276
  # @example
269
277
  # Synvert::Rewriter.new 'minitest', 'better_syntax' do
270
278
  # add_snippet 'minitest', 'assert_empty'
@@ -299,7 +307,7 @@ module Synvert::Core
299
307
  @sub_snippets << rewriter
300
308
  end
301
309
 
302
- # Parse +helper_method+ dsl, it defines helper method for {Synvert::Core::Rewriter::Instance}.
310
+ # It defines helper method for {Synvert::Core::Rewriter::Instance}.
303
311
  # @example
304
312
  # Synvert::Rewriter.new 'rails', 'convert_active_record_dirty_5_0_to_5_1' do
305
313
  # helper_method :find_callbacks_and_convert do |callback_names, callback_changes|
@@ -316,7 +324,7 @@ module Synvert::Core
316
324
  @helpers << { name: name, block: block }
317
325
  end
318
326
 
319
- # Parse +todo+ dsl, it sets todo of the rewriter.
327
+ # It sets todo of the rewriter.
320
328
  # Or get todo.
321
329
  # @example
322
330
  # Synvert::Rewriter.new 'rails', 'upgrade_3_2_to_4_0' do
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Synvert::Core
4
+ class Strategy
5
+ ALLOW_INSERT_AT_SAME_POSITION = "allow_insert_at_same_position"
6
+ end
7
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.13.1'
5
+ VERSION = '1.14.1'
6
6
  end
7
7
  end
data/lib/synvert/core.rb CHANGED
@@ -18,11 +18,13 @@ module Synvert
18
18
  autoload :Rewriter, 'synvert/core/rewriter'
19
19
  autoload :Engine, 'synvert/core/engine'
20
20
  autoload :Utils, 'synvert/core/utils'
21
+ autoload :Strategy, 'synvert/core/strategy'
21
22
  end
22
23
  end
23
24
 
24
25
  module Synvert
25
26
  Rewriter = Core::Rewriter
27
+ Strategy = Core::Strategy
26
28
 
27
29
  ALL_RUBY_FILES = %w[**/*.rb]
28
30
  ALL_RAKE_FILES = %w[**/*.rake]
@@ -128,7 +128,7 @@ module Synvert::Core
128
128
  instance.current_mutation = double
129
129
  instance.current_node = double
130
130
  expect(NodeMutation).to receive_message_chain(:adapter, :get_start_loc, :column).and_return(2)
131
- expect(instance.current_mutation).to receive(:insert).with(instance.current_node, "\n Foobar", at: 'end')
131
+ expect(instance.current_mutation).to receive(:insert).with(instance.current_node, "\n Foobar", at: 'end', to: nil)
132
132
  instance.insert_after 'Foobar'
133
133
  end
134
134
 
@@ -136,7 +136,7 @@ module Synvert::Core
136
136
  instance.current_mutation = double
137
137
  instance.current_node = double
138
138
  expect(NodeMutation).to receive_message_chain(:adapter, :get_start_loc, :column).and_return(2)
139
- expect(instance.current_mutation).to receive(:insert).with(instance.current_node, "Foobar\n ", at: 'beginning')
139
+ expect(instance.current_mutation).to receive(:insert).with(instance.current_node, "Foobar\n ", at: 'beginning', to: nil)
140
140
  instance.insert_before 'Foobar'
141
141
  end
142
142
 
@@ -197,6 +197,10 @@ module Synvert::Core
197
197
  instance.warn 'foobar'
198
198
  end
199
199
 
200
+ it 'parses any_value' do
201
+ expect(instance.any_value).to be_instance_of NodeQuery::AnyValue
202
+ end
203
+
200
204
  describe '#process' do
201
205
  let(:rewriter) { Rewriter.new('foo', 'bar') }
202
206
 
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: 1.13.1
4
+ version: 1.14.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-17 00:00:00.000000000 Z
11
+ date: 2022-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -133,7 +133,6 @@ files:
133
133
  - lib/synvert/core/node_ext.rb
134
134
  - lib/synvert/core/rewriter.rb
135
135
  - lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
136
- - lib/synvert/core/rewriter/any_value.rb
137
136
  - lib/synvert/core/rewriter/condition.rb
138
137
  - lib/synvert/core/rewriter/condition/if_exist_condition.rb
139
138
  - lib/synvert/core/rewriter/condition/if_only_exist_condition.rb
@@ -146,6 +145,7 @@ files:
146
145
  - lib/synvert/core/rewriter/scope/goto_scope.rb
147
146
  - lib/synvert/core/rewriter/scope/within_scope.rb
148
147
  - lib/synvert/core/rewriter/warning.rb
148
+ - lib/synvert/core/strategy.rb
149
149
  - lib/synvert/core/utils.rb
150
150
  - lib/synvert/core/version.rb
151
151
  - spec/spec_helper.rb
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Synvert::Core
4
- # A new type to match any value.
5
- class Rewriter::AnyValue
6
- end
7
- end