synvert-core 0.54.2 → 0.56.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -3
- data/lib/synvert/core/configuration.rb +5 -1
- data/lib/synvert/core/node_ext.rb +1 -1
- data/lib/synvert/core/rewriter/action/delete_action.rb +2 -1
- data/lib/synvert/core/rewriter/action/remove_action.rb +7 -7
- data/lib/synvert/core/rewriter/action.rb +26 -0
- data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/instance.rb +2 -1
- data/lib/synvert/core/rewriter/ruby_version.rb +10 -2
- data/lib/synvert/core/rewriter.rb +1 -1
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +15 -0
- data/spec/synvert/core/rewriter/action/delete_action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/ruby_version_spec.rb +5 -0
- data/spec/synvert/core/rewriter_spec.rb +4 -2
- 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: e60611e972f774c97a102f5db6bc171b024cb48aa4dcc94dd07e9de6d25506a2
|
4
|
+
data.tar.gz: 2a7f4da54b1ed5caeb7fea49dd07ca1cc9fad1aceef082d223e420b928825bb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2fa37ef33a1ec38afbf094715b239c46e35e92aa386f5fae635be4419da964a8543f07df36c4321408b9a5d80a9a252d68674ccb9040865191d0299d9775667
|
7
|
+
data.tar.gz: a31385980557ef6c6031365f1554c5e957e6933b3aaaa65d902011dc3920599c8d24ae80bde55227f36643ebcc6808ee55ff2f028ca4c92cab94eeb04e606413
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,20 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.56.1 (2021-09-19)
|
4
4
|
|
5
|
-
*
|
5
|
+
* Compare ruby version in `.ruby-version` or `.rvmrc`
|
6
|
+
* Abstract `squeeze_spaces` and `squeeze_lines`
|
7
|
+
|
8
|
+
## 0.56.0 (2021-09-14)
|
6
9
|
|
7
|
-
|
10
|
+
* Support `name` for `:lvar`, `:ivar`, `:cvar`
|
11
|
+
* Delete one more space if two spaces left
|
8
12
|
|
13
|
+
## 0.55.0 (2021-09-11)
|
14
|
+
|
15
|
+
* Add `Configuration.show_run_process`
|
16
|
+
* Fix remove action `begin_pos` and `end_pos`
|
17
|
+
* Fix `nil` match
|
9
18
|
* Rewrite `remove` action
|
10
19
|
|
11
20
|
## 0.54.0 (2021-08-28)
|
@@ -4,7 +4,7 @@ module Synvert::Core
|
|
4
4
|
# Synvert global configuration.
|
5
5
|
class Configuration
|
6
6
|
class << self
|
7
|
-
attr_writer :path, :skip_files
|
7
|
+
attr_writer :path, :skip_files, :show_run_process
|
8
8
|
|
9
9
|
def path
|
10
10
|
@path || '.'
|
@@ -13,6 +13,10 @@ module Synvert::Core
|
|
13
13
|
def skip_files
|
14
14
|
@skip_files || []
|
15
15
|
end
|
16
|
+
|
17
|
+
def show_run_process
|
18
|
+
@show_run_process || false
|
19
|
+
end
|
16
20
|
end
|
17
21
|
end
|
18
22
|
end
|
@@ -9,7 +9,7 @@ module Parser::AST
|
|
9
9
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
10
10
|
def name
|
11
11
|
case type
|
12
|
-
when :class, :module, :def, :arg, :blockarg, :restarg
|
12
|
+
when :class, :module, :def, :arg, :blockarg, :restarg, :lvar, :ivar, :cvar
|
13
13
|
children[0]
|
14
14
|
when :defs, :const
|
15
15
|
children[1]
|
@@ -12,7 +12,8 @@ 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) }.compact.map(&:begin_pos).min
|
15
|
+
pos = @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:begin_pos).min
|
16
|
+
squeeze_spaces(pos, end_pos)
|
16
17
|
end
|
17
18
|
|
18
19
|
# End position of code to delete.
|
@@ -14,7 +14,8 @@ module Synvert::Core
|
|
14
14
|
if take_whole_line?
|
15
15
|
start_index
|
16
16
|
else
|
17
|
-
@node.loc.expression.begin_pos
|
17
|
+
pos = @node.loc.expression.begin_pos
|
18
|
+
squeeze_spaces(pos, end_pos)
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
@@ -41,15 +42,14 @@ module Synvert::Core
|
|
41
42
|
end
|
42
43
|
|
43
44
|
def start_index
|
44
|
-
file_source[0..@node.loc.expression.begin_pos].rindex("\n")
|
45
|
+
index = file_source[0..@node.loc.expression.begin_pos].rindex("\n")
|
46
|
+
index ? index + "\n".length : @node.loc.expression.begin_pos
|
45
47
|
end
|
46
48
|
|
47
49
|
def end_index
|
48
|
-
file_source[@node.loc.expression.end_pos..-1].index("\n")
|
49
|
-
|
50
|
-
|
51
|
-
def file_source
|
52
|
-
@file_source ||= @instance.file_source
|
50
|
+
index = file_source[@node.loc.expression.end_pos..-1].index("\n")
|
51
|
+
pos = index ? @node.loc.expression.end_pos + index + "\n".length : @node.loc.expression.end_pos
|
52
|
+
squeeze_lines(pos, @node.loc.expression.first_line, @node.loc.expression.last_line)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
@@ -33,11 +33,37 @@ module Synvert::Core
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
protected
|
37
|
+
|
36
38
|
# The rewritten source code.
|
37
39
|
#
|
38
40
|
# @return [String] rewritten source code.
|
39
41
|
def rewritten_source
|
40
42
|
@rewritten_source ||= @node.rewritten_source(@code)
|
41
43
|
end
|
44
|
+
|
45
|
+
def squeeze_spaces(begin_pos, end_pos)
|
46
|
+
if file_source[begin_pos - 1] == ' ' && file_source[end_pos] == ' '
|
47
|
+
begin_pos - 1
|
48
|
+
else
|
49
|
+
begin_pos
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def squeeze_lines(end_pos, begin_line, end_line)
|
54
|
+
lines = file_source.split("\n")
|
55
|
+
before_line_is_blank = begin_line == 1 || lines[begin_line - 2] == ''
|
56
|
+
after_line_is_blank = lines[end_line] == ''
|
57
|
+
|
58
|
+
if before_line_is_blank && after_line_is_blank
|
59
|
+
end_pos + "\n".length
|
60
|
+
else
|
61
|
+
end_pos
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def file_source
|
66
|
+
@file_source ||= @instance.file_source
|
67
|
+
end
|
42
68
|
end
|
43
69
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
|
-
#
|
4
|
+
# IfOnlyExistCondition checks if node has only one child node and the child node matches rules.
|
5
5
|
class Rewriter::IfOnlyExistCondition < Rewriter::Condition
|
6
6
|
# check if only have one child node and the child node matches rules.
|
7
7
|
def match?
|
@@ -46,7 +46,7 @@ module Synvert::Core
|
|
46
46
|
# @param source [String] file source
|
47
47
|
def write_file(file_path, source)
|
48
48
|
source = Engine::ERB.decode(source) if /\.erb/.match?(file_path)
|
49
|
-
File.write
|
49
|
+
File.write(file_path, source.gsub(/ +\n/, "\n"))
|
50
50
|
@file_source[file_path] = nil
|
51
51
|
@file_ast[file_path] = nil
|
52
52
|
end
|
@@ -92,6 +92,7 @@ module Synvert::Core
|
|
92
92
|
next if Configuration.skip_files.include? file_path
|
93
93
|
|
94
94
|
begin
|
95
|
+
puts file_path if Configuration.show_run_process
|
95
96
|
conflict_actions = []
|
96
97
|
source = +self.class.file_source(file_path)
|
97
98
|
ast = self.class.file_ast(file_path)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
|
-
# GemSpec checks and compares
|
4
|
+
# GemSpec checks and compares ruby version.
|
5
5
|
class Rewriter::RubyVersion
|
6
6
|
attr_reader :version
|
7
7
|
|
@@ -16,7 +16,15 @@ module Synvert::Core
|
|
16
16
|
#
|
17
17
|
# @return [Boolean] true if matches, otherwise false.
|
18
18
|
def match?
|
19
|
-
|
19
|
+
if File.exist?(File.join(Configuration.path, '.ruby-version'))
|
20
|
+
versionFile = '.ruby-version'
|
21
|
+
elsif File.exist?(File.join(Configuration.path, '.rvmrc'))
|
22
|
+
versionFile = '.rvmrc'
|
23
|
+
end
|
24
|
+
return true if !versionFile
|
25
|
+
|
26
|
+
version = File.read(File.join(Configuration.path, versionFile))
|
27
|
+
Gem::Version.new(version) >= Gem::Version.new(@version)
|
20
28
|
end
|
21
29
|
end
|
22
30
|
end
|
@@ -213,7 +213,7 @@ module Synvert::Core
|
|
213
213
|
end
|
214
214
|
end
|
215
215
|
|
216
|
-
# Parse if_ruby
|
216
|
+
# Parse if_ruby dsl, it checks if ruby version if greater than or equal to the specified ruby version.
|
217
217
|
#
|
218
218
|
# @param version, [String] specified ruby version.
|
219
219
|
def if_ruby(version)
|
data/lib/synvert/core/version.rb
CHANGED
@@ -42,6 +42,21 @@ describe Parser::AST::Node do
|
|
42
42
|
expect(node.name).to eq :Synvert
|
43
43
|
end
|
44
44
|
|
45
|
+
it 'gets for lvar node' do
|
46
|
+
node = parse("foo = 'bar'\nfoo").children[1]
|
47
|
+
expect(node.name).to eq :foo
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'gets for ivar node' do
|
51
|
+
node = parse("@foo = 'bar'\n@foo").children[1]
|
52
|
+
expect(node.name).to eq :@foo
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'gets for cvar node' do
|
56
|
+
node = parse("@@foo = 'bar'\n@@foo").children[1]
|
57
|
+
expect(node.name).to eq :@@foo
|
58
|
+
end
|
59
|
+
|
45
60
|
it 'gets for mlhs node' do
|
46
61
|
node = parse('var.each { |(param1, param2)| }')
|
47
62
|
expect(node.arguments.first.name).to eq node.arguments.first
|
@@ -7,7 +7,7 @@ module Synvert::Core
|
|
7
7
|
subject {
|
8
8
|
source = 'arr.map {}.flatten'
|
9
9
|
node = Parser::CurrentRuby.parse(source)
|
10
|
-
instance = double(current_node: node)
|
10
|
+
instance = double(current_node: node, file_source: source)
|
11
11
|
Rewriter::DeleteAction.new(instance, :dot, :message)
|
12
12
|
}
|
13
13
|
|
@@ -4,6 +4,11 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
6
|
describe Rewriter::RubyVersion do
|
7
|
+
before do
|
8
|
+
expect(File).to receive(:exist?).with('./.ruby-version').and_return(true)
|
9
|
+
expect(File).to receive(:read).with('./.ruby-version').and_return('3.0.0')
|
10
|
+
end
|
11
|
+
|
7
12
|
it 'returns true if ruby version is greater than 1.9' do
|
8
13
|
ruby_version = Rewriter::RubyVersion.new('1.9')
|
9
14
|
expect(ruby_version).to be_match
|
@@ -39,7 +39,8 @@ module Synvert::Core
|
|
39
39
|
|
40
40
|
describe 'parses within_file' do
|
41
41
|
it 'does nothing if if_ruby does not match' do
|
42
|
-
|
42
|
+
expect(File).to receive(:exist?).with('./.ruby-version').and_return(true)
|
43
|
+
expect(File).to receive(:read).with('./.ruby-version').and_return('2.0.0')
|
43
44
|
expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
|
44
45
|
rewriter =
|
45
46
|
Rewriter.new 'group', 'name' do
|
@@ -51,7 +52,8 @@ module Synvert::Core
|
|
51
52
|
end
|
52
53
|
|
53
54
|
it 'delegates process to instances if if_ruby matches' do
|
54
|
-
|
55
|
+
expect(File).to receive(:exist?).with('./.ruby-version').and_return(true)
|
56
|
+
expect(File).to receive(:read).with('./.ruby-version').and_return('2.0.0')
|
55
57
|
expect_any_instance_of(Rewriter::Instance).to receive(:process)
|
56
58
|
rewriter =
|
57
59
|
Rewriter.new 'group', 'name' do
|
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.56.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-09-
|
11
|
+
date: 2021-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|