synvert-core 0.41.1 → 0.44.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b8def453eb2f8303d70dc014780f247e3d2e5e53e7cb9fc2a8d772f4e405dbe
4
- data.tar.gz: f5eac07008781607e399ba690871e4ea1e090db6da122068afad282181b6310f
3
+ metadata.gz: f858973b7f9b7bcfd9260bf3149cb76d841279e45302c52905164b2ae464e2f1
4
+ data.tar.gz: df45fb62ee7cf5c6aed86bc7e78a2c610b328b124e97a6ef6e009fdcc500d263
5
5
  SHA512:
6
- metadata.gz: dda7113a207fa2f5e615c298a4d93344eee9fcca9f4634ff8b99192ef0d27b33d14735b0446fb5649d2100c8aba41e7067d6168a91d9e712fd634d160dce3701
7
- data.tar.gz: 144b8bb301190c6382737961cd13b1340ef676c40214ced7dc8705f49034accde152c5b16ddc3f692697779cdd5c760f2cf79de77c09b09f4c890dee25969d5d
6
+ metadata.gz: cc6cbfa30627b61cf345834fd736e3795fbc6c4a6e063758459245f53cfc1954d5bbc477dc6a262f8a8754e0dbcd0f089d2196ef7edc7ce7e04b6141a374808e
7
+ data.tar.gz: 5bb7b3ad077e71753a4025416f62ee2c2979b8048466d34043c267b26ec92bf50ab850d57abaffd8f32013dab1d314ced834ed0b2f0e1bedbf70cb3c61d34fc9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
+ # 0.44.0 (2021-07-19)
4
+
5
+ * Return rewrtier after executing snippet
6
+ * `left_value` and `right_value` support `or_asgn` node
7
+ * `child_node_range` supports send `parentheses`
8
+
9
+ # 0.42.0 (2021-07-11)
10
+
11
+ * Match string with quote
12
+ * `match_value?` returns true if actual and expected are the same
13
+
3
14
  ## 0.41.0 (2021-06-24)
4
15
 
5
16
  * Remove unused autoindent option
@@ -217,6 +217,8 @@ module Parser::AST
217
217
  def left_value
218
218
  if %i[masgn lvasgn ivasgn].include? type
219
219
  children[0]
220
+ elsif :or_asgn == type
221
+ children[0].children[0]
220
222
  else
221
223
  raise Synvert::Core::MethodNotSupported, "left_value is not handled for #{debug_info}"
222
224
  end
@@ -227,7 +229,7 @@ module Parser::AST
227
229
  # @return [Array<Parser::AST::Node>] variable nodes.
228
230
  # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
229
231
  def right_value
230
- if %i[masgn lvasgn ivasgn].include? type
232
+ if %i[masgn lvasgn ivasgn or_asgn].include? type
231
233
  children[1]
232
234
  else
233
235
  raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{debug_info}"
@@ -240,7 +242,7 @@ module Parser::AST
240
242
  # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
241
243
  def to_value
242
244
  case type
243
- when :int, :str, :sym
245
+ when :int, :float, :str, :sym
244
246
  children.last
245
247
  when :true
246
248
  true
@@ -253,7 +255,7 @@ module Parser::AST
253
255
  when :begin
254
256
  children.first.to_value
255
257
  else
256
- raise Synvert::Core::MethodNotSupported, "to_value is not handled for #{debug_info}"
258
+ self
257
259
  end
258
260
  end
259
261
 
@@ -360,6 +362,12 @@ module Parser::AST
360
362
  else
361
363
  loc.selector
362
364
  end
365
+ when %i[send parentheses]
366
+ if loc.begin && loc.end
367
+ Parser::Source::Range.new('(string)', loc.begin.begin_pos, loc.end.end_pos)
368
+ else
369
+ nil
370
+ end
363
371
  else
364
372
  if respond_to?(child_name)
365
373
  child_node = send(child_name)
@@ -493,6 +501,8 @@ module Parser::AST
493
501
  # @return [Integer] -1, 0 or 1.
494
502
  # @raise [Synvert::Core::MethodNotSupported] if expected class is not supported.
495
503
  def match_value?(actual, expected)
504
+ return true if actual == expected
505
+
496
506
  case expected
497
507
  when Symbol
498
508
  if actual.is_a?(Parser::AST::Node)
@@ -502,10 +512,11 @@ module Parser::AST
502
512
  end
503
513
  when String
504
514
  if actual.is_a?(Parser::AST::Node)
515
+ return true if (Parser::CurrentRuby.parse(expected) == actual rescue nil)
505
516
  actual.to_source == expected || (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) ||
506
517
  actual.to_source[1...-1] == expected
507
518
  else
508
- actual.to_s == expected
519
+ actual.to_s == expected || wrap_quote(actual.to_s) == expected
509
520
  end
510
521
  when Regexp
