synvert-core 0.62.0 → 0.63.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 +4 -4
- data/.github/workflows/main.yml +2 -2
- data/CHANGELOG.md +9 -0
- data/Gemfile +0 -2
- data/lib/synvert/core/node_ext.rb +18 -16
- data/lib/synvert/core/rewriter/action/insert_action.rb +4 -2
- data/lib/synvert/core/rewriter/gem_spec.rb +1 -1
- data/lib/synvert/core/rewriter/instance.rb +3 -2
- data/lib/synvert/core/version.rb +1 -1
- data/lib/synvert/core.rb +1 -0
- data/spec/spec_helper.rb +0 -3
- data/spec/synvert/core/node_ext_spec.rb +28 -0
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +21 -0
- data/spec/synvert/core/rewriter/gem_spec_spec.rb +10 -9
- data/spec/synvert/core/rewriter/instance_spec.rb +3 -3
- data/synvert-core-ruby.gemspec +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5739f84132a6ece2551028571b7e6cb458812524f51ea11de6175eb621592d21
|
4
|
+
data.tar.gz: a55b00a9431cacbe740405294b351cd8c5a1face1f2c20ba3a61897316dc7bd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba153f740ea4d281dde99359dcf864beb49bdac33db3833878eebb9ba020966bb8957426936e65f7fc00ce84b76f7bb79fe2c01a5984ea155c5a4f6845c6e5b7
|
7
|
+
data.tar.gz: 6ff6c82355e35e6f86a42f01bcbd57b4e25d6c0ccef0079b89d4980bca26a983bf187526f9f5af150ee7a970d906b3cb22dd69b1af9f84263d3d8231d4673df7
|
data/.github/workflows/main.yml
CHANGED
@@ -19,7 +19,7 @@ jobs:
|
|
19
19
|
runs-on: ubuntu-latest
|
20
20
|
strategy:
|
21
21
|
matrix:
|
22
|
-
ruby-version: ['2.5', '2.6', '2.7', '3.0']
|
22
|
+
ruby-version: ['2.5', '2.6', '2.7', '3.0', '3.1']
|
23
23
|
|
24
24
|
steps:
|
25
25
|
- uses: actions/checkout@v2
|
@@ -29,4 +29,4 @@ jobs:
|
|
29
29
|
ruby-version: ${{ matrix.ruby-version }}
|
30
30
|
bundler-cache: true
|
31
31
|
- name: Run tests
|
32
|
-
run: bundle exec rake
|
32
|
+
run: bundle exec rake
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -487,27 +487,29 @@ module Parser::AST
|
|
487
487
|
# @param rules [Hash] rules to match.
|
488
488
|
# @return true if matches.
|
489
489
|
def match?(rules)
|
490
|
+
keywords = %i[any contain not in not_in gt gte lt lte]
|
490
491
|
flat_hash(rules).keys.all? do |multi_keys|
|
491
|
-
|
492
|
+
last_key = multi_keys.last
|
493
|
+
actual = keywords.include?(last_key) ? actual_value(multi_keys[0...-1]) : actual_value(multi_keys)
|
494
|
+
expected = expected_value(rules, multi_keys)
|
495
|
+
case last_key
|
492
496
|
when :any, :contain
|
493
|
-
|
494
|
-
expected = expected_value(rules, multi_keys)
|
495
|
-
actual_values.any? { |actual| match_value?(actual, expected) }
|
497
|
+
actual.any? { |actual_value| match_value?(actual_value, expected) }
|
496
498
|
when :not
|
497
|
-
actual = actual_value(self, multi_keys[0...-1])
|
498
|
-
expected = expected_value(rules, multi_keys)
|
499
499
|
!match_value?(actual, expected)
|
500
500
|
when :in
|
501
|
-
|
502
|
-
expected_values = expected_value(rules, multi_keys)
|
503
|
-
expected_values.any? { |expected| match_value?(actual, expected) }
|
501
|
+
expected.any? { |expected_value| match_value?(actual, expected_value) }
|
504
502
|
when :not_in
|
505
|
-
|
506
|
-
|
507
|
-
|
503
|
+
expected.all? { |expected_value| !match_value?(actual, expected_value) }
|
504
|
+
when :gt
|
505
|
+
actual > expected
|
506
|
+
when :gte
|
507
|
+
actual >= expected
|
508
|
+
when :lt
|
509
|
+
actual < expected
|
510
|
+
when :lte
|
511
|
+
actual <= expected
|
508
512
|
else
|
509
|
-
actual = actual_value(self, multi_keys)
|
510
|
-
expected = expected_value(rules, multi_keys)
|
511
513
|
match_value?(actual, expected)
|
512
514
|
end
|
513
515
|
end
|
@@ -696,8 +698,8 @@ module Parser::AST
|
|
696
698
|
# @param node [Parser::AST::Node]
|
697
699
|
# @param multi_keys [Array<Symbol>]
|
698
700
|
# @return [Object] actual value.
|
699
|
-
def actual_value(
|
700
|
-
multi_keys.inject(
|
701
|
+
def actual_value(multi_keys)
|
702
|
+
multi_keys.inject(self) { |n, key| n.send(key) if n }
|
701
703
|
end
|
702
704
|
|
703
705
|
# Get expected value from rules.
|
@@ -3,13 +3,15 @@
|
|
3
3
|
module Synvert::Core
|
4
4
|
# InsertAction to add code to the node.
|
5
5
|
class Rewriter::InsertAction < Rewriter::Action
|
6
|
-
def initialize(instance, code, at:)
|
6
|
+
def initialize(instance, code, at:, to: nil)
|
7
7
|
super(instance, code)
|
8
8
|
@at = at
|
9
|
+
@to = to
|
9
10
|
end
|
10
11
|
|
11
12
|
def calculate_position
|
12
|
-
|
13
|
+
node_range = @to ? @node.child_node_range(@to) : @node.loc.expression
|
14
|
+
@begin_pos = @at == 'end' ? node_range.end_pos : node_range.begin_pos
|
13
15
|
@end_pos = @begin_pos
|
14
16
|
end
|
15
17
|
|
@@ -19,7 +19,7 @@ module Synvert::Core
|
|
19
19
|
# @return [Boolean] true if matches, otherwise false.
|
20
20
|
# @raise [Synvert::Core::GemfileLockNotFound] raise if Gemfile.lock does not exist.
|
21
21
|
def match?
|
22
|
-
gemfile_lock_path = File.join(Configuration.path, 'Gemfile.lock')
|
22
|
+
gemfile_lock_path = File.expand_path(File.join(Configuration.path, 'Gemfile.lock'))
|
23
23
|
|
24
24
|
# if Gemfile.lock does not exist, just ignore this check
|
25
25
|
return true unless File.exist?(gemfile_lock_path)
|
@@ -240,8 +240,9 @@ module Synvert::Core
|
|
240
240
|
#
|
241
241
|
# @param code [String] code need to be inserted.
|
242
242
|
# @param at [String] insert position, beginning or end, end is the default.
|
243
|
-
|
244
|
-
|
243
|
+
# @param to [String] where to insert, if it is nil, will insert to current node.
|
244
|
+
def insert(code, at: 'end', to: nil)
|
245
|
+
@actions << Rewriter::InsertAction.new(self, code, at: at, to: to).process
|
245
246
|
end
|
246
247
|
|
247
248
|
# Parse insert_after dsl, it creates a [Synvert::Core::Rewriter::InsertAfterAction] to
|
data/lib/synvert/core/version.rb
CHANGED
data/lib/synvert/core.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -567,6 +567,34 @@ describe Parser::AST::Node do
|
|
567
567
|
node = parse(source)
|
568
568
|
expect(node).not_to be_match(type: 'send', message: { not_in: %i[create build] })
|
569
569
|
end
|
570
|
+
|
571
|
+
it 'matches gt' do
|
572
|
+
source = 'foobar(foo, bar)'
|
573
|
+
node = parse(source)
|
574
|
+
expect(node).to be_match(type: 'send', arguments: { size: { gt: 1 } })
|
575
|
+
expect(node).not_to be_match(type: 'send', arguments: { size: { gt: 2 } })
|
576
|
+
end
|
577
|
+
|
578
|
+
it 'matches gte' do
|
579
|
+
source = 'foobar(foo, bar)'
|
580
|
+
node = parse(source)
|
581
|
+
expect(node).to be_match(type: 'send', arguments: { size: { gte: 2 } })
|
582
|
+
expect(node).not_to be_match(type: 'send', arguments: { size: { gte: 3 } })
|
583
|
+
end
|
584
|
+
|
585
|
+
it 'matches lt' do
|
586
|
+
source = 'foobar(foo, bar)'
|
587
|
+
node = parse(source)
|
588
|
+
expect(node).to be_match(type: 'send', arguments: { size: { lt: 3 } })
|
589
|
+
expect(node).not_to be_match(type: 'send', arguments: { size: { lt: 2 } })
|
590
|
+
end
|
591
|
+
|
592
|
+
it 'matches lte' do
|
593
|
+
source = 'foobar(foo, bar)'
|
594
|
+
node = parse(source)
|
595
|
+
expect(node).to be_match(type: 'send', arguments: { size: { lte: 2 } })
|
596
|
+
expect(node).not_to be_match(type: 'send', arguments: { size: { lte: 1 } })
|
597
|
+
end
|
570
598
|
end
|
571
599
|
|
572
600
|
describe '#child_node_by_name' do
|
@@ -45,5 +45,26 @@ module Synvert::Core
|
|
45
45
|
expect(subject.rewritten_code).to eq 'URI.'
|
46
46
|
end
|
47
47
|
end
|
48
|
+
|
49
|
+
context 'to receiver' do
|
50
|
+
subject {
|
51
|
+
source = "User.where(username: 'Richard')"
|
52
|
+
node = Parser::CurrentRuby.parse(source)
|
53
|
+
instance = double(current_node: node)
|
54
|
+
Rewriter::InsertAction.new(instance, '.active', to: 'receiver', at: 'end').process
|
55
|
+
}
|
56
|
+
|
57
|
+
it 'gets begin_pos' do
|
58
|
+
expect(subject.begin_pos).to eq "User".length
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'gets end_pos' do
|
62
|
+
expect(subject.end_pos).to eq "User".length
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'gets rewritten_code' do
|
66
|
+
expect(subject.rewritten_code).to eq '.active'
|
67
|
+
end
|
68
|
+
end
|
48
69
|
end
|
49
70
|
end
|
@@ -15,38 +15,39 @@ module Synvert::Core
|
|
15
15
|
rake (10.1.1)
|
16
16
|
slop (3.4.7)
|
17
17
|
EOS
|
18
|
+
let(:lock_path) { File.absolute_path('./Gemfile.lock') }
|
18
19
|
before { allow(File).to receive(:exist?).with(File.join(ENV['HOME'], '.gem/specs')).and_return(false) }
|
19
20
|
|
20
21
|
it 'returns true if version in Gemfile.lock is greater than definition' do
|
21
|
-
expect(File).to receive(:exist?).with(
|
22
|
-
expect(File).to receive(:read).with(
|
22
|
+
expect(File).to receive(:exist?).with(lock_path).and_return(true)
|
23
|
+
expect(File).to receive(:read).with(lock_path).and_return(gemfile_lock_content)
|
23
24
|
gem_spec = Rewriter::GemSpec.new('ast', '~> 1.1')
|
24
25
|
expect(gem_spec).to be_match
|
25
26
|
end
|
26
27
|
|
27
28
|
it 'returns true if version in Gemfile.lock is equal to definition' do
|
28
|
-
expect(File).to receive(:exist?).with(
|
29
|
-
expect(File).to receive(:read).with(
|
29
|
+
expect(File).to receive(:exist?).with(lock_path).and_return(true)
|
30
|
+
expect(File).to receive(:read).with(lock_path).and_return(gemfile_lock_content)
|
30
31
|
gem_spec = Rewriter::GemSpec.new('ast', '1.1.0')
|
31
32
|
expect(gem_spec).to be_match
|
32
33
|
end
|
33
34
|
|
34
35
|
it 'returns false if version in Gemfile.lock is less than definition' do
|
35
|
-
expect(File).to receive(:exist?).with(
|
36
|
-
expect(File).to receive(:read).with(
|
36
|
+
expect(File).to receive(:exist?).with(lock_path).and_return(true)
|
37
|
+
expect(File).to receive(:read).with(lock_path).and_return(gemfile_lock_content)
|
37
38
|
gem_spec = Rewriter::GemSpec.new('ast', '> 1.2.0')
|
38
39
|
expect(gem_spec).not_to be_match
|
39
40
|
end
|
40
41
|
|
41
42
|
it 'returns false if gem does not exist in Gemfile.lock' do
|
42
|
-
expect(File).to receive(:exist?).with(
|
43
|
-
expect(File).to receive(:read).with(
|
43
|
+
expect(File).to receive(:exist?).with(lock_path).and_return(true)
|
44
|
+
expect(File).to receive(:read).with(lock_path).and_return(gemfile_lock_content)
|
44
45
|
gem_spec = Rewriter::GemSpec.new('synvert', '1.0.0')
|
45
46
|
expect(gem_spec).not_to be_match
|
46
47
|
end
|
47
48
|
|
48
49
|
it 'raise Synvert::Core::GemfileLockNotFound if Gemfile.lock does not exist' do
|
49
|
-
expect(File).to receive(:exist?).with(
|
50
|
+
expect(File).to receive(:exist?).with(lock_path).and_return(false)
|
50
51
|
gem_spec = Rewriter::GemSpec.new('ast', '1.1.0')
|
51
52
|
expect(gem_spec).to be_match
|
52
53
|
end
|
@@ -118,14 +118,14 @@ module Synvert::Core
|
|
118
118
|
|
119
119
|
it 'parses insert at end' do
|
120
120
|
action = double
|
121
|
-
expect(Rewriter::InsertAction).to receive(:new).with(instance, '.first', at: 'end').and_return(action)
|
121
|
+
expect(Rewriter::InsertAction).to receive(:new).with(instance, '.first', at: 'end', to: 'receiver').and_return(action)
|
122
122
|
expect(action).to receive(:process)
|
123
|
-
instance.insert '.first'
|
123
|
+
instance.insert '.first', to: 'receiver'
|
124
124
|
end
|
125
125
|
|
126
126
|
it 'parses insert at beginning' do
|
127
127
|
action = double
|
128
|
-
expect(Rewriter::InsertAction).to receive(:new).with(instance, 'URI.', at: 'beginning').and_return(action)
|
128
|
+
expect(Rewriter::InsertAction).to receive(:new).with(instance, 'URI.', at: 'beginning', to: nil).and_return(action)
|
129
129
|
expect(action).to receive(:process)
|
130
130
|
instance.insert 'URI.', at: 'beginning'
|
131
131
|
end
|
data/synvert-core-ruby.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_runtime_dependency "activesupport"
|
22
|
+
spec.add_runtime_dependency "activesupport"
|
23
23
|
spec.add_runtime_dependency "erubis"
|
24
24
|
spec.add_runtime_dependency "parser"
|
25
25
|
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: synvert-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.63.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:
|
11
|
+
date: 2022-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: erubis
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -218,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
218
218
|
- !ruby/object:Gem::Version
|
219
219
|
version: '0'
|
220
220
|
requirements: []
|
221
|
-
rubygems_version: 3.
|
221
|
+
rubygems_version: 3.3.7
|
222
222
|
signing_key:
|
223
223
|
specification_version: 4
|
224
224
|
summary: convert ruby code to better syntax.
|