slimcop 0.12.0 → 0.15.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: dd949a834535638a785282759fe46fb6fd368282d3030d17a113fa5253755bd7
4
- data.tar.gz: 70be88bc581d8ba538879cffabcef3c58e21bbddda4ce8c7812c32e465ab5108
3
+ metadata.gz: 617ae8055617ba19a3af9753558a3c2a20902caafcb3bbcf77ccd948b63b29f4
4
+ data.tar.gz: a16fb3fdd66b5886295411a0ae0235e6506f168ce3367b75926d729a6f358409
5
5
  SHA512:
6
- metadata.gz: 377b550717a43db56aefd5c66eb78622e2945c93436a45de5c50b8c9a61312a9d62d3e37d25108238788a5c08e0a4964dcc0401bd34531a620f02e62917091d3
7
- data.tar.gz: db3cb5e5f9bf3ea5cf56d0a3e15bd430907721b2d33dd7d0e59bb2bd65e2d1fe78d8e51aee8ada603c8b92142694dc415921ce16e7e08e70633f918ef8ea830f
6
+ metadata.gz: 8d6582adaecdd291cb2954b7b1c51066d3687a885874d966b3246c785fe8c7a1efba37f2ad32c9aa5b00fe4a03f38d10b5c4e2480bfe46539dbdbff50314d481
7
+ data.tar.gz: ad8eca7fb4d0fd1e9bd7faf4246f465d94f0267f34f2a79a77e305543b1ec2eae2fcc0f31cad39fa27352d9b6e685c5cffa0448ad96ed24b17d3da6a63980622
data/CHANGELOG.md CHANGED
@@ -2,6 +2,30 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.15.0 - 2022-01-22
6
+
7
+ ### Changed
8
+
9
+ - Disable Style/NestedTernaryOperator by default.
10
+
11
+ ## 0.14.0 - 2022-01-22
12
+
13
+ ### Changed
14
+
15
+ - Disable Style/MultilineTernaryOperator by default.
16
+
17
+ ## 0.13.1 - 2022-01-18
18
+
19
+ ### Fixed
20
+
21
+ - Fix missing some metadata in gemspec.
22
+
23
+ ## 0.13.0 - 2022-01-15
24
+
25
+ ### Changed
26
+
27
+ - Use templatecop.
28
+
5
29
  ## 0.12.0 - 2022-01-12
6
30
 
7
31
  ### Changed
data/Gemfile.lock CHANGED
@@ -1,10 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimcop (0.12.0)
5
- rainbow
6
- rubocop (>= 0.87)
4
+ slimcop (0.15.0)
7
5
  slimi (>= 0.5.1)
6
+ templatecop
8
7
 
9
8
  GEM
10
9
  remote: https://rubygems.org/
@@ -45,10 +44,14 @@ GEM
45
44
  rubocop-rspec (2.6.0)
46
45
  rubocop (~> 1.19)
47
46
  ruby-progressbar (1.11.0)
48
- slimi (0.7.0)
47
+ slimi (0.7.1)
49
48
  temple
50
49
  thor
51
50
  tilt
51
+ templatecop (0.2.0)
52
+ parallel
53
+ parser
54
+ rubocop (>= 0.87)
52
55
  temple (0.8.2)
53
56
  thor (1.2.1)
54
57
  tilt (2.0.10)
data/default.yml CHANGED
@@ -103,6 +103,12 @@ Style/IdenticalConditionalBranches:
103
103
  Style/IfUnlessModifier:
104
104
  Enabled: false
105
105
 
106
+ Style/MultilineTernaryOperator:
107
+ Enabled: false
108
+
109
+ Style/NestedTernaryOperator:
110
+ Enabled: false
111
+
106
112
  Style/Next:
107
113
  Enabled: false
108
114
 
data/exe/slimcop CHANGED
@@ -3,5 +3,12 @@
3
3
 
4
4
  $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
5
5
  require 'slimcop'
6
+ require 'templatecop'
6
7
 
