synvert-core 0.44.1 → 0.46.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 252c2e9347770f9363ca9d317d574fa6e1d9faad4826d2853816a669ad13dab1
4
- data.tar.gz: 1d19aac0f1dac17a84dd4a85bcbb8606d9391afeb9c4493eff516cf2b5ed948f
3
+ metadata.gz: 5251fd7b7ac886ba35aeb23c94e8d25bf2d26e2124db5d454d8994bdc5cecff3
4
+ data.tar.gz: 5b8395085acf698b7715a3cd48e3d22539308cc81e1731081c3b48dd00210ba7
5
5
  SHA512:
6
- metadata.gz: 795910a3044cc4cdbf02e0412a5784235b7c8fc0892a1243cf58677c53d09e1cdf097d7f56cf8f818864de0eab6ec65d829c59e2ba246e1d1150bdd3fd5e2b40
7
- data.tar.gz: fc2f006fd4b269f598cd23d05ebfcf72b098ce4a7ccfaa1a5d6004517d058caabea1005e999d2ccaf92a73bae34941b4bc8b37124db98ae90a71730c7e749098
6
+ metadata.gz: 65e5e5a87c8bbe7d5d824d216c02764042f4b585705887a1a1c69469e389273b8e5ccfc00c3af2dc1003b660d49751c6365ff176980a72f6d663bfc1df60c31b
7
+ data.tar.gz: 9c47b80c577c5595c665658bf824c0f0f635c7bc09ceee6b0e865c636b4445d28fd449087e8eededc51d9d34eb2ab9fcd19899af676a228850ae630e3c10a726
@@ -0,0 +1,32 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: CI
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.5', '2.6', '2.7', '3.0']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Set up Ruby
27
+ uses: ruby/setup-ruby@v1
28
+ with:
29
+ ruby-version: ${{ matrix.ruby-version }}
30
+ bundler-cache: true
31
+ - name: Run tests
32
+ run: bundle exec rake
data/CHANGELOG.md CHANGED
@@ -1,16 +1,23 @@
1
1
  # CHANGELOG
2
2
 
3
- # 0.44.1 (2021-07-19)
3
+ ## 0.46.0 (2021-07-25)
4
+
5
+ * Add `strip_curly_braces` and `wrap_curly_braces` for `hash` node
6
+ * Simplify symbol `match_value?`
7
+ * Unwrap quote when matching string value
8
+
9
+ ## 0.45.0 (2021-07-22)
4
10
 
5
11
  * Handle `nil` child node for `begin_pos` and `end_pos`
12
+ * Remove `Rewriter::Instance` options
6
13
 
7
- # 0.44.0 (2021-07-19)
14
+ ## 0.44.0 (2021-07-19)
8
15
 
9
16
  * Return rewrtier after executing snippet
10
17
  * `left_value` and `right_value` support `or_asgn` node
11
18
  * `child_node_range` supports send `parentheses`
12
19
 
13
- # 0.42.0 (2021-07-11)
20
+ ## 0.42.0 (2021-07-11)
14
21
 
15
22
  * Match string with quote
