slimcop 0.11.0 → 0.12.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: 4b967da47ab7a5721d082f4d1a4e9bb84006431c5acb5992a27f8246bdcfd904
4
- data.tar.gz: 6e260866461bc31ddef835279d647905f17f87890b63af229bd98557699e0d8f
3
+ metadata.gz: dd949a834535638a785282759fe46fb6fd368282d3030d17a113fa5253755bd7
4
+ data.tar.gz: 70be88bc581d8ba538879cffabcef3c58e21bbddda4ce8c7812c32e465ab5108
5
5
  SHA512:
6
- metadata.gz: 62cbf9db3a9f0fc741714afdd4f886b61efb4bed1e3f4ca54e6e7caa03b3cf2e8f3141aec2456b3b13f7fe0999e5a7ab9a7578a6700225a220db08200f9b2f96
7
- data.tar.gz: c6742245998d9fbd23dca47dbf55be3e8717722daa7515df5afab77baca05d1c2ddc26d80a62bbbc97a7cfa06332a5c6fb3bc078a7d3853479598f7cb86c36ea
6
+ metadata.gz: 377b550717a43db56aefd5c66eb78622e2945c93436a45de5c50b8c9a61312a9d62d3e37d25108238788a5c08e0a4964dcc0401bd34531a620f02e62917091d3
7
+ data.tar.gz: db3cb5e5f9bf3ea5cf56d0a3e15bd430907721b2d33dd7d0e59bb2bd65e2d1fe78d8e51aee8ada603c8b92142694dc415921ce16e7e08e70633f918ef8ea830f
data/CHANGELOG.md CHANGED
@@ -2,11 +2,16 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.12.0 - 2022-01-12
6
+
7
+ ### Changed
8
+
9
+ - Keep trying auto-correction up to 7 times until no offense detected.
10
+
5
11
  ## 0.11.0 - 2022-01-10
6
12
 
7
13
  ### Changed
8
14
 
9
- - Use :offset instead of :begin_ at RubyExtractor.
10
15
  - Detect offenses from code that containing `if`, `unless`, `do`, etc.
11
16
 
12
17
  ## 0.10.0 - 2022-01-06
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimcop (0.11.0)
4
+ slimcop (0.12.0)
5
5
  rainbow
6
6
  rubocop (>= 0.87)
7
7
  slimi (>= 0.5.1)
data/lib/slimcop/cli.rb CHANGED
@@ -15,58 +15,18 @@ module Slimcop
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)
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)
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
+
39
25
  exit(offenses.empty? ? 0 : 1)
40
26
  end
41
27
 
42
28
  private
43
29
 
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)
54
- end
55
-
56
- # @param [Boolean] auto_correct
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
68
- end
69
-
70
30
  # @return [Hash]
71
31
  def parse!
72
32
  options = {}
@@ -0,0 +1,115 @@
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,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slimcop
4
- VERSION = '0.11.0'
4
+ VERSION = '0.12.0'
5
5
  end
data/lib/slimcop.rb CHANGED
@@ -10,6 +10,7 @@ module Slimcop
10
10
  autoload :RubyClipper, 'slimcop/ruby_clipper'
11
11
  autoload :RubyExtractor, 'slimcop/ruby_extractor'
12
12
  autoload :RubyOffenseCollector, 'slimcop/ruby_offense_collector'
13
+ autoload :Runner, 'slimcop/runner'
13
14
  autoload :SlimCorrector, 'slimcop/slim_corrector'
14
15
  autoload :SlimOffenseCollector, 'slimcop/slim_offense_collector'
15
16
  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.11.0
4
+ version: 0.12.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-10 00:00:00.000000000 Z
11
+ date: 2022-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rainbow
@@ -81,6 +81,7 @@ files:
81
81
  - lib/slimcop/ruby_clipper.rb
82
82
  - lib/slimcop/ruby_extractor.rb
83
83
  - lib/slimcop/ruby_offense_collector.rb
84
+ - lib/slimcop/runner.rb
84
85
  - lib/slimcop/slim_corrector.rb
85
86
  - lib/slimcop/slim_offense_collector.rb
86
87
  - lib/slimcop/version.rb