synvert-core 0.46.0 → 0.49.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: 5251fd7b7ac886ba35aeb23c94e8d25bf2d26e2124db5d454d8994bdc5cecff3
4
- data.tar.gz: 5b8395085acf698b7715a3cd48e3d22539308cc81e1731081c3b48dd00210ba7
3
+ metadata.gz: 7b694eae9a3ae1910d61accf8d93a52511c49cfa7493ccfb0d8d55f9c42d5b88
4
+ data.tar.gz: fed8c019f692ad9a4846961558459bf302aa72266fe06c870ba4cdeb1f17d7a8
5
5
  SHA512:
6
- metadata.gz: 65e5e5a87c8bbe7d5d824d216c02764042f4b585705887a1a1c69469e389273b8e5ccfc00c3af2dc1003b660d49751c6365ff176980a72f6d663bfc1df60c31b
7
- data.tar.gz: 9c47b80c577c5595c665658bf824c0f0f635c7bc09ceee6b0e865c636b4445d28fd449087e8eededc51d9d34eb2ab9fcd19899af676a228850ae630e3c10a726
6
+ metadata.gz: fb38d31674706ffe1bece171664e05869a66b8362e1e5021b7981c52b04185709afa7b986fd8efcb1070d28bd4e405926a09f73553721f9b9af0c543293377d6
7
+ data.tar.gz: 93eecc39d451a9d2cbff536db0f99e78dbb0eb405943743c78f39bd8827c002e022ce323e1641d72336158e9eb8e2f73135e2d7e3c9eb12455ce975a369dd854
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.49.1 (2021-08-04)
4
+
5
+ * Fix symbol match
6
+
7
+ ## 0.49.0 (2021-08-04)
8
+
9
+ * Support :erange in to_value
10
+ * Do not use to_value in match_value?
11
+
12
+ ## 0.48.0 (2021-08-01)
13
+
14
+ * Force to read file as utf-8
15
+ * Add logo
16
+
17
+ ## 0.47.0 (2021-07-28)
18
+
19
+ * Add `to_single_quote` to `str` node
20
+ * Add `to_symbol` to `str` node
21
+ * Add `to_lambda_literal` to `lambda` node
22
+
3
23
  ## 0.46.0 (2021-07-25)
4
24
 
5
25
  * Add `strip_curly_braces` and `wrap_curly_braces` for `hash` node
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Synvert::Core
2
2
 