7
- Slimcop::Cli.new(ARGV).call
8
+ Templatecop::Cli.call(
9
+ default_configuration_path: File.expand_path('../default.yml', __dir__),
10
+ default_path_patterns: %w[**/*.slim],
11
+ executable_name: 'slimcop',
12
+ implicit_configuration_paths: %w[.slimcop.yml .rubocop.yml],
13
+ ruby_extractor: Slimcop::RubyExtractor
14
+ )
@@ -1,10 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'slimi'
4
+ require 'templatecop'
4
5
 
5
6
  module Slimcop
6
7
  # Extract Ruby codes from Slim source.
7
8
  class RubyExtractor
9
+ class << self
10
+ # @param [String, nil] file_path
11
+ # @param [String] source
12
+ def call(
13
+ file_path:,
14
+ source:
15
+ )
16
+ new(
17
+ file_path: file_path,
18
+ source: source
19
+ ).call
20
+ end
21
+ end
22
+
8
23
  # @param [String, nil] file_path
9
24
  # @param [String] source
10
25
  def initialize(file_path:, source:)
@@ -15,7 +30,7 @@ module Slimcop
15
30
  # @return [Array<Hash>]
16
31
  def call
17
32
  ranges.map do |(begin_, end_)|
18
- clipped = RubyClipper.new(@source[begin_...end_]).call
33
+ clipped = ::Templatecop::RubyClipper.new(@source[begin_...end_]).call
19
34
  {
20
35
  code: clipped[:code],
21
36
  offset: begin_ + clipped[:offset]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slimcop
4
- VERSION = '0.12.0'
4
+ VERSION = '0.15.0'
5
5
  end
data/lib/slimcop.rb CHANGED
@@ -3,14 +3,5 @@
3
3
  require_relative 'slimcop/version'
4
4
 
5
5
  module Slimcop
6
- autoload :Cli, 'slimcop/cli'
7
- autoload :Offense, 'slimcop/offense'
8
- autoload :PathFinder, 'slimcop/path_finder'
9
- autoload :RuboCopConfigGenerator, 'slimcop/rubo_cop_config_generator'
10
- autoload :RubyClipper, 'slimcop/ruby_clipper'
11
6
  autoload :RubyExtractor, 'slimcop/ruby_extractor'
12
- autoload :RubyOffenseCollector, 'slimcop/ruby_offense_collector'
13
- autoload :Runner, 'slimcop/runner'
14
- autoload :SlimCorrector, 'slimcop/slim_corrector'
15
- autoload :SlimOffenseCollector, 'slimcop/slim_offense_collector'
16
7
  end
data/slimcop.gemspec CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.metadata['homepage_uri'] = spec.homepage
17
17
  spec.metadata['source_code_uri'] = spec.homepage
18
18
  spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+ spec.metadata['rubygems_mfa_required'] = 'true'
19
20
 
20
21
  # Specify which files should be added to the gem when it is released.
21
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -27,11 +28,7 @@ Gem::Specification.new do |spec|
27
28
  spec.bindir = 'exe'
28
29
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
30
  spec.require_paths = ['lib']
30
- spec.metadata = {
31
- 'rubygems_mfa_required' => 'true'
32
- }
33
31
 
34
- spec.add_dependency 'rainbow'
35
- spec.add_dependency 'rubocop', '>= 0.87'
36
32
  spec.add_dependency 'slimi', '>= 0.5.1'
33
+ spec.add_dependency 'templatecop'
37
34
  end
metadata CHANGED
@@ -1,57 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slimcop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.15.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: 2022-01-12 00:00:00.000000000 Z
11
+ date: 2022-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rainbow
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rubocop
14
+ name: slimi
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - ">="
32
18
  - !ruby/object:Gem::Version
33
- version: '0.87'
19
+ version: 0.5.1
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
24
  - - ">="
39
25
  - !ruby/object:Gem::Version
40
- version: '0.87'
26
+ version: 0.5.1
41
27
  - !ruby/object:Gem::Dependency
42
- name: slimi
28
+ name: templatecop
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - ">="
46
32
  - !ruby/object:Gem::Version
47
- version: 0.5.1
33
+ version: '0'
48
34
  type: :runtime
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - ">="
53
39
  - !ruby/object:Gem::Version
54
- version: 0.5.1
40
+ version: '0'
55
41
  description:
56
42
  email:
57
43
  - r7kamura@gmail.com
@@ -74,22 +60,16 @@ files:
74
60
  - default.yml
75
61
  - exe/slimcop
76
62
  - lib/slimcop.rb
77
- - lib/slimcop/cli.rb
78
- - lib/slimcop/offense.rb
79
- - lib/slimcop/path_finder.rb
80
- - lib/slimcop/rubo_cop_config_generator.rb
81
- - lib/slimcop/ruby_clipper.rb
82
63
  - lib/slimcop/ruby_extractor.rb
83
- - lib/slimcop/ruby_offense_collector.rb
84
- - lib/slimcop/runner.rb
85
- - lib/slimcop/slim_corrector.rb
86
- - lib/slimcop/slim_offense_collector.rb
87
64
  - lib/slimcop/version.rb
88
65
  - slimcop.gemspec
89
66
  homepage: https://github.com/r7kamura/slimcop
90
67
  licenses:
91
68
  - MIT
92
69
  metadata:
70
+ homepage_uri: https://github.com/r7kamura/slimcop
71
+ source_code_uri: https://github.com/r7kamura/slimcop
72
+ changelog_uri: https://github.com/r7kamura/slimcop/blob/main/CHANGELOG.md
93
73
  rubygems_mfa_required: 'true'
94
74
  post_install_message:
95
75
  rdoc_options: []
data/lib/slimcop/cli.rb DELETED
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rainbow'
4
- require 'rubocop'
5
-
6
- module Slimcop
7
- class Cli
8
- def initialize(argv)
9
- @argv = argv.dup
10
- end
11
-
12
- def call
13
- options = parse!
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
17
-
18
- offenses = Runner.new(
19
- auto_correct: options[:auto_correct],
20
- file_paths: file_paths,
21
- formatter: formatter,
22
- rubocop_config: rubocop_config
23
- ).call
24
-
25
- exit(offenses.empty? ? 0 : 1)
26
- end
27
-
28
- private
29
-
30
- # @return [Hash]
31
- def parse!
32
- options = {}
33
- parser = ::OptionParser.new
34
- parser.banner = 'Usage: slimcop [options] [file1, file2, ...]'
35
- parser.version = VERSION
36
- parser.on('-a', '--auto-correct', 'Auto-correct offenses.') do
37
- options[:auto_correct] = true
38
- end
39
- parser.on('-c', '--config=', 'Specify configuration file. (default: .slimcop.yml or .rubocop.yml)') do |file_path|
40
- options[:additional_config_file_path] = file_path
41
- end
42
- parser.on('--[no-]color', 'Force color output on or off.') do |value|
43
- options[:color] = value
44
- end
45
- parser.parse!(@argv)
46
- options
47
- end
48
- end
49
- end
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
-
5
- require 'parser'
6
- require 'rubocop'
7
-
8
- module Slimcop
9
- class Offense
10
- extend ::Forwardable
11
-
12
- # @return [String]
13
- attr_reader :file_path
14
-
15
- # @return [Integer]
16
- attr_reader :offset
17
-
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
- )
33
-
34
- # @param [Integer] offset
35
- # @param [RuboCop::Cop::Offense] rubocop_offense
36
- # @param [String] source Slim code.
37
- def initialize(file_path:, offset:, rubocop_offense:, source:)
38
- @file_path = file_path
39
- @offset = offset
40
- @rubocop_offense = rubocop_offense
41
- @source = source
42
- end
43
-
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
- )
51
- end
52
-
53
- private
54
-
55
- # @return [Parser::Source::Buffer]
56
- def buffer
57
- ::Parser::Source::Buffer.new(
58
- file_path,
59
- source: @source
60
- )
61
- end
62
-
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
72
- )
73
- end
74
- end
75
- end
@@ -1,35 +0,0 @@
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
- DEFAULT_PATH_PATTERNS = %w[
9
- **/*.slim
10
- ].freeze
11
-
12
- # @param [Array<String>] patterns Patterns normally given as CLI arguments (e.g. `["app/views/**/*.html.slim"]`).
13
- def initialize(patterns:)
14
- @patterns = patterns
15
- end
16
-
17
- # @return [Array<String>]
18
- def call
19
- patterns.flat_map do |pattern|
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
32
- end
33
- end
34
- end
35
- end
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rubocop'
4
-
5
- module Slimcop
6
- class RuboCopConfigGenerator
7
- DEFAULT_ADDITIONAL_CONFIG_PATH1 = '.slimcop.yml'
8
- DEFAULT_ADDITIONAL_CONFIG_PATH2 = '.rubocop.yml'
9
-
10
- # @param [String] additional_config_file_path
11
- def initialize(additional_config_file_path: nil)
12
- @additional_config_file_path = additional_config_file_path
13
- end
14
-
15
- # @return [RuboCop::Config]
16
- def call
17
- ::RuboCop::ConfigLoader.merge_with_default(merged_config, loaded_path)
18
- end
19
-
20
- private
21
-
22
- # @return [String]
23
- def loaded_path
24
- @additional_config_file_path || slimcop_default_config_file_path
25
- end
26
-
27
- # @return [RuboCop::Config]
28
- def merged_config
29
- ::RuboCop::Config.create(merged_config_hash, loaded_path)
30
- end
31
-
32
- # @return [Hash]
33
- def merged_config_hash
34
- result = slimcop_default_config
35
- result = ::RuboCop::ConfigLoader.merge(result, additional_config) if additional_config
36
- result
37
- end
38
-
39
- # @return [RuboCop::Config, nil]
40
- def additional_config
41
- if instance_variable_defined?(:@additional_config)
42
- @additional_config
43
- else
44
- @additional_config = \
45
- if @additional_config_file_path
46
- ::RuboCop::ConfigLoader.load_file(@additional_config_file_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)
51
- end
52
- end
53
- end
54
-
55
- # @return [RuboCop::Config]
56
- def slimcop_default_config
57
- ::RuboCop::ConfigLoader.load_file(slimcop_default_config_file_path)
58
- end
59
-
60
- # @return [String]
61
- def slimcop_default_config_file_path
62
- @slimcop_default_config_file_path ||= ::File.expand_path('../../default.yml', __dir__)
63
- end
64
- end
65
- end
@@ -1,93 +0,0 @@
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
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rubocop'
4
-
5
- module Slimcop
6
- # Collect RuboCop offenses from Ruby code.
7
- class RubyOffenseCollector
8
- # @param [Boolean] auto_correct
9
- # @param [String] file_path
10
- # @param [RuboCop::Config] rubocop_config
11
- # @param [String] source
12
- def initialize(auto_correct:, file_path:, rubocop_config:, source:)
13
- @auto_correct = auto_correct
14
- @file_path = file_path
15
- @rubocop_config = rubocop_config
16
- @source = source
17
- end
18
-
19
- # @return [Array<RuboCop::Cop::Offense>]
20
- def call
21
- # Skip if invalid syntax Ruby code is given. (e.g. "- if a?")
22
- return [] unless rubocop_processed_source.valid_syntax?
23
-
24
- rubocop_team.investigate(rubocop_processed_source).offenses.reject(&:disabled?)
25
- end
26
-
27
- private
28
-
29
- # @return [RuboCop::ProcessedSource]
30
- def rubocop_processed_source
31
- @rubocop_processed_source ||= ::RuboCop::ProcessedSource.new(
32
- @source,
33
- @rubocop_config.target_ruby_version,
34
- @file_path
35
- )
36
- end
37
-
38
- # @return [RuboCop::Cop::Team]
39
- def rubocop_team
40
- ::RuboCop::Cop::Team.new(
41
- ::RuboCop::Cop::Registry.new(::RuboCop::Cop::Cop.all),
42
- @rubocop_config,
43
- auto_correct: @auto_correct,
44
- display_cop_names: true,
45
- extra_details: true,
46
- stdin: ''
47
- )
48
- end
49
- end
50
- end
@@ -1,115 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Slimcop
4
- # Run investigation and auto-correcttion.
5
- class Runner
6
- # @param [Boolean] auto_correct
7
- # @param [Array<String>] file_paths
8
- # @param [Object] formatter
9
- # @param [RuboCop::Config] rubocop_config
10
- def initialize(
11
- auto_correct:,
12
- file_paths:,
13
- formatter:,
14
- rubocop_config:
15
- )
16
- @auto_correct = auto_correct
17
- @file_paths = file_paths
18
- @formatter = formatter
19
- @rubocop_config = rubocop_config
20
- end
21
-
22
- # @return [Array<RuboCop::Cop::Offense>]
23
- def call
24
- on_started
25
- offenses = investigate_and_correct
26
- on_finished
27
- offenses
28
- end
29
-
30
- private
31
-
32
- # @param [String] file_path
33
- # @param [Array<Slimcop::Offense>] offenses
34
- # @param [String] source
35
- def correct(file_path:, offenses:, source:)
36
- rewritten_source = SlimCorrector.new(
37
- file_path: file_path,
38
- offenses: offenses,
39
- source: source
40
- ).call
41
- ::File.write(file_path, rewritten_source)
42
- end
43
-
44
- # @param [Boolean] auto_correct
45
- # @param [String] file_path
46
- # @param [String] rubocop_config
47
- # @param [String] source
48
- # @return [Array<Slimcop::Offense>]
49
- def investigate(auto_correct:, file_path:, rubocop_config:, source:)
50
- SlimOffenseCollector.new(
51
- auto_correct: auto_correct,
52
- file_path: file_path,
53
- rubocop_config: rubocop_config,
54
- source: source
55
- ).call
56
- end
57
-
58
- # @return [Array<RuboCop::Cop::Offense>]
59
- def investigate_and_correct
60
- @file_paths.flat_map do |file_path|
61
- offenses_per_file = []
62
- max_trials_count.times do
63
- on_file_started(file_path)
64
- source = ::File.read(file_path)
65
- offenses = investigate(
66
- auto_correct: @auto_correct,
67
- file_path: file_path,
68
- rubocop_config: @rubocop_config,
69
- source: source
70
- )
71
- offenses_per_file += offenses
72
- break if offenses.empty?
73
-
74
- next unless @auto_correct
75
-
76
- correct(
77
- file_path: file_path,
78
- offenses: offenses,
79
- source: source
80
- )
81
- end
82
- on_file_finished(file_path, offenses_per_file)
83
- offenses_per_file
84
- end
85
- end
86
-
87
- # @return [Integer]
88
- def max_trials_count
89
- if @auto_correct
90
- 7 # What a heuristic number.
91
- else
92
- 1
93
- end
94
- end
95
-
96
- def on_started
97
- @formatter.started(@file_paths)
98
- end
99
-
100
- # @param [String] file_path
101
- def on_file_started(file_path)
102
- @formatter.file_started(file_path, {})
103
- end
104
-
105
- # @param [String] file_path
106
- # @param [Array<RuboCop::Cop::Offenses]
107
- def on_file_finished(file_path, offenses)
108
- @formatter.file_finished(file_path, offenses)
109
- end
110
-
111
- def on_finished
112
- @formatter.finished(@file_paths)
113
- end
114
- end
115
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'parser'
4
- require 'rubocop/cop/legacy/corrector'
5
-
6
- module Slimcop
7
- # Apply auto-corrections to Slim file.
8
- class SlimCorrector
9
- # @param [String] file_path
10
- # @param [Array<Slimcop::Offense>] offenses
11
- # @param [String] source
12
- def initialize(file_path:, offenses:, source:)
13
- @file_path = file_path
14
- @offenses = offenses
15
- @source = source
16
- end
17
-
18
- # @return [String] Rewritten Slim code.
19
- def call
20
- ::RuboCop::Cop::Legacy::Corrector.new(source_buffer, corrections).rewrite
21
- end
22
-
23
- private
24
-
25
- # @return [Array<Proc>]
26
- def corrections
27
- @offenses.select(&:corrector).map do |offense|
28
- lambda do |corrector|
29
- corrector.import!(offense.corrector, offset: offense.offset)
30
- end
31
- end
32
- end
33
-
34
- # @return [Parser::Source::Buffer]
35
- def source_buffer
36
- ::Parser::Source::Buffer.new(
37
- @file_path,
38
- source: @source
39
- )
40
- end
41
- end
42
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Slimcop
4
- # Collect RuboCop offenses from Slim code.
5
- class SlimOffenseCollector
6
- # @param [Boolean] auto_correct
7
- # @param [String] file_path Slim file path
8
- # @param [RuboCop::Config] rubocop_config
9
- # @param [String] source Slim code
10
- def initialize(auto_correct:, file_path:, rubocop_config:, source:)
11
- @auto_correct = auto_correct
12
- @file_path = file_path
13
- @rubocop_config = rubocop_config
14
- @source = source
15
- end
16
-
17
- # @return [Array<Slimcop::Offense>]
18
- def call
19
- snippets.flat_map do |snippet|
20
- RubyOffenseCollector.new(
21
- auto_correct: @auto_correct,
22
- file_path: @file_path,
23
- rubocop_config: @rubocop_config,
24
- source: snippet[:code]
25
- ).call.map do |rubocop_offense|
26
- Offense.new(
27
- file_path: @file_path,
28
- offset: snippet[:offset],
29
- rubocop_offense: rubocop_offense,
30
- source: @source
31
- )
32
- end
33
- end
34
- end
35
-
36
- private
37
-
38
- # @return [Array<Hash>]
39
- def snippets
40
- RubyExtractor.new(
41
- file_path: @file_path,
42
- source: @source
43
- ).call
44
- end
45
- end
46
- end