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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e6217815771ca45d3fc81030b42ae126252ab800e127518e72c2f2b4dd2eb5c
4
- data.tar.gz: 266cc5cda364f6df8100a290d393f879075b433acfd7ee70c780b37d23a83292
3
+ metadata.gz: 5739f84132a6ece2551028571b7e6cb458812524f51ea11de6175eb621592d21
4
+ data.tar.gz: a55b00a9431cacbe740405294b351cd8c5a1face1f2c20ba3a61897316dc7bd4
5
5
  SHA512:
6
- metadata.gz: 89c3f93da22298305839c57fadb4963ea0973e16de10b2d2260c76e63927d25c302bbcd2c74515e79adeb24d2440ecba53025aaeeab0b173c6176adccb5d0875
7
- data.tar.gz: df7a75da7a8bbecee35d4cc1afb862f6aa0184e96d484a6f207a88510dfac70098ea17cca794aefaac0938e37e96d8a91f7ba21738f2f116554238f02344e4c1
6
+ metadata.gz: ba153f740ea4d281dde99359dcf864beb49bdac33db3833878eebb9ba020966bb8957426936e65f7fc00ce84b76f7bb79fe2c01a5984ea155c5a4f6845c6e5b7
7
+ data.tar.gz: 6ff6c82355e35e6f86a42f01bcbd57b4e25d6c0ccef0079b89d4980bca26a983bf187526f9f5af150ee7a970d906b3cb22dd69b1af9f84263d3d8231d4673df7
@@ -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
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.63.1 (2022-02-26)
4
+
5
+ * Read absolute path of Gemfile.lock
6
+
7
+ ## 0.63.0 (2022-02-26)
8
+
9
+ * Add `to` option to `InsertAction`
10
+ * Add `gt`, `gte`, `lt` and `lte` rules
11
+
3
12
  ## 0.62.0 (2021-12-24)
4
13
 
5
14
  * Support `csend` node
data/Gemfile CHANGED
@@ -4,5 +4,3 @@ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in synvert.gemspec
6
6
  gemspec
7
-
8
- gem 'coveralls', require: false
@@ -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
- case multi_keys.last
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
- actual_values = actual_value(self, multi_keys[0...-1])
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
- actual = actual_value(self, multi_keys[0...-1])
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
- actual = actual_value(self, multi_keys[0...-1])
506
- expected_values = expected_value(rules, multi_keys)
507
- expected_values.all? { |expected| !match_value?(actual, expected) }
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(node, multi_keys)
700
- multi_keys.inject(node) { |n, key| n.send(key) if n }
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
- @begin_pos = @at == 'end' ? @node.loc.expression.end_pos : @node.loc.expression.begin_pos
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
- def insert(code, at: 'end')
244
- @actions << Rewriter::InsertAction.new(self, code, at: at).process
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.62.0'
5
+ VERSION = '0.63.1'
6
6
  end
7
7
  end
data/lib/synvert/core.rb CHANGED
@@ -5,6 +5,7 @@ require 'bundler'
5
5
  require 'parser'
6
6
  require 'parser/current'
7
7
  require 'ast'
8
+ require 'active_support'
8
9
  require 'active_support/core_ext/object'
9
10
  require 'active_support/core_ext/array'
10
11
  require 'erubis'
data/spec/spec_helper.rb CHANGED
@@ -4,9 +4,6 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
5
  require 'synvert/core'
6
6
 
7
- require 'coveralls'
8
- Coveralls.wear!
9
-
10
7
  Dir[File.join(File.dirname(__FILE__), 'support', '*')].each do |path|
11
8
  require path
12
9
  end
@@ -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('./Gemfile.lock').and_return(true)
22
- expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
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('./Gemfile.lock').and_return(true)
29
- expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
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('./Gemfile.lock').and_return(true)
36
- expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
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('./Gemfile.lock').and_return(true)
43
- expect(File).to receive(:read).with('./Gemfile.lock').and_return(gemfile_lock_content)
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('./Gemfile.lock').and_return(false)
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
@@ -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", "~> 6"
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.62.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: 2021-12-24 00:00:00.000000000 Z
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: '6'
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: '6'
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.2.22
221
+ rubygems_version: 3.3.7
222
222
  signing_key:
223
223
  specification_version: 4
224
224
  summary: convert ruby code to better syntax.