synvert-core 0.41.2 → 0.44.1

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: 9e9e29097976ca69f755a698d9a3a78769105c4043b07c96a7ac1e714ae659a0
4
- data.tar.gz: 12d45e30a5de2ffc03bb440b503ff1a230e7baa55eb6a69e43518023e7d24f08
3
+ metadata.gz: 252c2e9347770f9363ca9d317d574fa6e1d9faad4826d2853816a669ad13dab1
4
+ data.tar.gz: 1d19aac0f1dac17a84dd4a85bcbb8606d9391afeb9c4493eff516cf2b5ed948f
5
5
  SHA512:
6
- metadata.gz: 28802dbdd2f53b267c621f811b959a0041064f97ff4f54ee009f75d230eeb2a1ff3fbb9b1c3cee524d990f53b8e7447105fa57e86cb4ee2b042cd4e17ef63c9e
7
- data.tar.gz: 8c0f5604e24d9ea0034214aa2b4d36f26d6c5f181d7d0d70f75a78fe71b0cab1868379953b2d071ea3ab922e62855a51ee5a0cb14ed7b4e26ebb8cc57adcb396
6
+ metadata.gz: 795910a3044cc4cdbf02e0412a5784235b7c8fc0892a1243cf58677c53d09e1cdf097d7f56cf8f818864de0eab6ec65d829c59e2ba246e1d1150bdd3fd5e2b40
7
+ data.tar.gz: fc2f006fd4b269f598cd23d05ebfcf72b098ce4a7ccfaa1a5d6004517d058caabea1005e999d2ccaf92a73bae34941b4bc8b37124db98ae90a71730c7e749098
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # CHANGELOG
2
2
 
3
+ # 0.44.1 (2021-07-19)
4
+
5
+ * Handle `nil` child node for `begin_pos` and `end_pos`
6
+
7
+ # 0.44.0 (2021-07-19)
8
+
9
+ * Return rewrtier after executing snippet
10
+ * `left_value` and `right_value` support `or_asgn` node
11
+ * `child_node_range` supports send `parentheses`
12
+
13
+ # 0.42.0 (2021-07-11)
14
+
15
+ * Match string with quote
16
+ * `match_value?` returns true if actual and expected are the same
17
+
3
18
  ## 0.41.0 (2021-06-24)
4
19
 
5
20
  * 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,10 @@ 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
+ end
363
369
  else
364
370
  if respond_to?(child_name)
365
371
  child_node = send(child_name)
@@ -504,10 +510,11 @@ module Parser::AST
504
510
  end
505
511
  when String
506
512
  if actual.is_a?(Parser::AST::Node)
513
+ return true if (Parser::CurrentRuby.parse(expected) == actual rescue nil)
507
514
  actual.to_source == expected || (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) ||
508
515
  actual.to_source[1...-1] == expected
509
516
  else
510
- actual.to_s == expected
517
+ actual.to_s == expected || wrap_quote(actual.to_s) == expected
511
518
  end
512
519
  when Regexp
513
520
  if actual.is_a?(Parser::AST::Node)
@@ -565,11 +572,7 @@ module Parser::AST
565
572
  # @param multi_keys [Array<Symbol>]
566
573
  # @return [Object] actual value.
567
574
  def actual_value(node, multi_keys)
568
- multi_keys.inject(node) { |n, key|
569
- if n
570
- key == :source ? n.send(key) : n.send(key)
571
- end
572
- }
575
+ multi_keys.inject(node) { |n, key| n.send(key) if n }
573
576
  end
574
577
 
575
578
  # Get expected value from rules.
@@ -580,5 +583,13 @@ module Parser::AST
580
583
  def expected_value(rules, multi_keys)
581
584
  multi_keys.inject(rules) { |o, key| o[key] }
582
585
  end
586
+
587
+ def wrap_quote(string)
588
+ if string.include?("'")
589
+ "\"#{string}\""
590
+ else
591
+ "'#{string}'"
592
+ end
593
+ end
583
594
  end
584
595
  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.
@@ -12,14 +12,14 @@ module Synvert::Core
12
12
  #
13
13
  # @return [Integer] begin position.
14
14
  def begin_pos
15
- @selectors.map { |selector| @node.child_node_range(selector).begin_pos }.min
15
+ @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:begin_pos).min
16
16
  end
17
17
 
18
18
  # End position of code to replace.
19
19
  #
20
20
  # @return [Integer] end position.
21
21
  def end_pos
22
- @selectors.map { |selector| @node.child_node_range(selector).end_pos }.max
22
+ @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:end_pos).max
23
23
  end
24
24
 
25
25
  # The rewritten code, always empty string.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.41.2'
5
+ VERSION = '0.44.1'
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.2
4
+ version: 0.44.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: 2021-07-06 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