slimcop 0.8.0 → 0.11.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: c8ddb3a3a507467e8a94efb06e67729026956faf788fae905c2520ddd2016d4d
4
- data.tar.gz: 793d36ff3bfffbb5f4979996395f219fe301e5ab6cbd283a75cba389d2ef0b99
3
+ metadata.gz: 4b967da47ab7a5721d082f4d1a4e9bb84006431c5acb5992a27f8246bdcfd904
4
+ data.tar.gz: 6e260866461bc31ddef835279d647905f17f87890b63af229bd98557699e0d8f
5
5
  SHA512:
6
- metadata.gz: 80f8d52c2fc2a845ce0f06f58bdf943123e8253dd3834b504ee3dd3da8f30ddb55902ec9e30db041d6eae084617addcefd42b338a720f3300b5c05cf8af34f32
7
- data.tar.gz: 1eef3d4bc19ad6824e6e70b98da9bb35b88cbf48e748a6bb8e00aa9bed21cedde553518ac7460098ef80ee5ac19ca6183973c89443896a22a4da4fc618ac30fb
6
+ metadata.gz: 62cbf9db3a9f0fc741714afdd4f886b61efb4bed1e3f4ca54e6e7caa03b3cf2e8f3141aec2456b3b13f7fe0999e5a7ab9a7578a6700225a220db08200f9b2f96
7
+ data.tar.gz: c6742245998d9fbd23dca47dbf55be3e8717722daa7515df5afab77baca05d1c2ddc26d80a62bbbc97a7cfa06332a5c6fb3bc078a7d3853479598f7cb86c36ea
data/CHANGELOG.md CHANGED
@@ -2,7 +2,36 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
- ## 0.8.0
5
+ ## 0.11.0 - 2022-01-10
6
+
7
+ ### Changed
8
+
9
+ - Use :offset instead of :begin_ at RubyExtractor.
10
+ - Detect offenses from code that containing `if`, `unless`, `do`, etc.
11
+
12
+ ## 0.10.0 - 2022-01-06
13
+
14
+ ### Added
15
+
16
+ - Use .slimcop.yml as its 1st default config file.
17
+
18
+ ## 0.9.1 - 2022-01-04
19
+
20
+ ### Fixed
21
+
22
+ - Fix $LOAD_PATH at ./exe/slimcop.
23
+
24
+ ## 0.9.0 - 2022-01-02
25
+
26
+ ### Changed
27
+
28
+ - Require slimi >= 0.5.1 to extract Ruby code from Ruby attribute.
29
+
30
+ ### Fixed
31
+
32
+ - Fix -v and --version option.
33
+
34
+ ## 0.8.0 - 2021-12-29
6
35
 
7
36
  ### Changed
8
37
 
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimcop (0.8.0)
4
+ slimcop (0.11.0)
5
5
  rainbow
6
6
  rubocop (>= 0.87)
7
- slimi (>= 0.4)
7
+ slimi (>= 0.5.1)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -45,9 +45,13 @@ GEM
45
45
  rubocop-rspec (2.6.0)
46
46
  rubocop (~> 1.19)
47
47
  ruby-progressbar (1.11.0)
48
- slimi (0.4.2)
48
+ slimi (0.7.0)
49
49
  temple
50
+ thor
51
+ tilt
50
52
  temple (0.8.2)
53
+ thor (1.2.1)
54
+ tilt (2.0.10)
51
55
  unicode-display_width (2.1.0)
52
56
 
53
57
  PLATFORMS
data/README.md CHANGED
@@ -40,7 +40,7 @@ Usage: slimcop [options] [file1, file2, ...]
40
40
  ### Example
41
41
 