511
522
  if actual.is_a?(Parser::AST::Node)
@@ -563,11 +574,7 @@ module Parser::AST
563
574
  # @param multi_keys [Array<Symbol>]
564
575
  # @return [Object] actual value.
565
576
  def actual_value(node, multi_keys)
566
- multi_keys.inject(node) { |n, key|
567
- if n
568
- key == :source ? n.send(key) : n.send(key)
569
- end
570
- }
577
+ multi_keys.inject(node) { |n, key| n.send(key) if n }
571
578
  end
572
579
 
573
580
  # Get expected value from rules.
@@ -578,5 +585,13 @@ module Parser::AST
578
585
  def expected_value(rules, multi_keys)
579
586
  multi_keys.inject(rules) { |o, key| o[key] }
580
587
  end
588
+
589
+ def wrap_quote(string)
590
+ if string.include?("'")
591
+ "\"#{string}\""
592
+ else
593
+ "'#{string}'"
594
+ end
595
+ end
581
596
  end
582
597
  end
@@ -52,7 +52,9 @@ module Synvert::Core
52
52
  #
53
53
  # @param block [Block] a block defines the behaviors of the rewriter.
54
54
  def execute(&block)
55
- Rewriter.new('', '', &block).process
55
+ rewriter = Rewriter.new('', '', &block)
56
+ rewriter.process
57
+ rewriter
56
58
  end
57
59
 
58
60
  # Register a rewriter with its group and name.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.41.1'
5
+ VERSION = '0.44.0'
6
6
  end
7
7
  end
@@ -261,6 +261,11 @@ describe Parser::AST::Node do
261
261
  node = parse('@a = 1')
262
262
  expect(node.left_value).to eq :@a
263
263
  end
264
+
265
+ it 'gets for or_asgn' do
266
+ node = parse('a ||= 1')
267
+ expect(node.left_value).to eq :a
268
+ end
264
269
  end
265
270
 
266
271
  describe '#right_value' do
@@ -283,6 +288,11 @@ describe Parser::AST::Node do
283
288
  node = parse('@a = 1')
284
289
  expect(node.right_value).to eq parse('1')
285
290
  end
291
+
292
+ it 'gets for or_asgn' do
293
+ node = parse('a ||= 1')
294
+ expect(node.right_value).to eq parse('1')
295
+ end
286
296
  end
287
297
 
288
298
  describe '#to_value' do
@@ -291,6 +301,11 @@ describe Parser::AST::Node do
291
301
  expect(node.to_value).to eq 1
292
302
  end
293
303
 
304
+ it 'gets for float' do
305
+ node = parse('1.5')
306
+ expect(node.to_value).to eq 1.5
307
+ end
308
+
294
309
  it 'gets for string' do
295
310
  node = parse("'str'")
296
311
  expect(node.to_value).to eq 'str'
@@ -385,11 +400,6 @@ describe Parser::AST::Node do
385
400
  end
386
401
 
387
402
  describe '#match?' do
388
- let(:instance) {
389
- rewriter = Synvert::Rewriter.new('foo', 'bar')
390
- Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
391
- }
392
-
393
403
  it 'matches class name' do
394
404
  source = 'class Synvert; end'
395
405
  node = parse(source)
@@ -414,18 +424,42 @@ describe Parser::AST::Node do
414
424
  expect(node).to be_match(type: 'send', arguments: [0])
415
425
  end
416
426
 
427
+ it 'matches assign float' do
428
+ source = 'at_least(1.5)'
429
+ node = parse(source)
430
+ expect(node).to be_match(type: 'send', arguments: [1.5])
431
+ end
432
+
417
433
  it 'matches arguments with string' do
418
434
  source = 'params["user"]'
419
435
  node = parse(source)
420
436
  expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ['user'])
421
437
  end
422
438
 
439
+ it 'matches arguments with string 2' do
440
+ source = 'params["user"]'
441
+ node = parse(source)
442
+ expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ["'user'"])
443
+ end
444
+
445
+ it 'matches arguments with string 3' do
446
+ source = "{ notice: 'Welcome' }"
447
+ node = parse(source)
448
+ expect(node).to be_match(type: 'hash', notice_value: "'Welcome'")
449
+ end
450
+
423
451
  it 'matches arguments any' do
424
452
  source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
425
453
  node = parse(source)
426
454
  expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
427
455
  end
428
456
 
457
+ it 'matches arguments with nested hash' do
458
+ source = '{ user_id: user.id }'
459
+ node = parse(source)
460
+ expect(node).to be_match(type: 'hash', user_id_value: { type: 'send', receiver: { type: 'send', message: 'user' }, message: 'id' })
461
+ end
462
+
429
463
  it 'matches arguments contain' do
430
464
  source = 'def slow(foo, bar, &block); end'
431
465
  node = parse(source)
