synvert-core 0.41.0 → 0.42.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: 4b54b506cc880f8fff9834b131659fd6f32cc60082bfade8a07d2d9a767352dd
4
- data.tar.gz: 0ec762ecd158a05142674be271a10a0cbbe260ae7d4e0f0cd36ab4d426cefcd7
3
+ metadata.gz: c29440498ff5186fde3ea7828482bfc0e93551350682f68b2fd26f3054894460
4
+ data.tar.gz: 055d1a185ed10f154f3b6a5ee99d8f142ead2a62935e461f3b73d15a32822496
5
5
  SHA512:
6
- metadata.gz: dabaa021af949aa5b15bbe5830d2e6154eb1679d5144d614c933c48d5767b92f0b1ad10b281d7d3819f755a762677c7dac26d696b501710e29f9730188f4afe9
7
- data.tar.gz: 319e76c109e66ee558ff5c844ba3c9a1f3b6be1f2e6ca769bb3a97dedb19c6c0e4cf7dd22f7e04ff9a2508722f2a96e77e4f7521356f1c97ad0aef0e9959fe0f
6
+ metadata.gz: 757170374b87d2941befeaa9e1d17681bfff6bd8486880dec9fa9549e4a71ec660983e7f8bf3d9e71046ae4d24c4fc4ec0282aba71d62cd5de240d3f36962f5e
7
+ data.tar.gz: ad55dd58dc35f53804d11a99f2dc926881b84a05eb0e13ab3f0a3c07e374fddb1dd8ddae91ec1d8a36a3564d3c61ab3bf1c0282ac45193cb4cc93e90500ef17d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ # 0.42.1 (2021-07-14)
4
+
5
+ * Return rewrtier after executing snippet
6
+
7
+ # 0.42.0 (2021-07-11)
8
+
9
+ * Match string with quote
10
+ * `match_value?` returns true if actual and expected are the same
11
+
3
12
  ## 0.41.0 (2021-06-24)
4
13
 
5
14
  * Remove unused autoindent option
@@ -240,7 +240,7 @@ module Parser::AST
240
240
  # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
241
241
  def to_value
242
242
  case type
243
- when :int, :str, :sym
243
+ when :int, :float, :str, :sym
244
244
  children.last
245
245
  when :true
246
246
  true
@@ -253,7 +253,7 @@ module Parser::AST
253
253
  when :begin
254
254
  children.first.to_value
255
255
  else
256
- raise Synvert::Core::MethodNotSupported, "to_value is not handled for #{debug_info}"
256
+ self
257
257
  end
258
258
  end
259
259
 
@@ -388,7 +388,7 @@ module Parser::AST
388
388
  end
389
389
 
390
390
  raise Synvert::Core::MethodNotSupported,
391
- "child_node_range is not handled for #{evaluated.inspect}, child_name: #{child_name}"
391
+ "child_node_range is not handled for #{debug_info}, child_name: #{child_name}"
392
392
  end
393
393
  end
394
394
 
@@ -493,6 +493,8 @@ module Parser::AST
493
493
  # @return [Integer] -1, 0 or 1.
494
494
  # @raise [Synvert::Core::MethodNotSupported] if expected class is not supported.
495
495
  def match_value?(actual, expected)
496
+ return true if actual == expected
497
+
496
498
  case expected
497
499
  when Symbol
498
500
  if actual.is_a?(Parser::AST::Node)
@@ -502,10 +504,11 @@ module Parser::AST
502
504
  end
503
505
  when String
504
506
  if actual.is_a?(Parser::AST::Node)
507
+ return true if (Parser::CurrentRuby.parse(expected) == actual rescue nil)
505
508
  actual.to_source == expected || (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) ||
506
509
  actual.to_source[1...-1] == expected
507
510
  else
508
- actual.to_s == expected
511
+ actual.to_s == expected || wrap_quote(actual.to_s) == expected
509
512
  end
510
513
  when Regexp
511
514
  if actual.is_a?(Parser::AST::Node)
@@ -563,11 +566,7 @@ module Parser::AST
563
566
  # @param multi_keys [Array<Symbol>]
564
567
  # @return [Object] actual value.
565
568
  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
- }
569
+ multi_keys.inject(node) { |n, key| n.send(key) if n }
571
570
  end
572
571
 
573
572
  # Get expected value from rules.
@@ -578,5 +577,13 @@ module Parser::AST
578
577
  def expected_value(rules, multi_keys)
579
578
  multi_keys.inject(rules) { |o, key| o[key] }
580
579
  end
580
+
581
+ def wrap_quote(string)
582
+ if string.include?("'")
583
+ "\"#{string}\""
584
+ else
585
+ "'#{string}'"
586
+ end
587
+ end
581
588
  end
582
589
  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.0'
5
+ VERSION = '0.42.1'
6
6
  end
7
7
  end
@@ -291,6 +291,11 @@ describe Parser::AST::Node do
291
291
  expect(node.to_value).to eq 1
292
292
  end
293
293
 
294
+ it 'gets for float' do
295
+ node = parse('1.5')
296
+ expect(node.to_value).to eq 1.5
297
+ end
298
+
294
299
  it 'gets for string' do
295
300
  node = parse("'str'")
296
301
  expect(node.to_value).to eq 'str'
@@ -385,11 +390,6 @@ describe Parser::AST::Node do
385
390
  end
386
391
 
387
392
  describe '#match?' do
388
- let(:instance) {
389
- rewriter = Synvert::Rewriter.new('foo', 'bar')
390
- Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
391
- }
392
-
393
393
  it 'matches class name' do
394
394
  source = 'class Synvert; end'
395
395
  node = parse(source)
@@ -414,18 +414,42 @@ describe Parser::AST::Node do
414
414
  expect(node).to be_match(type: 'send', arguments: [0])
415
415
  end
416
416
 
417
+ it 'matches assign float' do
418
+ source = 'at_least(1.5)'
419
+ node = parse(source)
420
+ expect(node).to be_match(type: 'send', arguments: [1.5])
421
+ end
422
+
417
423
  it 'matches arguments with string' do
418
424
  source = 'params["user"]'
419
425
  node = parse(source)
420
426
  expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ['user'])
421
427
  end
422
428
 
429
+ it 'matches arguments with string 2' do
430
+ source = 'params["user"]'
431
+ node = parse(source)
432
+ expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ["'user'"])
433
+ end
434
+
435
+ it 'matches arguments with string 3' do
436
+ source = "{ notice: 'Welcome' }"
437
+ node = parse(source)
438
+ expect(node).to be_match(type: 'hash', notice_value: "'Welcome'")
439
+ end
440
+
423
441
  it 'matches arguments any' do
424
442
  source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
425
443
  node = parse(source)
426
444
  expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
427
445
  end
428
446
 
447
+ it 'matches arguments with nested hash' do
448
+ source = '{ user_id: user.id }'
449
+ node = parse(source)
450
+ expect(node).to be_match(type: 'hash', user_id_value: { type: 'send', receiver: { type: 'send', message: 'user' }, message: 'id' })
451
+ end
452
+
429
453
  it 'matches arguments contain' do
430
454
  source = 'def slow(foo, bar, &block); end'
431
455
  node = parse(source)
@@ -594,11 +618,6 @@ describe Parser::AST::Node do
594
618
  end
595
619
 
596
620
  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
621
  it 'does not rewrite with unknown method' do
603
622
  source = 'class Synvert; end'
604
623
  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.0
4
+ version: 0.42.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-06-24 00:00:00.000000000 Z
11
+ date: 2021-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport