slimcop 0.6.0 → 0.9.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: 8f5970bfa12ccffdb04b26f3b827b8102e035cb95ddd7f6d2ad70f1ff53d240f
4
- data.tar.gz: d293f0d5c72e0e96c21f515ed16c6f0dbe9bf0aae745e42cec20186e654922a0
3
+ metadata.gz: 4ddeb0d894aab7002ef81c70937513071636e1d048f5fd8e94300fd9af6c96a4
4
+ data.tar.gz: c12290288593975b5e2ca620fd023ada7b6c04d39c41dcbae8155f9f8245809f
5
5
  SHA512:
6
- metadata.gz: b2423cffaf849c867d335df2d3fec06645f5578aecc9597a9d218d5cc99989a86be664609b364c6092695ad391825ce9ff244387d3dd6d77710d5c820d02d5f1
7
- data.tar.gz: 294f4adb1bc4e8198a2d0b6fb79b810561a57a90fb01ebcfdfe0d3d057278e67ca3c8f77c5063b1bbadb55c36e84b7363db4b39d3004de2d7884f1afcb5f6cca
6
+ metadata.gz: 02b791ca65f375995f387c08cb107c7d7d37791fdcfa6546cef9ae60869e00bd21bed8610bce6f5dad8b8641f48b1ab8528f29538ee2352af356a08d10cc7ae5
7
+ data.tar.gz: 7d58c62120d8fc325d339f5837d7d3f4ef9e72209ca106f2c3b5a9ce96f27d57f7a8b9fbd5da0e9292e479dcb2cb9459907189a9622f98d9d9f2cfd27666eca8
data/CHANGELOG.md CHANGED
@@ -2,6 +2,40 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.9.0 - 2022-01-02
6
+
7
+ ### Changed
8
+
9
+ - Require slimi >= 0.5.1 to extract Ruby code from Ruby attribute.
10
+
11
+ ### Fixed
12
+
13
+ - Fix -v and --version option.
14
+
15
+ ## 0.8.0 - 2021-12-29
16
+
17
+ ### Changed
18
+
19
+ - Use .rubocop.yml by default.
20
+ - Use "**/*.slim" by default.
21
+
22
+ ### Fixed
23
+
24
+ - Fix bug that --no-color was not working.
25
+
26
+ ## 0.7.1 - 2021-12-29
27
+
28
+ ### Fixed
29
+
30
+ - Exclude disabled offenses.
31
+ - Uniq paths at PathFinder.
32
+
33
+ ## 0.7.0 - 2021-12-28
34
+
35
+ ### Changed
36
+
37
+ - Re-use RuboCop's progress formatter as our default formatter.
38
+
5
39
  ## 0.6.0 - 2021-12-28
6
40
 
7
41
  ### Added
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimcop (0.6.0)
4
+ slimcop (0.9.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,11 @@ 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.5.1)
49
49
  temple
50
+ tilt
50
51
  temple (0.8.2)
52
+ tilt (2.0.10)
51
53
  unicode-display_width (2.1.0)
52
54
 
53
55
  PLATFORMS
data/README.md CHANGED
@@ -33,16 +33,25 @@ Use `slimcop` executable to check offenses and auto-correct them.
33
33
  $ slimcop --help
34
34
  Usage: slimcop [options] [file1, file2, ...]
35
35
  -a, --auto-correct Auto-correct offenses.
36
+ -c, --config= Specify configuration file.
36
37
  --[no-]color Force color output on or off.
37
38
  ```
38
39
 
39
40
  ### Example
40
41
 
41
42
  ```console
42
- $ slimcop spec/**/*.slim
43
+ $ slimcop
44
+ Inspecting 1 file
45
+ C
43
46
 
44
47
  Offenses:
45
48
 