16
23
  * `match_value?` returns true if actual and expected are the same
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Synvert::Core
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/xinminlabs/synvert-core.png)](http://travis-ci.org/xinminlabs/synvert-core)
3
+ ![Main workflow](https://github.com/xinminlabs/synvert-core/actions/workflows/main.yml/badge.svg)
4
4
  [![Coverage Status](https://coveralls.io/repos/xinminlabs/synvert-core/badge.png?branch=master)](https://coveralls.io/r/xinminlabs/synvert-core)
5
5
  [![Gem Version](https://badge.fury.io/rb/synvert-core.png)](http://badge.fury.io/rb/synvert-core)
6
6
 
@@ -490,6 +490,20 @@ 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
+
493
507
  private
494
508
 
495
509
  # Compare actual value with expected value.
@@ -504,15 +518,14 @@ module Parser::AST
504
518
  case expected
505
519
  when Symbol
506
520
  if actual.is_a?(Parser::AST::Node)
507
- actual.to_source == ":#{expected}"
521
+ actual.to_value == expected
508
522
  else
509
523
  actual.to_sym == expected
510
524
  end
511
525
  when String
512
526
  if actual.is_a?(Parser::AST::Node)
513
- return true if (Parser::CurrentRuby.parse(expected) == actual rescue nil)
514
- actual.to_source == expected || (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) ||
515
- actual.to_source[1...-1] == expected
527
+ actual.to_source == expected || actual.to_value == expected ||
528
+ actual.to_source == unwrap_quote(expected) || actual.to_value == unwrap_quote(expected)
516
529
  else
517
530
  actual.to_s == expected || wrap_quote(actual.to_s) == expected
518
531
  end
@@ -591,5 +604,13 @@ module Parser::AST
591
604
  "'#{string}'"
592
605
  end
593
606
  end
607
+
608
+ def unwrap_quote(string)
609
+ if (string[0] == '"' && string[-1] == '"') || (string[0] == "'" && string[-1] == "'")
610
+ string[1...-1]
611
+ else
612
+ string
613
+ end
614
+ end
594
615
  end
595
616
  end
@@ -245,13 +245,12 @@ module Synvert::Core
245
245
  # It creates a [Synvert::Core::Rewriter::Instance] to rewrite code.
246
246
  #
247
247
  # @param file_pattern [String] pattern to find files, e.g. spec/**/*_spec.rb
248
- # @param options [Hash] instance options.
249
248
  # @param block [Block] the block to rewrite code in the matching files.
250
- def within_files(file_pattern, options = {}, &block)
249
+ def within_files(file_pattern, &block)
251
250
  return if @sandbox
252
251
 
253
252
  if (!@ruby_version || @ruby_version.match?) && (!@gem_spec || @gem_spec.match?)
254
- Rewriter::Instance.new(self, file_pattern, options, &block).process
253
+ Rewriter::Instance.new(self, file_pattern, &block).process
255
254
  end
256
255
  end
257
256
 
@@ -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
 
@@ -64,20 +64,16 @@ module Synvert::Core
64
64
  # @return current filename
65
65
  attr_accessor :current_node, :current_file
66
66
 
67
- DEFAULT_OPTIONS = { sort_by: 'begin_pos' }.freeze
68
-
69
67
  # Initialize an instance.
70
68
  #
71
69
  # @param rewriter [Synvert::Core::Rewriter]
72
70
  # @param file_pattern [String] pattern to find files, e.g. spec/**/*_spec.rb
73
- # @param options [Hash] instance options, it includes :sort_by.
74
71
  # @param block [Block] block code to find nodes, match conditions and rewrite code.
75
72
  # @return [Synvert::Core::Rewriter::Instance]
76
- def initialize(rewriter, file_pattern, options = {}, &block)
73
+ def initialize(rewriter, file_pattern, &block)
77
74
  @rewriter = rewriter
78
75
  @actions = []
79
76
  @file_pattern = file_pattern
80
- @options = DEFAULT_OPTIONS.merge(options)
81
77
  @block = block
82
78
  rewriter.helpers.each { |helper| singleton_class.send(:define_method, helper[:name], &helper[:block]) }
83
79
  end
@@ -107,7 +103,7 @@ module Synvert::Core
107
103
  end
108
104
 
109
105
  if @actions.length > 0
110
- @actions.sort_by! { |action| action.send(@options[:sort_by]) }
106
+ @actions.sort_by! { |action| [action.begin_pos, action.end_pos] }
111
107
  conflict_actions = get_conflict_actions
112
108
  @actions.reverse_each do |action|
113
109
  source[action.begin_pos...action.end_pos] = action.rewritten_code
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.44.1'
5
+ VERSION = '0.46.0'
6
6
  end
7
7
  end
@@ -679,4 +679,37 @@ 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 'do nothing' do
710
+ node = parse("'foobar'")
711
+ expect(node.wrap_curly_braces).to eq("'foobar'")
712
+ end
713
+ end
714
+ end
682
715
  end
@@ -4,19 +4,18 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter::GemSpec do
7
- let(:gemfile_lock_content) {
8
- '
9
- GEM
10
- remote: https://rubygems.org/
11
- specs:
12
- ast (1.1.0)
13
- parser (2.1.7)
14
- ast (~> 1.1)
15
- slop (~> 3.4, >= 3.4.5)
16
- rake (10.1.1)
17
- slop (3.4.7)
18
- '
19
- }
7
+ let(:gemfile_lock_content) { <<~EOS }
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (1.1.0)
12
+ parser (2.1.7)
13
+ ast (~> 1.1)
14
+ slop (~> 3.4, >= 3.4.5)
15
+ rake (10.1.1)
16
+ slop (3.4.7)
17
+ EOS
18
+ before { allow(File).to receive(:exist?).with(File.join(ENV['HOME'], '.gem/specs')).and_return(false) }
20
19
 
21
20
  it 'returns true if version in Gemfile.lock is greater than definition' do
22
21
  expect(File).to receive(:exist?).with('./Gemfile.lock').and_return(true)
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.44.1
4
+ version: 0.46.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-07-18 00:00:00.000000000 Z
11
+ date: 2021-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -129,9 +129,9 @@ executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
+ - ".github/workflows/main.yml"
132
133
  - ".gitignore"
133
134
  - ".rspec"
134
- - ".travis.yml"
135
135
  - CHANGELOG.md
136
136
  - Gemfile
137
137
  - Guardfile
data/.travis.yml DELETED
@@ -1,8 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.3
4
- - 2.4
5
- - 2.5
6
- - 2.6
7
- - 2.7
8
- - 3.0