synvert-core 0.56.0 → 0.56.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d9026b58fc968fd19fb6bcfb02a7235ba90fa2dee89b74c4707191ddbe892e7
4
- data.tar.gz: 80b9f33e6c34e9b575a1dfac6b89adc8e5362cca7506a0b64b86047e3e27afbf
3
+ metadata.gz: e60611e972f774c97a102f5db6bc171b024cb48aa4dcc94dd07e9de6d25506a2
4
+ data.tar.gz: 2a7f4da54b1ed5caeb7fea49dd07ca1cc9fad1aceef082d223e420b928825bb9
5
5
  SHA512:
6
- metadata.gz: 40638d3f2674232745f15810f9f5d12d7f2f5f9b99dc353f7fc568812cad747c24b502aec3c50b1952dfba14db034545719e8f14792658bdd44c7ec7db6e05be
7
- data.tar.gz: 3d9dc8284d401ec5453c8023a23b0b5096c38d8d9581155dca7069f57c25999315f5a075c1d6eff4b802b8f4d2cc4d8a037ce0c0cf33cd8e2c225781e7790b00
6
+ metadata.gz: f2fa37ef33a1ec38afbf094715b239c46e35e92aa386f5fae635be4419da964a8543f07df36c4321408b9a5d80a9a252d68674ccb9040865191d0299d9775667
7
+ data.tar.gz: a31385980557ef6c6031365f1554c5e957e6933b3aaaa65d902011dc3920599c8d24ae80bde55227f36643ebcc6808ee55ff2f028ca4c92cab94eeb04e606413
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.56.1 (2021-09-19)
4
+
5
+ * Compare ruby version in `.ruby-version` or `.rvmrc`
6
+ * Abstract `squeeze_spaces` and `squeeze_lines`
7
+
3
8
  ## 0.56.0 (2021-09-14)
4
9
 
5
10
  * Support `name` for `:lvar`, `:ivar`, `:cvar`
@@ -13,11 +13,7 @@ module Synvert::Core
13
13
  # @return [Integer] begin position.
14
14
  def begin_pos
15
15
  pos = @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:begin_pos).min
16
- if @instance.file_source[pos - 1] == ' ' && @instance.file_source[end_pos] == ' '
17
- pos - 1
18
- else
19
- pos
20
- end
16
+ squeeze_spaces(pos, end_pos)
21
17
  end
22
18
 
23
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
 
@@ -47,11 +48,8 @@ module Synvert::Core
47
48
 
48
49
  def end_index
49
50
  index = file_source[@node.loc.expression.end_pos..-1].index("\n")
50
- index ? @node.loc.expression.end_pos + index + "\n".length : @node.loc.expression.end_pos
51
- end
52
-
53
- def file_source
54
- @file_source ||= @instance.file_source
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)
55
53
  end
56
54
  end
57
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
- # GemSpec checks and compares gem version.
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
- Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(@version)
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 dal, it checks if ruby version if greater than or equal to the specified ruby version.
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)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.56.0'
5
+ VERSION = '0.56.1'
6
6
  end
7
7
  end
@@ -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
- stub_const('RUBY_VERSION', '2.0.0')
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
- stub_const('RUBY_VERSION', '2.0.0')
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.56.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-14 00:00:00.000000000 Z
11
+ date: 2021-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport