synvert-core 0.45.1 → 0.48.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 +4 -4
- data/CHANGELOG.md +17 -4
- data/README.md +4 -2
- data/lib/synvert/core/node_ext.rb +45 -1
- 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 +87 -0
- 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: d053ad88384be0c41958704354fff02d9c6d4aba71748ee4a57713e5502eb398
|
4
|
+
data.tar.gz: f856e276eb76e6cf16f72039bca6ea138b5aae6038c11887f7b81d0128b6a606
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b15b022aab4015583b3c9b966b312c9dd3e0a4ecd184c498be19b566b2ef47eaace511ba83f65ecadc70b959fd2d685b1e9ee286bb64f4697016b7c1f5e1040a
|
7
|
+
data.tar.gz: e62eefc4e31cf53b3b0f7dff13e2909b49db206afea12b82122aae3f7d31037627776d92b8bbf546bb5911b38a0c80b73c5063375399b8b3b61abb519439ae7b
|
data/CHANGELOG.md
CHANGED
@@ -1,21 +1,34 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
|
3
|
+
## 0.48.0 (2021-08-01)
|
4
4
|
|
5
|
+
* Force to read file as utf-8
|
6
|
+
* Add logo
|
7
|
+
|
8
|
+
## 0.47.0 (2021-07-28)
|
9
|
+
|
10
|
+
* Add `to_single_quote` to `str` node
|
11
|
+
* Add `to_symbol` to `str` node
|
12
|
+
* Add `to_lambda_literal` to `lambda` node
|
13
|
+
|
14
|
+
## 0.46.0 (2021-07-25)
|
15
|
+
|
16
|
+
* Add `strip_curly_braces` and `wrap_curly_braces` for `hash` node
|
17
|
+
* Simplify symbol `match_value?`
|
5
18
|
* Unwrap quote when matching string value
|
6
19
|
|
7
|
-
|
20
|
+
## 0.45.0 (2021-07-22)
|
8
21
|
|
9
22
|
* Handle `nil` child node for `begin_pos` and `end_pos`
|
10
23
|
* Remove `Rewriter::Instance` options
|
11
24
|
|
12
|
-
|
25
|
+
## 0.44.0 (2021-07-19)
|
13
26
|
|
14
27
|
* Return rewrtier after executing snippet
|
15
28
|
* `left_value` and `right_value` support `or_asgn` node
|
16
29
|
* `child_node_range` supports send `parentheses`
|
17
30
|
|
18
|
-
|
31
|
+
## 0.42.0 (2021-07-11)
|
19
32
|
|
20
33
|
* Match string with quote
|
21
34
|
* `match_value?` returns true if actual and expected are the same
|
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
|