3
+ <img src="https://xinminlabs.github.io/synvert/img/logo_96.png" alt="logo" width="32" height="32" />
4
+
3
5
  ![Main workflow](https://github.com/xinminlabs/synvert-core/actions/workflows/main.yml/badge.svg)
4
6
  [![Coverage Status](https://coveralls.io/repos/xinminlabs/synvert-core/badge.png?branch=master)](https://coveralls.io/r/xinminlabs/synvert-core)
5
7
  [![Gem Version](https://badge.fury.io/rb/synvert-core.png)](http://badge.fury.io/rb/synvert-core)
@@ -35,5 +37,5 @@ Or install it yourself as:
35
37
  4. Push to the branch (`git push origin my-new-feature`)
36
38
  5. Create a new Pull Request
37
39
 
38
- [1]: http://xinminlabs.github.io/synvert/
39
- [2]: http://rubydoc.info/github/xinminlabs/synvert-core/master/frames
40
+ [1]: https://xinminlabs.github.io/synvert/
41
+ [2]: https://rubydoc.info/github/xinminlabs/synvert-core/master/frames
@@ -252,6 +252,8 @@ module Parser::AST
252
252
  children.map(&:to_value)
253
253
  when :irange
254
254
  (children.first.to_value..children.last.to_value)
255
+ when :erange
256
+ (children.first.to_value...children.last.to_value)
255
257
  when :begin
256
258
  children.first.to_value
257
259
  else
@@ -504,6 +506,36 @@ module Parser::AST
504
506
  "{ #{to_source} }"
505
507
  end
506
508
 
509
+ # get single quote string
510
+ def to_single_quote
511
+ return to_source unless type == :str
512
+
513
+ "'#{to_value}'"
514
+ end
515
+
516
+ # convert string to symbol
517
+ def to_symbol
518
+ return to_source unless type == :str
519
+
520
+ ":#{to_value}"
521
+ end
522
+
523
+ # convert lambda {} to -> {}
524
+ def to_lambda_literal
525
+ if type == :block && caller.type == :send && caller.receiver.nil? && caller.message == :lambda
526
+ new_source = to_source
527
+ if arguments.size > 1
528
+ new_source = new_source[0...arguments.loc.begin.to_range.begin - 1] + new_source[arguments.loc.end.to_range.end..-1]
529
+ new_source = new_source.sub('lambda', "->(#{arguments.map(&:to_source).join(', ')})")
530
+ else
531
+ new_source = new_source.sub('lambda', '->')
532
+ end
533
+ new_source
534
+ else
535
+ to_source
536
+ end
537
+ end
538
+
507
539
  private
508
540
 
509
541
  # Compare actual value with expected value.
@@ -518,14 +550,14 @@ module Parser::AST
518
550
  case expected
519
551
  when Symbol
520
552
  if actual.is_a?(Parser::AST::Node)
521
- actual.to_value == expected
553
+ actual.to_source == ":#{expected}" || actual.to_source == expected.to_s
522
554
  else
523
555
  actual.to_sym == expected
524
556
  end
525
557
  when String
526
558
  if actual.is_a?(Parser::AST::Node)
527
- actual.to_source == expected || actual.to_value == expected ||
528
- actual.to_source == unwrap_quote(expected) || actual.to_value == unwrap_quote(expected)
559
+ actual.to_source == expected || actual.to_source == unwrap_quote(expected) ||
560
+ unwrap_quote(actual.to_source) == expected || unwrap_quote(actual.to_source) == unwrap_quote(expected)
529
561
  else
530
562
  actual.to_s == expected || wrap_quote(actual.to_s) == expected
531
563
  end
@@ -17,7 +17,7 @@ module Synvert::Core
17
17
  @file_source ||= {}
18
18
  @file_source[file_path] ||=
19
19
  begin
20
- source = File.read(file_path)
20
+ source = File.read(file_path, encoding: 'UTF-8')
21
21
  source = Engine::ERB.encode(source) if /\.erb$/.match?(file_path)
22
22
  source
23
23
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.46.0'
5
+ VERSION = '0.49.1'
6
6
  end
7
7
  end
@@ -316,18 +316,23 @@ describe Parser::AST::Node do
316
316
  expect(node.to_value).to eq :str
317
317
  end
318
318
 
319
- it 'get for boolean' do
319
+ it 'gets for boolean' do
320
320
  node = parse('true')
321
321
  expect(node.to_value).to be_truthy
322
322
  node = parse('false')
323
323
  expect(node.to_value).to be_falsey
324
324
  end
325
325
 
326
- it 'get for range' do
326
+ it 'gets for irange' do
327
327
  node = parse('(1..10)')
328
328
  expect(node.to_value).to eq(1..10)
329
329
  end
330
330
 
331
+ it 'gets for erange' do
332
+ node = parse('(1...10)')
333
+ expect(node.to_value).to eq(1...10)
334
+ end
335
+
331
336
  it 'gets for array' do
332
337
  node = parse("['str', :str]")
333
338
  expect(node.to_value).to eq ['str', :str]
@@ -418,6 +423,12 @@ describe Parser::AST::Node do
418
423
  expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: [:user])
419
424
  end
420
425
 
426
+ it 'matches pair key with symbol' do
427
+ source = '{ type: :model }'
428
+ node = parse(source).children[0]
429
+ expect(node).to be_match(type: 'pair', key: :type)
430
+ end
431
+
421
432
  it 'matches assign number' do
422
433
  source = 'at_least(0)'
423
434
  node = parse(source)
@@ -706,10 +717,64 @@ describe Parser::AST::Node do
706
717
  end
707
718
 
708
719
  context 'other node' do
709
- it 'do nothing' do
720
+ it 'does nothing' do
710
721
  node = parse("'foobar'")
711
722
  expect(node.wrap_curly_braces).to eq("'foobar'")
712
723
  end
713
724
  end
714
725
  end
726
+
727
+ describe '#to_single_quote' do
728
+ context 'str node' do
729
+ it 'converts double quote to single quote' do
730
+ node = parse('"foobar"')
731
+ expect(node.to_source).to eq '"foobar"'
732
+ expect(node.to_single_quote).to eq "'foobar'"
733
+ end
734
+ end
735
+
736
+ context 'other node' do
737
+ it 'does nothing' do
738
+ node = parse(':foobar')
739
+ expect(node.to_single_quote).to eq ':foobar'
740
+ end
741
+ end
742
+ end
743
+
744
+ describe '#to_symbol' do
745
+ context 'str node' do
746
+ it 'converts string to symbol' do
747
+ node = parse("'foobar'")
748
+ expect(node.to_symbol).to eq ':foobar'
749
+ end
750
+ end
751
+
752
+ context 'other node' do
753
+ it 'does nothing' do
754
+ node = parse(':foobar')
755
+ expect(node.to_symbol).to eq ':foobar'
756
+ end
757
+ end
758
+ end
759
+
760
+ describe '#to_lambda_literal' do
761
+ context 'lambda node' do
762
+ it 'converts to lambda literal without arguments' do
763
+ node = parse('lambda { foobar }')
764
+ expect(node.to_lambda_literal).to eq('-> { foobar }')
765
+ end
766
+
767
+ it 'converts to lambda literal with arguments' do
768
+ node = parse('lambda { |x, y| foobar }')
769
+ expect(node.to_lambda_literal).to eq('->(x, y) { foobar }')
770
+ end
771
+ end
772
+
773
+ context 'other node' do
774
+ it 'does nothing' do
775
+ node = parse(':foobar')
776
+ expect(node.to_lambda_literal).to eq ':foobar'
777
+ end
778
+ end
779
+ end
715
780
  end
@@ -170,7 +170,7 @@ module Synvert::Core
170
170
  end
171
171
  EOS
172
172
  expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
173
- expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
173
+ expect(File).to receive(:read).with('spec/models/post_spec.rb', encoding: 'UTF-8').and_return(input)
174
174
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
175
175
  instance.process
176
176
  end
@@ -195,7 +195,7 @@ module Synvert::Core
195
195
  end
196
196
  EOS
197
197
  expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
198
- expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input)
198
+ expect(File).to receive(:read).with('spec/spec_helper.rb', encoding: 'UTF-8').and_return(input)
199
199
  expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
200
200
  instance.process
201
201
  end
@@ -220,7 +220,7 @@ module Synvert::Core
220
220
  end
221
221
  EOS
222
222
  expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb']).twice
223
- expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input).once
223
+ expect(File).to receive(:read).with('spec/spec_helper.rb', encoding: 'UTF-8').and_return(input).once
224
224
  expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
225
225
  instance.process
226
226
  instance.process
@@ -248,9 +248,9 @@ module Synvert::Core
248
248
  end
249
249
  EOS
250
250
  expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb']).twice
251
- expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
251
+ expect(File).to receive(:read).with('spec/models/post_spec.rb', encoding: 'UTF-8').and_return(input)
252
252
  expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
253
- expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(output)
253
+ expect(File).to receive(:read).with('spec/models/post_spec.rb', encoding: 'UTF-8').and_return(output)
254
254
  instance.process
255
255
  instance.process
256
256
  expect(rewriter.affected_files).to be_include('spec/models/post_spec.rb')
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.46.0
4
+ version: 0.49.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-25 00:00:00.000000000 Z
11
+ date: 2021-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport