synvert-core 0.45.2 → 0.49.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -3
- data/README.md +4 -2
- data/lib/synvert/core/node_ext.rb +49 -3
- data/lib/synvert/core/rewriter/helper.rb +1 -1
- data/lib/synvert/core/rewriter/instance.rb +1 -1
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +94 -2
- data/spec/synvert/core/rewriter/instance_spec.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff7201f9f25e9092d5bebf6359bf5fb7889d37dae90187666fc208a2a688d01d
|
4
|
+
data.tar.gz: 6ff9746cef63978f4cbd62cdc9862dabab9f8ba10512f12bbc1292dda19d6483
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7b44b09b43013829adeb097eafbf143da5d2acbeb731eeb6239388e39dd596c6946ae9cccd91c6e6c9d795bd8118e7a375794f18692461b572bad425b076c9b
|
7
|
+
data.tar.gz: 3b14e2a314d04bb0699125c69bef1a5ed46e2ebf79123017c342dccf66ccf6eb1b1f39f27cca3fa817e5eae51ab41d587b606c0a714bd6df1cbb862b3b9e4646
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,25 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.49.0 (2021-08-04)
|
4
4
|
|
5
|
-
*
|
5
|
+
* Support :erange in to_value
|
6
|
+
* Do not use to_value in match_value?
|
7
|
+
|
8
|
+
## 0.48.0 (2021-08-01)
|
9
|
+
|
10
|
+
* Force to read file as utf-8
|
11
|
+
* Add logo
|
6
12
|
|
7
|
-
## 0.
|
13
|
+
## 0.47.0 (2021-07-28)
|
8
14
|
|
15
|
+
* Add `to_single_quote` to `str` node
|
16
|
+
* Add `to_symbol` to `str` node
|
17
|
+
* Add `to_lambda_literal` to `lambda` node
|
18
|
+
|
19
|
+
## 0.46.0 (2021-07-25)
|
20
|
+
|
21
|
+
* Add `strip_curly_braces` and `wrap_curly_braces` for `hash` node
|
22
|
+
* Simplify symbol `match_value?`
|
9
23
|
* Unwrap quote when matching string value
|
10
24
|
|
11
25
|
## 0.45.0 (2021-07-22)
|
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]:
|
39
|
-
[2]:
|
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
|
@@ -490,6 +492,50 @@ module Parser::AST
|
|
490
492
|
end
|
491
493
|
end
|
492
494
|
|
495
|
+
# strip curly braces for hash
|
496
|
+
def strip_curly_braces
|
497
|
+
return to_source unless type == :hash
|
498
|
+
|
499
|
+
to_source.sub(/^{(.*)}$/) { Regexp.last_match(1).strip }
|
500
|
+
end
|
501
|
+
|
502
|
+
# wrap curly braces for hash
|
503
|
+
def wrap_curly_braces
|
504
|
+
return to_source unless type == :hash
|
505
|
+
|
506
|
+
"{ #{to_source} }"
|
507
|
+
end
|
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
|
+
|
493
539
|
private
|
494
540
|
|
495
541
|
# Compare actual value with expected value.
|
@@ -504,14 +550,14 @@ module Parser::AST
|
|
504
550
|
case expected
|
505
551
|
when Symbol
|
506
552
|
if actual.is_a?(Parser::AST::Node)
|
507
|
-
actual.
|
553
|
+
actual.to_source == ":#{expected}"
|
508
554
|
else
|
509
555
|
actual.to_sym == expected
|
510
556
|
end
|
511
557
|
when String
|
512
558
|
if actual.is_a?(Parser::AST::Node)
|
513
|
-
actual.to_source == expected || actual.
|
514
|
-
actual.to_source ==
|
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)
|
515
561
|
else
|
516
562
|
actual.to_s == expected || wrap_quote(actual.to_s) == expected
|
517
563
|
end
|
@@ -66,7 +66,7 @@ module Synvert::Core
|
|
66
66
|
# strip_brackets("(1..100)") #=> "1..100"
|
67
67
|
def strip_brackets(code)
|
68
68
|
code.sub(/^\((.*)\)$/) { Regexp.last_match(1) }.sub(/^\[(.*)\]$/) { Regexp.last_match(1) }.sub(/^{(.*)}$/) {
|
69
|
-
Regexp.last_match(1)
|
69
|
+
Regexp.last_match(1).strip
|
70
70
|
}
|
71
71
|
end
|
72
72
|
|
data/lib/synvert/core/version.rb
CHANGED
@@ -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 '
|
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 '
|
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]
|
@@ -679,4 +684,91 @@ describe Parser::AST::Node do
|
|
679
684
|
EOS
|
680
685
|
end
|
681
686
|
end
|
687
|
+
|
688
|
+
describe '#strip_curly_braces' do
|
689
|
+
context 'hash node' do
|
690
|
+
it 'removes curly braces' do
|
691
|
+
node = parse("{ foo: 'bar' }")
|
692
|
+
expect(node.strip_curly_braces).to eq("foo: 'bar'")
|
693
|
+
end
|
694
|
+
end
|
695
|
+
|
696
|
+
context 'other node' do
|
697
|
+
it 'do nothing' do
|
698
|
+
node = parse("'foobar'")
|
699
|
+
expect(node.strip_curly_braces).to eq("'foobar'")
|
700
|
+
end
|
701
|
+
end
|
702
|
+
end
|
703
|
+
|
704
|
+
describe '#wrap_curly_braces' do
|
705
|
+
context 'hash node' do
|
706
|
+
it 'adds curly braces' do
|
707
|
+
node = parse("test(foo: 'bar')").arguments.first
|
708
|
+
expect(node.to_source).to eq("foo: 'bar'")
|
709
|
+
expect(node.wrap_curly_braces).to eq("{ foo: 'bar' }")
|
710
|
+
end
|
711
|
+
end
|
712
|
+
|
713
|
+
context 'other node' do
|
714
|
+
it 'does nothing' do
|
715
|
+
node = parse("'foobar'")
|
716
|
+
expect(node.wrap_curly_braces).to eq("'foobar'")
|
717
|
+
end
|
718
|
+
end
|
719
|
+
end
|
720
|
+
|
721
|
+
describe '#to_single_quote' do
|
722
|
+
context 'str node' do
|
723
|
+
it 'converts double quote to single quote' do
|
724
|
+
node = parse('"foobar"')
|
725
|
+
expect(node.to_source).to eq '"foobar"'
|
726
|
+
expect(node.to_single_quote).to eq "'foobar'"
|
727
|
+
end
|
728
|
+
end
|
729
|
+
|
730
|
+
context 'other node' do
|
731
|
+
it 'does nothing' do
|
732
|
+
node = parse(':foobar')
|
733
|
+
expect(node.to_single_quote).to eq ':foobar'
|
734
|
+
end
|
735
|
+
end
|
736
|
+
end
|
737
|
+
|
738
|
+
describe '#to_symbol' do
|
739
|
+
context 'str node' do
|
740
|
+
it 'converts string to symbol' do
|
741
|
+
node = parse("'foobar'")
|
742
|
+
expect(node.to_symbol).to eq ':foobar'
|
743
|
+
end
|
744
|
+
end
|
745
|
+
|
746
|
+
context 'other node' do
|
747
|
+
it 'does nothing' do
|
748
|
+
node = parse(':foobar')
|
749
|
+
expect(node.to_symbol).to eq ':foobar'
|
750
|
+
end
|
751
|
+
end
|
752
|
+
end
|
753
|
+
|
754
|
+
describe '#to_lambda_literal' do
|
755
|
+
context 'lambda node' do
|
756
|
+
it 'converts to lambda literal without arguments' do
|
757
|
+
node = parse('lambda { foobar }')
|
758
|
+
expect(node.to_lambda_literal).to eq('-> { foobar }')
|
759
|
+
end
|
760
|
+
|
761
|
+
it 'converts to lambda literal with arguments' do
|
762
|
+
node = parse('lambda { |x, y| foobar }')
|
763
|
+
expect(node.to_lambda_literal).to eq('->(x, y) { foobar }')
|
764
|
+
end
|
765
|
+
end
|
766
|
+
|
767
|
+
context 'other node' do
|
768
|
+
it 'does nothing' do
|
769
|
+
node = parse(':foobar')
|
770
|
+
expect(node.to_lambda_literal).to eq ':foobar'
|
771
|
+
end
|
772
|
+
end
|
773
|
+
end
|
682
774
|
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.
|
4
|
+
version: 0.49.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-
|
11
|
+
date: 2021-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|