synvert-core 0.42.1 → 0.45.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +32 -0
- data/CHANGELOG.md +12 -1
- data/README.md +1 -1
- data/lib/synvert/core/node_ext.rb +17 -4
- data/lib/synvert/core/rewriter.rb +2 -3
- data/lib/synvert/core/rewriter/action/delete_action.rb +2 -2
- data/lib/synvert/core/rewriter/instance.rb +2 -6
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +24 -0
- data/spec/synvert/core/rewriter/gem_spec_spec.rb +12 -13
- metadata +3 -3
- data/.travis.yml +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35318f19e568f0e25bce6e982efd115aacb5fe2857fda56406839e3b4693253b
|
4
|
+
data.tar.gz: 0a89e0f6376e41c13a5dae28a2bbeeee53ecb9c1e9b8e00e20ae0a082ed3c0bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de01431c5662dd61f698a537e68f74de2d6a19b63ecc7465fd81d6496fae158859446bc5d42fa1f432e559aeccb7c27a28d0f6b261f1195b68a97601260d0aa0
|
7
|
+
data.tar.gz: ee623d625ee63212b64f752cb14b0214ff9d58c01c9c7fd23b51991b9fbfcca502ae0a96e633347bfa9486af3c4bfb0b424bdcaba8cf401c44fcdeea31caa4c4
|
@@ -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,8 +1,19 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
# 0.
|
3
|
+
# 0.45.1 (2021-07-24)
|
4
|
+
|
5
|
+
* Unwrap quote when matching string value
|
6
|
+
|
7
|
+
# 0.45.0 (2021-07-22)
|
8
|
+
|
9
|
+
* Handle `nil` child node for `begin_pos` and `end_pos`
|
10
|
+
* Remove `Rewriter::Instance` options
|
11
|
+
|
12
|
+
# 0.44.0 (2021-07-19)
|
4
13
|
|
5
14
|
* Return rewrtier after executing snippet
|
15
|
+
* `left_value` and `right_value` support `or_asgn` node
|
16
|
+
* `child_node_range` supports send `parentheses`
|
6
17
|
|
7
18
|
# 0.42.0 (2021-07-11)
|
8
19
|
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Synvert::Core
|
2
2
|
|
3
|
-
|
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
|
|
@@ -217,6 +217,8 @@ module Parser::AST
|
|
217
217
|
def left_value
|
218
218
|
if %i[masgn lvasgn ivasgn].include? type
|
219
219
|
children[0]
|
220
|
+
elsif :or_asgn == type
|
221
|
+
children[0].children[0]
|
220
222
|
else
|
221
223
|
raise Synvert::Core::MethodNotSupported, "left_value is not handled for #{debug_info}"
|
222
224
|
end
|
@@ -227,7 +229,7 @@ module Parser::AST
|
|
227
229
|
# @return [Array<Parser::AST::Node>] variable nodes.
|
228
230
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
229
231
|
def right_value
|
230
|
-
if %i[masgn lvasgn ivasgn].include? type
|
232
|
+
if %i[masgn lvasgn ivasgn or_asgn].include? type
|
231
233
|
children[1]
|
232
234
|
else
|
233
235
|
raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{debug_info}"
|
@@ -360,6 +362,10 @@ module Parser::AST
|
|
360
362
|
else
|
361
363
|
loc.selector
|
362
364
|
end
|
365
|
+
when %i[send parentheses]
|
366
|
+
if loc.begin && loc.end
|
367
|
+
Parser::Source::Range.new('(string)', loc.begin.begin_pos, loc.end.end_pos)
|
368
|
+
end
|
363
369
|
else
|
364
370
|
if respond_to?(child_name)
|
365
371
|
child_node = send(child_name)
|
@@ -504,9 +510,8 @@ module Parser::AST
|
|
504
510
|
end
|
505
511
|
when String
|
506
512
|
if actual.is_a?(Parser::AST::Node)
|
507
|
-
|
508
|
-
|
509
|
-
actual.to_source[1...-1] == expected
|
513
|
+
actual.to_source == expected || actual.to_value == expected ||
|
514
|
+
actual.to_source == unwrap_quote(expected) || actual.to_value == unwrap_quote(expected)
|
510
515
|
else
|
511
516
|
actual.to_s == expected || wrap_quote(actual.to_s) == expected
|
512
517
|
end
|
@@ -585,5 +590,13 @@ module Parser::AST
|
|
585
590
|
"'#{string}'"
|
586
591
|
end
|
587
592
|
end
|
593
|
+
|
594
|
+
def unwrap_quote(string)
|
595
|
+
if (string[0] == '"' && string[-1] == '"') || (string[0] == "'" && string[-1] == "'")
|
596
|
+
string[1...-1]
|
597
|
+
else
|
598
|
+
string
|
599
|
+
end
|
600
|
+
end
|
588
601
|
end
|
589
602
|
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,
|
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,
|
253
|
+
Rewriter::Instance.new(self, file_pattern, &block).process
|
255
254
|
end
|
256
255
|
end
|
257
256
|
|
@@ -12,14 +12,14 @@ module Synvert::Core
|
|
12
12
|
#
|
13
13
|
# @return [Integer] begin position.
|
14
14
|
def begin_pos
|
15
|
-
@selectors.map { |selector| @node.child_node_range(selector)
|
15
|
+
@selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:begin_pos).min
|
16
16
|
end
|
17
17
|
|
18
18
|
# End position of code to replace.
|
19
19
|
#
|
20
20
|
# @return [Integer] end position.
|
21
21
|
def end_pos
|
22
|
-
@selectors.map { |selector| @node.child_node_range(selector)
|
22
|
+
@selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:end_pos).max
|
23
23
|
end
|
24
24
|
|
25
25
|
# The rewritten code, always empty string.
|
@@ -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,
|
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.
|
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
|
data/lib/synvert/core/version.rb
CHANGED
@@ -261,6 +261,11 @@ describe Parser::AST::Node do
|
|
261
261
|
node = parse('@a = 1')
|
262
262
|
expect(node.left_value).to eq :@a
|
263
263
|
end
|
264
|
+
|
265
|
+
it 'gets for or_asgn' do
|
266
|
+
node = parse('a ||= 1')
|
267
|
+
expect(node.left_value).to eq :a
|
268
|
+
end
|
264
269
|
end
|
265
270
|
|
266
271
|
describe '#right_value' do
|
@@ -283,6 +288,11 @@ describe Parser::AST::Node do
|
|
283
288
|
node = parse('@a = 1')
|
284
289
|
expect(node.right_value).to eq parse('1')
|
285
290
|
end
|
291
|
+
|
292
|
+
it 'gets for or_asgn' do
|
293
|
+
node = parse('a ||= 1')
|
294
|
+
expect(node.right_value).to eq parse('1')
|
295
|
+
end
|
286
296
|
end
|
287
297
|
|
288
298
|
describe '#to_value' do
|
@@ -614,6 +624,20 @@ describe Parser::AST::Node do
|
|
614
624
|
range = node.child_node_range(:arguments)
|
615
625
|
expect(range).to be_nil
|
616
626
|
end
|
627
|
+
|
628
|
+
it 'checks parentheses' do
|
629
|
+
node = parse('foo.bar(test)')
|
630
|
+
range = node.child_node_range(:parentheses)
|
631
|
+
expect(range.to_range).to eq(7...13)
|
632
|
+
|
633
|
+
node = parse('foobar(test)')
|
634
|
+
range = node.child_node_range(:parentheses)
|
635
|
+
expect(range.to_range).to eq(6...12)
|
636
|
+
|
637
|
+
node = parse('foo.bar')
|
638
|
+
range = node.child_node_range(:parentheses)
|
639
|
+
expect(range).to be_nil
|
640
|
+
end
|
617
641
|
end
|
618
642
|
end
|
619
643
|
|
@@ -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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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.
|
4
|
+
version: 0.45.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-07-
|
11
|
+
date: 2021-07-23 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
|