slimcop 0.5.0 → 0.8.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: 294e38d0a479f35dc2d3c6c2c042af57645927ec17cc2b91b6e6c68107defd40
4
- data.tar.gz: 5f65145f2f89a7fbae3341228bc5d3b2ac8c53c065748ed2dc2e86a1f49d8d95
3
+ metadata.gz: c8ddb3a3a507467e8a94efb06e67729026956faf788fae905c2520ddd2016d4d
4
+ data.tar.gz: 793d36ff3bfffbb5f4979996395f219fe301e5ab6cbd283a75cba389d2ef0b99
5
5
  SHA512:
6
- metadata.gz: cd736f85dafc680f45a77ab81f791620909eda2c23523cf0d7ce4e999587c464946c14a77a2e1853cd4f14da26e9cd2fd17b444ef0d53e031ae38220adef9998
7
- data.tar.gz: 840ee9ef8e7db1c8fb6590b36c157114784c51c20de7adad8a947fc4df305bb54b838fd63faadcbfafb52f715533effaaa95d5fd408a5be81fea546456dfaf95
6
+ metadata.gz: 80f8d52c2fc2a845ce0f06f58bdf943123e8253dd3834b504ee3dd3da8f30ddb55902ec9e30db041d6eae084617addcefd42b338a720f3300b5c05cf8af34f32
7
+ data.tar.gz: 1eef3d4bc19ad6824e6e70b98da9bb35b88cbf48e748a6bb8e00aa9bed21cedde553518ac7460098ef80ee5ac19ca6183973c89443896a22a4da4fc618ac30fb
data/CHANGELOG.md CHANGED
@@ -2,12 +2,48 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.8.0
6
+
7
+ ### Changed
8
+
9
+ - Use .rubocop.yml by default.
10
+ - Use "**/*.slim" by default.
11
+
12
+ ### Fixed
13
+
14
+ - Fix bug that --no-color was not working.
15
+
16
+ ## 0.7.1 - 2021-12-29
17
+
18
+ ### Fixed
19
+
20
+ - Exclude disabled offenses.
21
+ - Uniq paths at PathFinder.
22
+
23
+ ## 0.7.0 - 2021-12-28
24
+
25
+ ### Changed
26
+
27
+ - Re-use RuboCop's progress formatter as our default formatter.
28
+
29
+ ## 0.6.0 - 2021-12-28
30
+
31
+ ### Added
32
+
33
+ - Add -c, --config CLI option to customize RuboCop config.
34
+
35
+ ### Changed
36
+
37
+ - Not investigate all files then auto-correct them, but do it for each file.
38
+ - Sort processed files in alphabetical order.
39
+ - Disable Lint/UselessAssignment by default.
40
+
5
41
  ## 0.5.0 - 2021-12-27
6
42
 
7
43
  ### Changed
8
44
 
9
- - Ignore Lint/EmptyFile by default.
10
- - Ignore Style/RescueModifier by default.
45
+ - Disable Lint/EmptyFile by default.
46
+ - Disable Style/RescueModifier by default.
11
47
 
12
48
  ### Fixed
13
49
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimcop (0.5.0)
4
+ slimcop (0.8.0)
5
5
  rainbow
6
6
  rubocop (>= 0.87)
7
7
  slimi (>= 0.4)
@@ -45,7 +45,7 @@ GEM
45
45
  rubocop-rspec (2.6.0)
46
46
  rubocop (~> 1.19)
47
47
  ruby-progressbar (1.11.0)
48
- slimi (0.4.1)
48
+ slimi (0.4.2)
49
49
  temple
50
50
  temple (0.8.2)
51
51
  unicode-display_width (2.1.0)
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/default.yml CHANGED
@@ -79,6 +79,9 @@ Layout/TrailingEmptyLines:
79
79
  Layout/TrailingWhitespace:
80
80
  Enabled: false
81
81
 
82
+ Lint/UselessAssignment:
83
+ Enabled: false
84
+
82
85
  Lint/Void:
83
86
  Enabled: false
84
87
 
data/lib/slimcop/cli.rb CHANGED
@@ -1,69 +1,70 @@
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
- @configuration = Configuration.new
10
- @formatter = Formatter.new
11
10
  end
12
11
 
13
12
  def call
14
13
  options = parse!
15
- slim_file_paths = PathFinder.new(patterns: @argv).call
14
+ formatter = ::RuboCop::Formatter::ProgressFormatter.new($stdout, color: options[:color])
15
+ rubocop_config = RuboCopConfigGenerator.new(additional_config_file_path: options[:additional_config_file_path]).call
16
+ file_paths = PathFinder.new(patterns: @argv).call
16
17
 
17
- Rainbow.enabled = options[:color] if options.key?(:color)
18
-
19
- offenses_set = investigate(auto_correct: options[:auto_correct], slim_file_paths: slim_file_paths)
20
- correct(offenses_set) if options[:auto_correct]
21
- offenses = offenses_set.flat_map { |(_, _, array)| array }
22
- report(offenses)
18
+ formatter.started(file_paths)
19
+ offenses = file_paths.flat_map do |file_path|
20
+ formatter.file_started(file_path, {})
21
+ source = ::File.read(file_path)
22
+ offenses_ = investigate(
23
+ auto_correct: options[:auto_correct],
24
+ file_path: file_path,
25
+ rubocop_config: rubocop_config,
26
+ source: source
27
+ )
28
+ if options[:auto_correct]
29
+ correct(
30
+ file_path: file_path,
31
+ offenses: offenses_,
32
+ source: source
33
+ )
34
+ end
35
+ formatter.file_finished(file_path, offenses_)
36
+ offenses_
37
+ end
38
+ formatter.finished(file_paths)
23
39
  exit(offenses.empty? ? 0 : 1)
24
40
  end
25
41
 
26
42
  private
27
43
 
28
- # @param [Array] offenses_set
29
- def correct(offenses_set)
30
- offenses_set.each do |(file_path, source, offenses)|
31
- rewritten_source = SlimCorrector.new(
32
- file_path: file_path,
33
- offenses: offenses,
34
- source: source
35
- ).call
36
- ::File.write(file_path, rewritten_source)
37
- end
44
+ # @param [String] file_path
45
+ # @param [Array<Slimcop::Offense>] offenses
46
+ # @param [String] source
47
+ def correct(file_path:, offenses:, source:)
48
+ rewritten_source = SlimCorrector.new(
49
+ file_path: file_path,
50
+ offenses: offenses,
51
+ source: source
52
+ ).call
53
+ ::File.write(file_path, rewritten_source)
38
54
  end
39
55
 
40
56
  # @param [Boolean] auto_correct
41
- # @param [Array] slim_file_paths
42
- # @return [Array]
43
- def investigate(auto_correct:, slim_file_paths:)
44
- slim_file_paths.map do |file_path|
45
- source = ::File.read(file_path)
46
- offenses = SlimOffenseCollector.new(
47
- auto_correct: auto_correct,
48
- file_path: file_path,
49
- rubocop_config: @configuration.rubocop_config,
50
- source: source
51
- ).call
52
- [file_path, source, offenses]
53
- end
54
- end
55
-
56
- # @param [Array<Slimcop::Offense>] offenses
57
- def report(offenses)
58
- result = +''
59
- unless offenses.empty?
60
- result << "\nOffenses:\n\n"
61
- lines = offenses.map do |offense|
62
- @formatter.format_offense(offense)
63
- end
64
- result << lines.join("\n")
65
- end
66
- puts(result)
57
+ # @param [String] file_path
58
+ # @param [String] rubocop_config
59
+ # @param [String] source
60
+ # @return [Array<Slimcop::Offense>]
61
+ def investigate(auto_correct:, file_path:, rubocop_config:, source:)
62
+ SlimOffenseCollector.new(
63
+ auto_correct: auto_correct,
64
+ file_path: file_path,
65
+ rubocop_config: rubocop_config,
66
+ source: source
67
+ ).call
67
68
  end
68
69
 
69
70
  # @return [Hash]
@@ -74,6 +75,9 @@ module Slimcop
74
75
  parser.on('-a', '--auto-correct', 'Auto-correct offenses.') do
75
76
  options[:auto_correct] = true
76
77
  end