|
4
6
|
[](https://coveralls.io/r/xinminlabs/synvert-core)
|
5
7
|
[](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
|
@@ -490,6 +490,50 @@ module Parser::AST
|
|
490
490
|
end
|
491
491
|
end
|
492
492
|
|
493
|
+
# strip curly braces for hash
|
494
|
+
def strip_curly_braces
|
495
|
+
return to_source unless type == :hash
|
496
|
+
|
497
|
+
to_source.sub(/^{(.*)}$/) { Regexp.last_match(1).strip }
|
498
|
+
end
|
499
|
+
|
500
|
+
# wrap curly braces for hash
|
501
|
+
def wrap_curly_braces
|
502
|
+
return to_source unless type == :hash
|
503
|
+
|
504
|
+
"{ #{to_source} }"
|
505
|
+
end
|
506
|
+
|
507
|
+
# get single quote string
|
508
|
+
def to_single_quote
|
509
|
+
return to_source unless type == :str
|
510
|
+
|
511
|
+
"'#{to_value}'"
|
512
|
+
end
|
513
|
+
|
514
|
+
# convert string to symbol
|
515
|
+
def to_symbol
|
516
|
+
return to_source unless type == :str
|
517
|
+
|
518
|
+
":#{to_value}"
|
519
|
+
end
|
520
|
+
|
521
|
+
# convert lambda {} to -> {}
|
522
|
+
def to_lambda_literal
|
523
|
+
if type == :block && caller.type == :send && caller.receiver.nil? && caller.message == :lambda
|
524
|
+
new_source = to_source
|
525
|
+
if arguments.size > 1
|
526
|
+
new_source = new_source[0...arguments.loc.begin.to_range.begin - 1] + new_source[arguments.loc.end.to_range.end..-1]
|
527
|
+
new_source = new_source.sub('lambda', "->(#{arguments.map(&:to_source).join(', ')})")
|
528
|
+
else
|
529
|
+
new_source = new_source.sub('lambda', '->')
|
530
|
+
end
|
531
|
+
new_source
|
532
|
+
else
|
533
|
+
to_source
|
534
|
+
end
|
535
|
+
end
|
536
|
+
|
493
537
|
private
|
494
538
|
|
495
539
|
# Compare actual value with expected value.
|
@@ -504,7 +548,7 @@ module Parser::AST
|
|
504
548
|
case expected
|
505
549
|
when Symbol
|
506
550
|
if actual.is_a?(Parser::AST::Node)
|
507
|
-
actual.
|
551
|
+
actual.to_value == expected
|
508
552
|
else
|
509
553
|
actual.to_sym == expected
|
510
554
|
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
@@ -679,4 +679,91 @@ describe Parser::AST::Node do
|
|
679
679
|
EOS
|
680
680
|
end
|
681
681
|
end
|
682
|
+
|
683
|
+
describe '#strip_curly_braces' do
|
684
|
+
context 'hash node' do
|
685
|
+
it 'removes curly braces' do
|
686
|
+
node = parse("{ foo: 'bar' }")
|
687
|
+
expect(node.strip_curly_braces).to eq("foo: 'bar'")
|
688
|
+
end
|
689
|
+
end
|
690
|
+
|
691
|
+
context 'other node' do
|
692
|
+
it 'do nothing' do
|
693
|
+
node = parse("'foobar'")
|
694
|
+
expect(node.strip_curly_braces).to eq("'foobar'")
|
695
|
+
end
|
696
|
+
end
|
697
|
+
end
|
698
|
+
|
699
|
+
describe '#wrap_curly_braces' do
|
700
|
+
context 'hash node' do
|
701
|
+
it 'adds curly braces' do
|
702
|
+
node = parse("test(foo: 'bar')").arguments.first
|
703
|
+
expect(node.to_source).to eq("foo: 'bar'")
|
704
|
+
expect(node.wrap_curly_braces).to eq("{ foo: 'bar' }")
|
705
|
+
end
|
706
|
+
end
|
707
|
+
|
708
|
+
context 'other node' do
|
709
|
+
it 'does nothing' do
|
710
|
+
node = parse("'foobar'")
|
711
|
+
expect(node.wrap_curly_braces).to eq("'foobar'")
|
712
|
+
end
|
713
|
+
end
|
714
|
+
end
|
715
|
+
|
716
|
+
describe '#to_single_quote' do
|
717
|
+
context 'str node' do
|
718
|
+
it 'converts double quote to single quote' do
|
719
|
+
node = parse('"foobar"')
|
720
|
+
expect(node.to_source).to eq '"foobar"'
|
721
|
+
expect(node.to_single_quote).to eq "'foobar'"
|
722
|
+
end
|
723
|
+
end
|
724
|
+
|
725
|
+
context 'other node' do
|
726
|
+
it 'does nothing' do
|
727
|
+
node = parse(':foobar')
|
728
|
+
expect(node.to_single_quote).to eq ':foobar'
|
729
|
+
end
|
730
|
+
end
|
731
|
+
end
|
732
|
+
|
733
|
+
describe '#to_symbol' do
|
734
|
+
context 'str node' do
|
735
|
+
it 'converts string to symbol' do
|
736
|
+
node = parse("'foobar'")
|
737
|
+
expect(node.to_symbol).to eq ':foobar'
|
738
|
+
end
|
739
|
+
end
|
740
|
+
|
741
|
+
context 'other node' do
|
742
|
+
it 'does nothing' do
|
743
|
+
node = parse(':foobar')
|
744
|
+
expect(node.to_symbol).to eq ':foobar'
|
745
|
+
end
|
746
|
+
end
|
747
|
+
end
|
748
|
+
|
749
|
+
describe '#to_lambda_literal' do
|
750
|
+
context 'lambda node' do
|
751
|
+
it 'converts to lambda literal without arguments' do
|
752
|
+
node = parse('lambda { foobar }')
|
753
|
+
expect(node.to_lambda_literal).to eq('-> { foobar }')
|
754
|
+
end
|
755
|
+
|
756
|
+
it 'converts to lambda literal with arguments' do
|
757
|
+
node = parse('lambda { |x, y| foobar }')
|
758
|
+
expect(node.to_lambda_literal).to eq('->(x, y) { foobar }')
|
759
|
+
end
|
760
|
+
end
|
761
|
+
|
762
|
+
context 'other node' do
|
763
|
+
it 'does nothing' do
|
764
|
+
node = parse(':foobar')
|
765
|
+
expect(node.to_lambda_literal).to eq ':foobar'
|
766
|
+
end
|
767
|
+
end
|
768
|
+
end
|
682
769
|
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.48.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-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|