@@ -590,15 +624,24 @@ describe Parser::AST::Node do
590
624
  range = node.child_node_range(:arguments)
591
625
  expect(range).to be_nil
592
626
  end
627
+
628
+ it 'checks parentheses' do
629
+ node = parse('foo.bar(test)')
630
+ range = node.child_node_range(:parentheses)
631
+ expect(range.to_range).to eq(7...13)
632
+
633
+ node = parse('foobar(test)')
634
+ range = node.child_node_range(:parentheses)
635
+ expect(range.to_range).to eq(6...12)
636
+
637
+ node = parse('foo.bar')
638
+ range = node.child_node_range(:parentheses)
639
+ expect(range).to be_nil
640
+ end
593
641
  end
594
642
  end
595
643
 
596
644
  describe '#rewritten_source' do
597
- let(:instance) {
598
- rewriter = Synvert::Rewriter.new('foo', 'bar')
599
- Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
600
- }
601
-
602
645
  it 'does not rewrite with unknown method' do
603
646
  source = 'class Synvert; end'
604
647
  node = parse(source)
@@ -155,22 +155,20 @@ module Synvert::Core
155
155
  replace_with 'create {{arguments}}'
156
156
  end
157
157
  end
158
- input =
159
- "
160
- it 'uses factory_girl' do
161
- user = FactoryGirl.create :user
162
- post = FactoryGirl.create :post, user: user
163
- assert post.valid?
164
- end
165
- "
166
- output =
167
- "
168
- it 'uses factory_girl' do
169
- user = create :user
170
- post = create :post, user: user
171
- assert post.valid?
172
- end
173
- "
158
+ input = <<~EOS
159
+ it 'uses factory_girl' do
160
+ user = FactoryGirl.create :user
161
+ post = FactoryGirl.create :post, user: user
162
+ assert post.valid?
163
+ end
164
+ EOS
165
+ output = <<~EOS
166
+ it 'uses factory_girl' do
167
+ user = create :user
168
+ post = create :post, user: user
169
+ assert post.valid?
170
+ end
171
+ EOS
174
172
  expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
175
173
  expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
176
174
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
@@ -186,18 +184,16 @@ end
186
184
  end
187
185
  end
188
186
  end
189
- input =
190
- '
191
- RSpec.configure do |config|
192
- config.include FactoryGirl::Syntax::Methods
193
- end
194
- '
195
- output =
196
- '
197
- RSpec.configure do |config|
198
- config.include FactoryGirl::Syntax::Methods
199
- end
200
- '
187
+ input = <<~EOS
188
+ RSpec.configure do |config|
189
+ config.include FactoryGirl::Syntax::Methods
190
+ end
191
+ EOS
192
+ output = <<~EOS
193
+ RSpec.configure do |config|
194
+ config.include FactoryGirl::Syntax::Methods
195
+ end
196
+ EOS
201
197
  expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
202
198
  expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input)
203
199
  expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
@@ -213,18 +209,16 @@ end
213
209
  end
214
210
  end
215
211
  end
216
- input =
217
- '
218
- RSpec.configure do |config|
219
- config.include FactoryGirl::Syntax::Methods
220
- end
221
- '
222
- output =
223
- '
224
- RSpec.configure do |config|
225
- config.include FactoryGirl::Syntax::Methods
226
- end
227
- '
212
+ input = <<~EOS
213
+ RSpec.configure do |config|
214
+ config.include FactoryGirl::Syntax::Methods
215
+ end
216
+ EOS
217
+ output = <<~EOS
218
+ RSpec.configure do |config|
219
+ config.include FactoryGirl::Syntax::Methods
220
+ end
221
+ EOS
228
222
  expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb']).twice
229
223
  expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input).once
230
224
  expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
@@ -239,22 +233,20 @@ end
239
233
  replace_with 'create {{arguments}}'
240
234
  end
241
235
  end
242
- input =
243
- "
244
- it 'uses factory_girl' do
245
- user = FactoryGirl.create :user
246
- post = FactoryGirl.create :post, user: user
247
- assert post.valid?
248
- end
249
- "
250
- output =
251
- "
252
- it 'uses factory_girl' do
253
- user = create :user
254
- post = create :post, user: user
255
- assert post.valid?
256
- end
257
- "
236
+ input = <<~EOS
237
+ it 'uses factory_girl' do
238
+ user = FactoryGirl.create :user
239
+ post = FactoryGirl.create :post, user: user
240
+ assert post.valid?
241
+ end
242
+ EOS
243
+ output = <<~EOS
244
+ it 'uses factory_girl' do
245
+ user = create :user
246
+ post = create :post, user: user
247
+ assert post.valid?
248
+ end
249
+ EOS
258
250
  expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb']).twice
259
251
  expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
260
252
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
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.41.1
4
+ version: 0.44.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-07-04 00:00:00.000000000 Z
11
+ date: 2021-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport