slimcop 0.2.0 → 0.6.0

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: bace4128c4ef162ff7f9a4f1eca5af7275631b850df5a6ad48a92d8693114771
4
- data.tar.gz: d7a04c3c5ed2930490174e41e838cd6c7e26a8eae8e6476fa188ee89ae3f103f
3
+ metadata.gz: 8f5970bfa12ccffdb04b26f3b827b8102e035cb95ddd7f6d2ad70f1ff53d240f
4
+ data.tar.gz: d293f0d5c72e0e96c21f515ed16c6f0dbe9bf0aae745e42cec20186e654922a0
5
5
  SHA512:
6
- metadata.gz: d4cacec671cd5855ceedd2183099450f2e4abf759d71f11e77e4af5f5c25b7e52a71d6da4f4e7fbaac812bf7509311c4edc5233501b021dfbb9da6259e1b305d
7
- data.tar.gz: 0db1c7751629572c7e8738e52fe8d44f41e20047b48f091aa00defa0dad62f2674bf5ae0fafe2b2a53ce5b3eab006fde3f0a9ae840a8cc6c779cadb9632c4845
6
+ metadata.gz: b2423cffaf849c867d335df2d3fec06645f5578aecc9597a9d218d5cc99989a86be664609b364c6092695ad391825ce9ff244387d3dd6d77710d5c820d02d5f1
7
+ data.tar.gz: 294f4adb1bc4e8198a2d0b6fb79b810561a57a90fb01ebcfdfe0d3d057278e67ca3c8f77c5063b1bbadb55c36e84b7363db4b39d3004de2d7884f1afcb5f6cca
data/.rubocop.yml CHANGED
@@ -1,3 +1,6 @@
1
+ require:
2
+ - rubocop-rspec
3
+
1
4
  AllCops:
2
5
  NewCops: enable
3
6
  SuggestExtensions: false
@@ -9,5 +12,20 @@ Layout/LineLength:
9
12
  Metrics:
10
13
  Enabled: false
11
14
 
15
+ RSpec/AnyInstance:
16
+ Enabled: false
17
+
18
+ RSpec/ExampleLength:
19
+ Enabled: false
20
+
21
+ RSpec/ImplicitSubject:
22
+ Enabled: false
23
+
24
+ RSpec/MultipleExpectations:
25
+ Enabled: false
26
+
27
+ RSpec/NamedSubject:
28
+ Enabled: false
29
+
12
30
  Style/Documentation:
13
31
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -2,6 +2,50 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.6.0 - 2021-12-28
6
+
7
+ ### Added
8
+
9
+ - Add -c, --config CLI option to customize RuboCop config.
10
+
11
+ ### Changed
12
+
13
+ - Not investigate all files then auto-correct them, but do it for each file.
14
+ - Sort processed files in alphabetical order.
15
+ - Disable Lint/UselessAssignment by default.
16
+
17
+ ## 0.5.0 - 2021-12-27
18
+
19
+ ### Changed
20
+
21
+ - Disable Lint/EmptyFile by default.
22
+ - Disable Style/RescueModifier by default.
23
+
24
+ ### Fixed
25
+
26
+ - Fix NoMethodError on some RuboCop offenses.
27
+ - Fix bug when uncorrectable RuboCop offense exists.
28
+
29
+ ## 0.4.0 - 2021-12-26
30
+
31
+ ### Added
32
+
33
+ - Support glob pattern on arguments of executable.
34
+
35
+ ### Fixed
36
+
37
+ - Fix bug on parsing invalid syntax Ruby code.
38
+
39
+ ## 0.3.0 - 2021-12-25
40
+
41
+ ### Added
42
+
43
+ - Show Slim file path on Slim syntax error.
44
+
45
+ ### Changed
46
+
47
+ - Require slimi >= 0.4 for :file option handling.
48
+
5
49
  ## 0.2.0 - 2021-12-21
6
50
 
7
51
  ### Added
data/Gemfile CHANGED
@@ -2,11 +2,9 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
- # Specify your gem's dependencies in slimcop.gemspec
6
5
  gemspec
7
6
 
8
7
  gem 'rake', '~> 13.0'
9
-
10
- gem 'rspec', '~> 3.0'
11
-
12
- gem 'rubocop', '~> 1.21'
8
+ gem 'rspec'
9
+ gem 'rubocop'
10
+ gem 'rubocop-rspec'
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimcop (0.2.0)
4
+ slimcop (0.6.0)
5
5
  rainbow
6
6
  rubocop (>= 0.87)
7
- slimi (>= 0.1.1)
7
+ slimi (>= 0.4)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -42,8 +42,10 @@ GEM
42
42
  unicode-display_width (>= 1.4.0, < 3.0)
43
43
  rubocop-ast (1.15.0)
44
44
  parser (>= 3.0.1.1)
45
+ rubocop-rspec (2.6.0)
46
+ rubocop (~> 1.19)
45
47
  ruby-progressbar (1.11.0)
46
- slimi (0.1.1)
48
+ slimi (0.4.2)
47
49
  temple
48
50
  temple (0.8.2)
49
51
  unicode-display_width (2.1.0)
@@ -53,8 +55,9 @@ PLATFORMS
53
55
 
54
56
  DEPENDENCIES
55
57
  rake (~> 13.0)
56
- rspec (~> 3.0)
57
- rubocop (~> 1.21)
58
+ rspec
59
+ rubocop
60
+ rubocop-rspec
58
61
  slimcop!
59
62
 
60
63
  BUNDLED WITH
data/README.md CHANGED
@@ -35,3 +35,14 @@ Usage: slimcop [options] [file1, file2, ...]
35
35
  -a, --auto-correct Auto-correct offenses.
36
36
  --[no-]color Force color output on or off.
37
37
  ```
38
+
39
+ ### Example
40
+
41
+ ```console
42
+ $ slimcop spec/**/*.slim
43
+
44
+ Offenses:
45
+
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.
48
+ ```
data/default.yml CHANGED
@@ -10,6 +10,9 @@ Layout/BlockAlignment:
10
10
  Layout/ClosingParenthesisIndentation:
11
11
  Enabled: false
12
12
 
13
+ Lint/EmptyFile:
14
+ Enabled: false
15
+
13
16
  Layout/EmptyLineAfterGuardClause:
14
17
  Enabled: false
15
18
 
@@ -76,6 +79,9 @@ Layout/TrailingEmptyLines:
76
79
  Layout/TrailingWhitespace:
77
80
  Enabled: false
78
81
 
82
+ Lint/UselessAssignment:
83
+ Enabled: false
84
+
79
85
  Lint/Void:
80
86
  Enabled: false
81
87
 
@@ -100,6 +106,9 @@ Style/IfUnlessModifier:
100
106
  Style/Next:
101
107
  Enabled: false
102
108
 
109
+ Style/RescueModifier:
110
+ Enabled: false
111
+
103
112
  Style/WhileUntilDo:
104
113
  Enabled: false
105
114
 
data/lib/slimcop/cli.rb CHANGED
@@ -6,51 +6,62 @@ module Slimcop
6
6
  class Cli
7
7
  def initialize(argv)
8
8
  @argv = argv.dup
9
- @configuration = Configuration.new
10
9
  @formatter = Formatter.new
11
10
  end
12
11
 
13
12
  def call
14
13
  options = parse!
15
- slim_file_paths = @argv
16
-
17
14
  Rainbow.enabled = options[:color] if options.key?(:color)
15
+ rubocop_config = RuboCopConfigGenerator.new(additional_config_file_path: options[:additional_config_file_path]).call
16
+ file_paths = PathFinder.new(patterns: @argv).call
18
17
 
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 }
18
+ offenses = file_paths.flat_map do |file_path|
19
+ source = ::File.read(file_path)
20
+ offenses_ = investigate(
21
+ auto_correct: options[:auto_correct],
22
+ file_path: file_path,
23
+ rubocop_config: rubocop_config,
24
+ source: source
25
+ )
26
+ if options[:auto_correct]
27
+ correct(
28
+ file_path: file_path,
29
+ offenses: offenses_,
30
+ source: source
31
+ )
32
+ end
33
+ offenses_
34
+ end
22
35
  report(offenses)
23
36
  exit(offenses.empty? ? 0 : 1)
24
37
  end
25
38
 
26
39
  private
27
40
 
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
41
+ # @param [String] file_path
42
+ # @param [Array<Slimcop::Offense>] offenses
43
+ # @param [String] source
44
+ def correct(file_path:, offenses:, source:)
45
+ rewritten_source = SlimCorrector.new(
46
+ file_path: file_path,
47
+ offenses: offenses,
48
+ source: source
49
+ ).call
50
+ ::File.write(file_path, rewritten_source)
38
51
  end
39
52
 
40
53
  # @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
+ # @param [String] file_path
55
+ # @param [String] rubocop_config
56
+ # @param [String] source
57
+ # @return [Array<Slimcop::Offense>]
58
+ def investigate(auto_correct:, file_path:, rubocop_config:, source:)
59
+ SlimOffenseCollector.new(
60
+ auto_correct: auto_correct,
61
+ file_path: file_path,
62
+ rubocop_config: rubocop_config,
63
+ source: source
64
+ ).call
54
65
  end
55
66
 
56
67
  # @param [Array<Slimcop::Offense>] offenses
@@ -74,6 +85,9 @@ module Slimcop
74
85
  parser.on('-a', '--auto-correct', 'Auto-correct offenses.') do
75
86
  options[:auto_correct] = true
76
87
  end
88
+ parser.on('-c', '--config=', 'Specify configuration file.') do |file_path|
89
+ options[:additional_config_file_path] = file_path
90
+ end
77
91
  parser.on('--[no-]color', 'Force color output on or off.') do |value|
78
92
  options[:color] = value
79
93
  end
@@ -4,6 +4,9 @@ require 'parser'
4
4
 
5
5
  module Slimcop
6
6
  class Offense
7
+ # @return [String]
8
+ attr_reader :file_path
9
+
7
10
  # @return [Integer]
8
11
  attr_reader :offset
9
12
 
@@ -13,7 +16,8 @@ module Slimcop
13
16
  # @param [Integer] offset
14
17
  # @param [RuboCop::Cop::Offense] rubocop_offense
15
18
  # @param [String] source Slim code.
16
- def initialize(offset:, rubocop_offense:, source:)
19
+ def initialize(file_path:, offset:, rubocop_offense:, source:)
20
+ @file_path = file_path
17
21
  @offset = offset
18
22
  @rubocop_offense = rubocop_offense
19
23
  @source = source
@@ -24,11 +28,6 @@ module Slimcop
24
28
  @rubocop_offense.corrector
25
29
  end
26
30
 
27
- # @return [String]
28
- def file_path
29
- @rubocop_offense.location.source_buffer.name
30
- end
31
-
32
31
  # @return [Integer]
33
32
  def line
34
33
  range.line
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ module Slimcop
6
+ # Collect file paths from given path patterns.
7
+ class PathFinder
8
+ # @param [Array<String>] patterns Patterns normally given as CLI arguments (e.g. `["app/views/**/*.html.slim"]`).
9
+ def initialize(patterns:)
10
+ @patterns = patterns
11
+ end
12
+
13
+ # @return [Array<String>]
14
+ def call
15
+ @patterns.flat_map do |pattern|
16
+ ::Pathname.glob(pattern).select(&:file?).map(&:to_s)
17
+ end.sort
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module Slimcop
6
+ class RuboCopConfigGenerator
7
+ # @param [String] additional_config_file_path
8
+ def initialize(additional_config_file_path: nil)
9
+ @additional_config_file_path = additional_config_file_path
10
+ end
11
+
12
+ # @return [RuboCop::Config]
13
+ def call
14
+ ::RuboCop::ConfigLoader.merge_with_default(merged_config, loaded_path)
15
+ end
16
+
17
+ private
18
+
19
+ # @return [String]
20
+ def loaded_path
21
+ @additional_config_file_path || slimcop_default_config_file_path
22
+ end
23
+
24
+ # @return [RuboCop::Config]
25
+ def merged_config
26
+ ::RuboCop::Config.create(merged_config_hash, loaded_path)
27
+ end
28
+
29
+ # @return [Hash]
30
+ def merged_config_hash
31
+ result = slimcop_default_config
32
+ result = ::RuboCop::ConfigLoader.merge(result, additional_config) if @additional_config_file_path
33
+ result
34
+ end
35
+
36
+ # @return [RuboCop::Config, nil]
37
+ def additional_config
38
+ ::RuboCop::ConfigLoader.load_file(@additional_config_file_path) if @additional_config_file_path
39
+ end
40
+
41
+ # @return [RuboCop::Config]
42
+ def slimcop_default_config
43
+ ::RuboCop::ConfigLoader.load_file(slimcop_default_config_file_path)
44
+ end
45
+
46
+ # @return [String]
47
+ def slimcop_default_config_file_path
48
+ @slimcop_default_config_file_path ||= ::File.expand_path('../../default.yml', __dir__)
49
+ end
50
+ end
51
+ end
@@ -5,8 +5,10 @@ require 'slimi'
5
5
  module Slimcop
6
6
  # Extract codes from Slim source.
7
7
  class RubyExtractor
8
+ # @param [String, nil] file_path
8
9
  # @param [String] source
9
- def initialize(source:)
10
+ def initialize(file_path:, source:)
11
+ @file_path = file_path
10
12
  @source = source
11
13
  end
12
14
 
@@ -26,7 +28,7 @@ module Slimcop
26
28
  # @return [Array] Slim AST, represented in S-expression.
27
29
  def ast
28
30
  ::Slimi::Filters::Interpolation.new.call(
29
- ::Slimi::Parser.new.call(@source)
31
+ ::Slimi::Parser.new(file: @file_path).call(@source)
30
32
  )
31
33
  end
32
34
 
@@ -16,9 +16,10 @@ module Slimcop
16
16
  @source = source
17
17
  end
18
18
 
19
- # @return [Array<RuboCop::Cop::Offense>, nil]
19
+ # @return [Array<RuboCop::Cop::Offense>]
20
20
  def call
21
- return unless rubocop_processed_source.valid_syntax?
21
+ # Skip if invalid syntax Ruby code is given. (e.g. "- if a?")
22
+ return [] unless rubocop_processed_source.valid_syntax?
22
23
 
23
24
  rubocop_team.investigate(rubocop_processed_source).offenses
24
25
  end
@@ -24,7 +24,7 @@ module Slimcop
24
24
 
25
25
  # @return [Array<Proc>]
26
26
  def corrections
27
- @offenses.map do |offense|
27
+ @offenses.select(&:corrector).map do |offense|
28
28
  lambda do |corrector|
29
29
  corrector.import!(offense.corrector, offset: offense.offset)
30
30
  end
@@ -24,6 +24,7 @@ module Slimcop
24
24
  source: snippet[:code]
25
25
  ).call.map do |rubocop_offense|
26
26
  Offense.new(
27
+ file_path: @file_path,
27
28
  offset: snippet[:begin_],
28
29
  rubocop_offense: rubocop_offense,
29
30
  source: @source
@@ -36,7 +37,10 @@ module Slimcop
36
37
 
37
38
  # @return [Array<Hash>]
38
39
  def snippets
39
- RubyExtractor.new(source: @source).call
40
+ RubyExtractor.new(
41
+ file_path: @file_path,
42
+ source: @source
43
+ ).call
40
44
  end
41
45
  end
42
46
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slimcop
4
- VERSION = '0.2.0'
4
+ VERSION = '0.6.0'
5
5
  end
data/lib/slimcop.rb CHANGED
@@ -4,9 +4,10 @@ require_relative 'slimcop/version'
4
4
 
5
5
  module Slimcop
6
6
  autoload :Cli, 'slimcop/cli'
7
- autoload :Configuration, 'slimcop/configuration'
8
7
  autoload :Formatter, 'slimcop/formatter'
9
8
  autoload :Offense, 'slimcop/offense'
9
+ autoload :PathFinder, 'slimcop/path_finder'
10
+ autoload :RuboCopConfigGenerator, 'slimcop/rubo_cop_config_generator'
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.1.1'
36
+ spec.add_dependency 'slimi', '>= 0.4'
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.2.0
4
+ version: 0.6.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-21 00:00:00.000000000 Z
11
+ date: 2021-12-27 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.1.1
47
+ version: '0.4'
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.1.1
54
+ version: '0.4'
55
55
  description:
56
56
  email:
57
57
  - r7kamura@gmail.com
@@ -75,9 +75,10 @@ files:
75
75
  - exe/slimcop
76
76
  - lib/slimcop.rb
77
77
  - lib/slimcop/cli.rb
78
- - lib/slimcop/configuration.rb
79
78
  - lib/slimcop/formatter.rb
80
79
  - lib/slimcop/offense.rb
80
+ - lib/slimcop/path_finder.rb
81
+ - lib/slimcop/rubo_cop_config_generator.rb
81
82
  - lib/slimcop/ruby_extractor.rb
82
83
  - lib/slimcop/ruby_offense_collector.rb
83
84
  - 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