46
- spec/fixtures/dummy.slim:1:3 C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
47
- spec/fixtures/dummy.slim:3:5 C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
49
+ spec/fixtures/dummy.slim:1:3: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
50
+ - "a"
51
+ ^^^
52
+ spec/fixtures/dummy.slim:3:5: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
53
+ | #{"c"}
54
+ ^^^
55
+
56
+ 1 file inspected, 2 offenses detected, 2 offenses auto-correctable
48
57
  ```
data/lib/slimcop/cli.rb CHANGED
@@ -1,21 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rainbow'
4
+ require 'rubocop'
4
5
 
5
6
  module Slimcop
6
7
  class Cli
7
8
  def initialize(argv)
8
9
  @argv = argv.dup
9
- @formatter = Formatter.new
10
10
  end
11
11
 
12
12
  def call
13
13
  options = parse!
14
- Rainbow.enabled = options[:color] if options.key?(:color)
14
+ formatter = ::RuboCop::Formatter::ProgressFormatter.new($stdout, color: options[:color])
15
15
  rubocop_config = RuboCopConfigGenerator.new(additional_config_file_path: options[:additional_config_file_path]).call
16
16
  file_paths = PathFinder.new(patterns: @argv).call
17
17
 
18
+ formatter.started(file_paths)
18
19
  offenses = file_paths.flat_map do |file_path|
20
+ formatter.file_started(file_path, {})
19
21
  source = ::File.read(file_path)
20
22
  offenses_ = investigate(
21
23
  auto_correct: options[:auto_correct],
@@ -30,9 +32,10 @@ module Slimcop
30
32
  source: source
31
33
  )
32
34
  end
35
+ formatter.file_finished(file_path, offenses_)
33
36
  offenses_
34
37
  end
35
- report(offenses)
38
+ formatter.finished(file_paths)
36
39
  exit(offenses.empty? ? 0 : 1)
37
40
  end
38
41
 
@@ -64,28 +67,16 @@ module Slimcop
64
67
  ).call
65
68
  end
66
69
 
67
- # @param [Array<Slimcop::Offense>] offenses
68
- def report(offenses)
69
- result = +''
70
- unless offenses.empty?
71
- result << "\nOffenses:\n\n"
72
- lines = offenses.map do |offense|
73
- @formatter.format_offense(offense)
74
- end
75
- result << lines.join("\n")
76
- end
77
- puts(result)
78
- end
79
-
80
70
  # @return [Hash]
81
71
  def parse!
82
72
  options = {}
83
73
  parser = ::OptionParser.new
84
74
  parser.banner = 'Usage: slimcop [options] [file1, file2, ...]'
75
+ parser.version = VERSION
85
76
  parser.on('-a', '--auto-correct', 'Auto-correct offenses.') do
86
77
  options[:auto_correct] = true
87
78
  end
88
- parser.on('-c', '--config=', 'Specify configuration file.') do |file_path|
79
+ parser.on('-c', '--config=', 'Specify configuration file. (default: .rubocop.yml if it exists)') do |file_path|
89
80
  options[:additional_config_file_path] = file_path
90
81
  end
91
82
  parser.on('--[no-]color', 'Force color output on or off.') do |value|
@@ -1,17 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'forwardable'
4
+
3
5
  require 'parser'
6
+ require 'rubocop'
4
7
 
5
8
  module Slimcop
6
9
  class Offense
10
+ extend ::Forwardable
11
+
7
12
  # @return [String]
8
13
  attr_reader :file_path
9
14
 
10
15
  # @return [Integer]
11
16
  attr_reader :offset
12
17
 
13
- # @return [RuboCop::Cop::Offense]
14
- attr_reader :rubocop_offense
18
+ delegate(
19
+ %i[
20
+ column
21
+ column_length
22
+ correctable?
23
+ corrected_with_todo?
24
+ corrected?
25
+ corrector
26
+ highlighted_area
27
+ line
28
+ message
29
+ real_column
30
+ severity
31
+ ] => :rubocop_offense_with_real_location
32
+ )
15
33
 
16
34
  # @param [Integer] offset
17
35
  # @param [RuboCop::Cop::Offense] rubocop_offense
@@ -23,29 +41,13 @@ module Slimcop
23
41
  @source = source
24
42
  end
25
43
 
26
- # @return [RuboCop::Cop::Corrector]
27
- def corrector
28
- @rubocop_offense.corrector
29
- end
30
-
31
- # @return [Integer]
32
- def line
33
- range.line
34
- end
35
-
36
- # @return [String]
37
- def message
38
- @rubocop_offense.message
39
- end
40
-
41
- # @return [Integer]
42
- def real_column
43
- range.column + 1
44
- end
45
-
46
- # @return [RuboCop::Cop::Severity]
47
- def severity
48
- @rubocop_offense.severity
44
+ # @return [Parser::Source::Range]
45
+ def location
46
+ @location ||= ::Parser::Source::Range.new(
47
+ buffer,
48
+ @rubocop_offense.location.begin_pos + @offset,
49
+ @rubocop_offense.location.end_pos + @offset
50
+ )
49
51
  end
50
52
 
51
53
  private
@@ -58,12 +60,15 @@ module Slimcop
58
60
  )
59
61
  end
60
62
 
61
- # @return [Parser::Source::Range]
62
- def range
63
- @range ||= ::Parser::Source::Range.new(
64
- buffer,
65
- @rubocop_offense.location.begin_pos + @offset,
66
- @rubocop_offense.location.end_pos + @offset
63
+ # @return [RuboCop::Cop::Offense]
64
+ def rubocop_offense_with_real_location
65
+ ::RuboCop::Cop::Offense.new(
66
+ @rubocop_offense.severity.name,
67
+ location,
68
+ @rubocop_offense.message,
69
+ @rubocop_offense.cop_name,
70
+ @rubocop_offense.status,
71
+ @rubocop_offense.corrector
67
72
  )
68
73
  end
69
74
  end
@@ -5,6 +5,10 @@ require 'pathname'
5
5
  module Slimcop
6
6
  # Collect file paths from given path patterns.
7
7
  class PathFinder
8
+ DEFAULT_PATH_PATTERNS = %w[
9
+ **/*.slim
10
+ ].freeze
11
+
8
12
  # @param [Array<String>] patterns Patterns normally given as CLI arguments (e.g. `["app/views/**/*.html.slim"]`).
9
13
  def initialize(patterns:)
10
14
  @patterns = patterns
@@ -12,9 +16,20 @@ module Slimcop
12
16
 
13
17
  # @return [Array<String>]
14
18
  def call
15
- @patterns.flat_map do |pattern|
19
+ patterns.flat_map do |pattern|
16
20
  ::Pathname.glob(pattern).select(&:file?).map(&:to_s)
17
- end.sort
21
+ end.uniq.sort
22
+ end
23
+
24
+ private
25
+
26
+ # @return [Array<String>]
27
+ def patterns
28
+ if @patterns.empty?
29
+ DEFAULT_PATH_PATTERNS
30
+ else
31
+ @patterns
32
+ end
18
33
  end
19
34
  end
20
35
  end
@@ -4,6 +4,8 @@ require 'rubocop'
4
4
 
5
5
  module Slimcop
6
6
  class RuboCopConfigGenerator
7
+ DEFAULT_ADDITIONAL_CONFIG_PATH = '.rubocop.yml'
8
+
7
9
  # @param [String] additional_config_file_path
8
10
  def initialize(additional_config_file_path: nil)
9
11
  @additional_config_file_path = additional_config_file_path
@@ -29,13 +31,22 @@ module Slimcop
29
31
  # @return [Hash]
30
32
  def merged_config_hash
31
33
  result = slimcop_default_config
32
- result = ::RuboCop::ConfigLoader.merge(result, additional_config) if @additional_config_file_path
34
+ result = ::RuboCop::ConfigLoader.merge(result, additional_config) if additional_config
33
35
  result
34
36
  end
35
37
 
36
38
  # @return [RuboCop::Config, nil]
37
39
  def additional_config
38
- ::RuboCop::ConfigLoader.load_file(@additional_config_file_path) if @additional_config_file_path
40
+ if instance_variable_defined?(:@additional_config)
41
+ @additional_config
42
+ else
43
+ @additional_config = \
44
+ if @additional_config_file_path
45
+ ::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)
48
+ end
49
+ end
39
50
  end
40
51
 
41
52
  # @return [RuboCop::Config]
@@ -21,7 +21,7 @@ module Slimcop
21
21
  # Skip if invalid syntax Ruby code is given. (e.g. "- if a?")
22
22
  return [] unless rubocop_processed_source.valid_syntax?
23
23
 
24
- rubocop_team.investigate(rubocop_processed_source).offenses
24
+ rubocop_team.investigate(rubocop_processed_source).offenses.reject(&:disabled?)
25
25
  end
26
26
 
27
27
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slimcop
4
- VERSION = '0.6.0'
4
+ VERSION = '0.9.0'
5
5
  end
data/lib/slimcop.rb CHANGED
@@ -4,7 +4,6 @@ require_relative 'slimcop/version'
4
4
 
5
5
  module Slimcop
6
6
  autoload :Cli, 'slimcop/cli'
7
- autoload :Formatter, 'slimcop/formatter'
8
7
  autoload :Offense, 'slimcop/offense'
9
8
  autoload :PathFinder, 'slimcop/path_finder'
10
9
  autoload :RuboCopConfigGenerator, 'slimcop/rubo_cop_config_generator'
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.6.0
4
+ version: 0.9.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-27 00:00:00.000000000 Z
11
+ date: 2022-01-02 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
@@ -75,7 +75,6 @@ files:
75
75
  - exe/slimcop
76
76
  - lib/slimcop.rb
77
77
  - lib/slimcop/cli.rb
78
- - lib/slimcop/formatter.rb
79
78
  - lib/slimcop/offense.rb
80
79
  - lib/slimcop/path_finder.rb
81
80
  - lib/slimcop/rubo_cop_config_generator.rb
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'optparse'
4
- require 'rainbow'
5
-
6
- module Slimcop
7
- # Format String for CLI output.
8
- class Formatter
9
- COLOR_FOR_SEVERITY_CODE = {
10
- convention: :yellow,
11
- error: :red,
12
- fatal: :red,
13
- info: :gray,
14
- refactor: :yellow,
15
- warning: :magenta
16
- }.freeze
17
-
18
- # @param [Slimcop::Offense] offense
19
- # @return [String]
20
- def format_offense(offense)
21
- format(
22
- '%<path>s:%<line>d:%<column>d %<severity>s: %<message>s',
23
- column: offense.real_column,
24
- line: offense.line,
25
- message: message(offense),
26
- path: file_path(offense),
27
- severity: severity(offense)
28
- )
29
- end
30
-
31
- private
32
-
33
- # @param [String] path e.g. "./spec/fixtures/dummy.slim"
34
- # @return [String]
35
- # @example "spec/fixtures/dummy.slim"
36
- def canonicalize_path(path)
37
- ::File.expand_path(path).delete_prefix("#{::Dir.pwd}/")
38
- end
39
-
40
- # @param [Slimcop::Offense] offense
41
- # @return [String]
42
- def file_path(offense)
43
- Rainbow(canonicalize_path(offense.file_path)).cyan
44
- end
45
-
46
- # @param [Slimcop::Offense] offense
47
- # @return [String]
48
- def message(offense)
49
- "#{status(offense)}#{offense.message}"
50
- end
51
-
52
- # @param [Slimcop::Offense] offense
53
- # @return [String]
54
- def severity(offense)
55
- Rainbow(offense.severity.code).color(COLOR_FOR_SEVERITY_CODE[offense.severity.name])
56
- end
57
-
58
- # @param [Slimcop::Offense] offense
59
- # @return [String]
60
- def status(offense)
61
- if offense.rubocop_offense.corrected_with_todo?
62
- Rainbow('[Todo] ').green
63
- elsif offense.rubocop_offense.corrected?
64
- Rainbow('[Corrected] ').green
65
- elsif offense.rubocop_offense.correctable?
66
- Rainbow('[Correctable] ').yellow
67
- else
68
- ''
69
- end
70
- end
71
- end
72
- end