slimcop 0.9.1 → 0.13.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: 75b0d9431db2adbba5b1b238af9c11bdbf95528541136714f1d09acdb226048c
4
- data.tar.gz: 1bdfd90997739807a9ca4e38ad37bd29ae2d9f736fbaf0d74824aa367bbd5dd4
3
+ metadata.gz: 583b9f7c6ea2e90edb94a7ed4c84762881cac6937374dc747f55db4c4e6c3e58
4
+ data.tar.gz: fc8d197b9b6b469281a99d49b62a57cf6674895376d3f549ceb4fbb82bb24260
5
5
  SHA512:
6
- metadata.gz: a8bb9144ee99b9745fa1f6ae1c136ed7e438e6d433c22963ade6cb25120d4fb81780ee03f4051ed393b4d37ee7f33b5a74c603229a88f487f6646e094d13ea5f
7
- data.tar.gz: 1af01925da76e7116394f222ec7b9630e842e9be4d4d3dd71ff0a2e4480d0160e5e408c4675ef28dbbcbb04dc541a162d602341b9002098829eb24ced4946519
6
+ metadata.gz: 564ce43ebcf8fcd8d18a0834f98cc23a779de0bb1db57eb332a3694486ffd4ec567066a2b8e544c3ed2b3c42357ef27346fa19beff58f45b4bd5fcb0d902abf6
7
+ data.tar.gz: 33db301ef4145581fcff267e9def94f7ef3e6bd6f34e75f4e31e82605e9c58479b360429ef313db582fedda09f3b06121a4095f5b7d1c6ab139669bba25abc5b
data/CHANGELOG.md CHANGED
@@ -2,6 +2,30 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.13.0 - 2022-01-15
6
+
7
+ ### Changed
8
+
9
+ - Use templatecop.
10
+
11
+ ## 0.12.0 - 2022-01-12
12
+
13
+ ### Changed
14
+
15
+ - Keep trying auto-correction up to 7 times until no offense detected.
16
+
17
+ ## 0.11.0 - 2022-01-10
18
+
19
+ ### Changed
20
+
21
+ - Detect offenses from code that containing `if`, `unless`, `do`, etc.
22
+
23
+ ## 0.10.0 - 2022-01-06
24
+
25
+ ### Added
26
+
27
+ - Use .slimcop.yml as its 1st default config file.
28
+
5
29
  ## 0.9.1 - 2022-01-04
6
30
 
7
31
  ### Fixed
data/Gemfile.lock CHANGED
@@ -1,10 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimcop (0.9.1)
5
- rainbow
6
- rubocop (>= 0.87)
4
+ slimcop (0.13.0)
7
5
  slimi (>= 0.5.1)
6
+ templatecop
8
7
 
9
8
  GEM
10
9
  remote: https://rubygems.org/
@@ -45,10 +44,15 @@ GEM
45
44
  rubocop-rspec (2.6.0)
46
45
  rubocop (~> 1.19)
47
46
  ruby-progressbar (1.11.0)
48
- slimi (0.6.0)
47
+ slimi (0.7.0)
49
48
  temple
49
+ thor
50
50
  tilt
51
+ templatecop (0.1.0)
52
+ parser
53
+ rubocop (>= 0.87)
51
54
  temple (0.8.2)
55
+ thor (1.2.1)
52
56
  tilt (2.0.10)
53
57
  unicode-display_width (2.1.0)
54
58
 
data/README.md CHANGED
@@ -40,7 +40,7 @@ Usage: slimcop [options] [file1, file2, ...]
40
40
  ### Example
41
41
 
42
42
  ```console
43
- $ slimcop
43
+ $ slimcop 'spec/fixtures/**/*.slim'
44
44
  Inspecting 1 file
45
45
  C
46
46
 
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
- # Extract codes from Slim source.
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,10 +30,10 @@ module Slimcop
15
30
  # @return [Array<Hash>]
16
31
  def call
17
32
  ranges.map do |(begin_, end_)|
33
+ clipped = ::Templatecop::RubyClipper.new(@source[begin_...end_]).call
18
34
  {
19
- begin_: begin_,
20
- code: @source[begin_...end_],
21
- end_: end_
35
+ code: clipped[:code],
36
+ offset: begin_ + clipped[:offset]
22
37
  }
23
38
  end
24
39
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slimcop
4
- VERSION = '0.9.1'
4
+ VERSION = '0.13.0'
5
5
  end
data/lib/slimcop.rb CHANGED
@@ -3,12 +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
6
  autoload :RubyExtractor, 'slimcop/ruby_extractor'
11
- autoload :RubyOffenseCollector, 'slimcop/ruby_offense_collector'
12
- autoload :SlimCorrector, 'slimcop/slim_corrector'
13
- autoload :SlimOffenseCollector, 'slimcop/slim_offense_collector'
14
7
  end
data/slimcop.gemspec CHANGED
@@ -31,7 +31,6 @@ Gem::Specification.new do |spec|
31
31
  'rubygems_mfa_required' => 'true'
32
32
  }
33
33
 
34
- spec.add_dependency 'rainbow'
35
- spec.add_dependency 'rubocop', '>= 0.87'
36
34
  spec.add_dependency 'slimi', '>= 0.5.1'
35
+ spec.add_dependency 'templatecop'
37
36
  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.9.1
4
+ version: 0.13.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-03 00:00:00.000000000 Z
11
+ date: 2022-01-15 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,14 +60,8 @@ 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
63
  - lib/slimcop/ruby_extractor.rb
82
- - lib/slimcop/ruby_offense_collector.rb
83
64
  - lib/slimcop/slim_corrector.rb
84
- - lib/slimcop/slim_offense_collector.rb
85
65
  - lib/slimcop/version.rb
86
66
  - slimcop.gemspec
87
67
  homepage: https://github.com/r7kamura/slimcop
data/lib/slimcop/cli.rb DELETED
@@ -1,89 +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
- 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)
39
- exit(offenses.empty? ? 0 : 1)
40
- end
41
-
42
- private
43
-
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
- # @return [Hash]
71
- def parse!
72
- options = {}
73
- parser = ::OptionParser.new
74
- parser.banner = 'Usage: slimcop [options] [file1, file2, ...]'
75
- parser.version = VERSION
76
- parser.on('-a', '--auto-correct', 'Auto-correct offenses.') do
77
- options[:auto_correct] = true
78
- end
79
- parser.on('-c', '--config=', 'Specify configuration file. (default: .rubocop.yml if it exists)') do |file_path|
80
- options[:additional_config_file_path] = file_path
81
- end
82
- parser.on('--[no-]color', 'Force color output on or off.') do |value|
83
- options[:color] = value
84
- end
85
- parser.parse!(@argv)
86
- options
87
- end
88
- end
89
- 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,62 +0,0 @@
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
@@ -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,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[:begin_],
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