42
42
  ```console
43
- $ slimcop
43
+ $ slimcop 'spec/fixtures/**/*.slim'
44
44
  Inspecting 1 file
45
45
  C
46
46
 
data/exe/slimcop CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
5
  require 'slimcop'
5
6
 
6
7
  Slimcop::Cli.new(ARGV).call
data/lib/slimcop/cli.rb CHANGED
@@ -72,10 +72,11 @@ module Slimcop
72
72
  options = {}
73
73
  parser = ::OptionParser.new
74
74
  parser.banner = 'Usage: slimcop [options] [file1, file2, ...]'
75
+ parser.version = VERSION
75
76
  parser.on('-a', '--auto-correct', 'Auto-correct offenses.') do
76
77
  options[:auto_correct] = true
77
78
  end
78
- parser.on('-c', '--config=', 'Specify configuration file. (default: .rubocop.yml if it exists)') do |file_path|
79
+ parser.on('-c', '--config=', 'Specify configuration file. (default: .slimcop.yml or .rubocop.yml)') do |file_path|
79
80
  options[:additional_config_file_path] = file_path
80
81
  end
81
82
  parser.on('--[no-]color', 'Force color output on or off.') do |value|
@@ -4,7 +4,8 @@ require 'rubocop'
4
4
 
5
5
  module Slimcop
6
6
  class RuboCopConfigGenerator
7
- DEFAULT_ADDITIONAL_CONFIG_PATH = '.rubocop.yml'
7
+ DEFAULT_ADDITIONAL_CONFIG_PATH1 = '.slimcop.yml'
8
+ DEFAULT_ADDITIONAL_CONFIG_PATH2 = '.rubocop.yml'
8
9
 
9
10
  # @param [String] additional_config_file_path
10
11
  def initialize(additional_config_file_path: nil)
@@ -43,8 +44,10 @@ module Slimcop
43
44
  @additional_config = \
44
45
  if @additional_config_file_path
45
46
  ::RuboCop::ConfigLoader.load_file(@additional_config_file_path)
46
- elsif ::File.exist?(DEFAULT_ADDITIONAL_CONFIG_PATH)
47
- ::RuboCop::ConfigLoader.load_file(DEFAULT_ADDITIONAL_CONFIG_PATH)
47
+ elsif ::File.exist?(DEFAULT_ADDITIONAL_CONFIG_PATH1)
48
+ ::RuboCop::ConfigLoader.load_file(DEFAULT_ADDITIONAL_CONFIG_PATH1)
49
+ elsif ::File.exist?(DEFAULT_ADDITIONAL_CONFIG_PATH2)
50
+ ::RuboCop::ConfigLoader.load_file(DEFAULT_ADDITIONAL_CONFIG_PATH2)
48
51
  end
49
52
  end
50
53
  end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slimcop
4
+ # Remove unnecessary part (e.g. `if`, `unless`, `do`, ...) from Ruby code.
5
+ class RubyClipper
6
+ # @param [String] code
7
+ def initialize(code)
8
+ @code = code
9
+ end
10
+
11
+ # @return [Hash]
12
+ def call
13
+ [
14
+ PrecedingKeywordRemover,
15
+ TrailingDoRemover
16
+ ].each_with_object(
17
+ code: @code,
18
+ offset: 0
19
+ ) do |klass, object|
20
+ result = klass.new(object[:code]).call
21
+ object[:code] = result[:code]
22
+ object[:offset] += result[:offset]
23
+ end
24
+ end
25
+
26
+ class PrecedingKeywordRemover
27
+ REGEXP = /
28
+ \A
29
+ (?:
30
+ begin
31
+ | case
32
+ | else
33
+ | elsif
34
+ | ensure
35
+ | if
36
+ | rescue
37
+ | unless
38
+ | until
39
+ | when
40
+ | while
41
+ | for[ \t]+\w+[ \t]+in
42
+ )
43
+ [ \t]
44
+ /x.freeze
45
+
46
+ # @param [String] code
47
+ def initialize(code)
48
+ @code = code
49
+ end
50
+
51
+ # @return [Hash]
52
+ def call
53
+ data = @code.match(REGEXP)
54
+ if data
55
+ offset = data[0].length
56
+ {
57
+ code: @code[offset..],
58
+ offset: offset
59
+ }
60
+ else
61
+ {
62
+ code: @code,
63
+ offset: 0
64
+ }
65
+ end
66
+ end
67
+ end
68
+
69
+ class TrailingDoRemover
70
+ REGEXP = /
71
+ [ \t]
72
+ do
73
+ [ \t]*
74
+ (\|[^|]*\|)?
75
+ [ \t]*
76
+ \Z
77
+ /x.freeze
78
+
79
+ # @param [String] code
80
+ def initialize(code)
81
+ @code = code
82
+ end
83
+
84
+ # @return [Hash]
85
+ def call
86
+ {
87
+ code: @code.sub(REGEXP, ''),
88
+ offset: 0
89
+ }
90
+ end
91
+ end
92
+ end
93
+ end
@@ -3,7 +3,7 @@
3
3
  require 'slimi'
4
4
 
5
5
  module Slimcop
6
- # Extract codes from Slim source.
6
+ # Extract Ruby codes from Slim source.
7
7
  class RubyExtractor
8
8
  # @param [String, nil] file_path
9
9
  # @param [String] source
@@ -15,10 +15,10 @@ module Slimcop
15
15
  # @return [Array<Hash>]
16
16
  def call
17
17
  ranges.map do |(begin_, end_)|
18
+ clipped = RubyClipper.new(@source[begin_...end_]).call
18
19
  {
19
- begin_: begin_,
20
- code: @source[begin_...end_],
21
- end_: end_
20
+ code: clipped[:code],
21
+ offset: begin_ + clipped[:offset]
22
22
  }
23
23
  end
24
24
  end
@@ -25,7 +25,7 @@ module Slimcop
25
25
  ).call.map do |rubocop_offense|
26
26
  Offense.new(
27
27
  file_path: @file_path,
28
- offset: snippet[:begin_],
28
+ offset: snippet[:offset],
29
29
  rubocop_offense: rubocop_offense,
30
30
  source: @source
31
31
  )
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slimcop
4
- VERSION = '0.8.0'
4
+ VERSION = '0.11.0'
5
5
  end
data/lib/slimcop.rb CHANGED
@@ -7,6 +7,7 @@ module Slimcop
7
7
  autoload :Offense, 'slimcop/offense'
8
8
  autoload :PathFinder, 'slimcop/path_finder'
9
9
  autoload :RuboCopConfigGenerator, 'slimcop/rubo_cop_config_generator'
10
+ autoload :RubyClipper, 'slimcop/ruby_clipper'
10
11
  autoload :RubyExtractor, 'slimcop/ruby_extractor'
11
12
  autoload :RubyOffenseCollector, 'slimcop/ruby_offense_collector'
12
13
  autoload :SlimCorrector, 'slimcop/slim_corrector'
data/slimcop.gemspec CHANGED
@@ -33,5 +33,5 @@ Gem::Specification.new do |spec|
33
33
 
34
34
  spec.add_dependency 'rainbow'
35
35
  spec.add_dependency 'rubocop', '>= 0.87'
36
- spec.add_dependency 'slimi', '>= 0.4'
36
+ spec.add_dependency 'slimi', '>= 0.5.1'
37
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slimcop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-28 00:00:00.000000000 Z
11
+ date: 2022-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rainbow
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0.4'
47
+ version: 0.5.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0.4'
54
+ version: 0.5.1
55
55
  description:
56
56
  email:
57
57
  - r7kamura@gmail.com
@@ -78,6 +78,7 @@ files:
78
78
  - lib/slimcop/offense.rb
79
79
  - lib/slimcop/path_finder.rb
80
80
  - lib/slimcop/rubo_cop_config_generator.rb
81
+ - lib/slimcop/ruby_clipper.rb
81
82
  - lib/slimcop/ruby_extractor.rb
82
83
  - lib/slimcop/ruby_offense_collector.rb
83
84
  - lib/slimcop/slim_corrector.rb