78
+ parser.on('-c', '--config=', 'Specify configuration file. (default: .rubocop.yml if it exists)') do |file_path|
79
+ options[:additional_config_file_path] = file_path
80
+ end
77
81
  parser.on('--[no-]color', 'Force color output on or off.') do |value|
78
82
  options[:color] = value
79
83
  end
@@ -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,8 +16,19 @@ 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)
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
17
32
  end
18
33
  end
19
34
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module Slimcop
6
+ class RuboCopConfigGenerator
7
+ DEFAULT_ADDITIONAL_CONFIG_PATH = '.rubocop.yml'
8
+
9
+ # @param [String] additional_config_file_path
10
+ def initialize(additional_config_file_path: nil)
11
+ @additional_config_file_path = additional_config_file_path
12
+ end
13
+
14
+ # @return [RuboCop::Config]
15
+ def call
16
+ ::RuboCop::ConfigLoader.merge_with_default(merged_config, loaded_path)
17
+ end
18
+
19
+ private
20
+
21
+ # @return [String]
22
+ def loaded_path
23
+ @additional_config_file_path || slimcop_default_config_file_path
24
+ end
25
+
26
+ # @return [RuboCop::Config]
27
+ def merged_config
28
+ ::RuboCop::Config.create(merged_config_hash, loaded_path)
29
+ end
30
+
31
+ # @return [Hash]
32
+ def merged_config_hash
33
+ result = slimcop_default_config
34
+ result = ::RuboCop::ConfigLoader.merge(result, additional_config) if additional_config
35
+ result
36
+ end
37
+
38
+ # @return [RuboCop::Config, nil]
39
+ def additional_config
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
50
+ end
51
+
52
+ # @return [RuboCop::Config]
53
+ def slimcop_default_config
54
+ ::RuboCop::ConfigLoader.load_file(slimcop_default_config_file_path)
55
+ end
56
+
57
+ # @return [String]
58
+ def slimcop_default_config_file_path
59
+ @slimcop_default_config_file_path ||= ::File.expand_path('../../default.yml', __dir__)
60
+ end
61
+ end
62
+ end
@@ -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.5.0'
4
+ VERSION = '0.8.0'
5
5
  end
data/lib/slimcop.rb CHANGED
@@ -4,10 +4,9 @@ require_relative 'slimcop/version'
4
4
 
5
5
  module Slimcop
6
6
  autoload :Cli, 'slimcop/cli'
7
- autoload :Configuration, 'slimcop/configuration'
8
- autoload :Formatter, 'slimcop/formatter'
9
7
  autoload :Offense, 'slimcop/offense'
10
8
  autoload :PathFinder, 'slimcop/path_finder'
9
+ autoload :RuboCopConfigGenerator, 'slimcop/rubo_cop_config_generator'
11
10
  autoload :RubyExtractor, 'slimcop/ruby_extractor'
12
11
  autoload :RubyOffenseCollector, 'slimcop/ruby_offense_collector'
13
12
  autoload :SlimCorrector, 'slimcop/slim_corrector'
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.5.0
4
+ version: 0.8.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-26 00:00:00.000000000 Z
11
+ date: 2021-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rainbow
@@ -75,10 +75,9 @@ files:
75
75
  - exe/slimcop
76
76
  - lib/slimcop.rb
77
77
  - lib/slimcop/cli.rb
78
- - lib/slimcop/configuration.rb
79
- - lib/slimcop/formatter.rb
80
78
  - lib/slimcop/offense.rb
81
79
  - lib/slimcop/path_finder.rb
80
+ - lib/slimcop/rubo_cop_config_generator.rb
82
81
  - lib/slimcop/ruby_extractor.rb
83
82
  - lib/slimcop/ruby_offense_collector.rb
84
83
  - lib/slimcop/slim_corrector.rb
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rubocop'
4
-
5
- module Slimcop
6
- class Configuration
7
- # @return [RuboCop::Config]
8
- def rubocop_config
9
- @rubocop_config ||= begin
10
- config_path = ::File.expand_path('../../default.yml', __dir__)
11
- config = ::RuboCop::ConfigLoader.load_file(config_path)
12
- ::RuboCop::ConfigLoader.merge_with_default(config, config_path)
13
- end
14
- end
15
- end
16
- end
@@ -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