slimcop 0.10.0 → 0.13.1

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: 02e411169bcf03ec8058dab2f6c316188b78cfda2ee5b10839f2bc89e0ffbcb7
4
- data.tar.gz: 3081e815ee3f7ce7a518ab6a7b384042d4d96eda53bb8c5c776f3cbe21cd90e2
3
+ metadata.gz: 7940460cb1e32440204359ed19e93fd6b767edeb88733f2bd27cd419a012bf85
4
+ data.tar.gz: 1ff28299d0f05f3cb270a051467cfcd4c53fbf5df8b66545742778fe803e6e61
5
5
  SHA512:
6
- metadata.gz: adcd201a0fb01c9b40ec857f341bedec643648b463313433558bbc26564b2dc50742d771f68e40d8b9a4a829785e4e49cbbcda2d2ca147b94e8e61a9f5d3f183
7
- data.tar.gz: 484b00bcf0e97e8980216cb70b0cd4badf5075eaf542f59657ceac2aa8de598c0059a58091210ee69c34bc09fdbe6df9ffc5d666169ccfa3da24a7a81bd10d44
6
+ metadata.gz: cf85bc72298ceec46aa702ed31c914659095fe6323cb1fc97890ea8bb06f416e16182d9243a0d036ca9d3b51d29ac204d60a33ba2a676c73fee7773ef889ffff
7
+ data.tar.gz: 5c9c05d471956877ad11a9522d8ee247169cb9e02a5ae69cc8261d309114fce4c5bf88922ae849a0228f5be2f86d5fcf3281a344ca44a3ad37eaa6f86f2a059e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,30 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.13.1 - 2022-01-18
6
+
7
+ ### Fixed
8
+
9
+ - Fix missing some metadata in gemspec.
10
+
11
+ ## 0.13.0 - 2022-01-15
12
+
13
+ ### Changed
14
+
15
+ - Use templatecop.
16
+
17
+ ## 0.12.0 - 2022-01-12
18
+
19
+ ### Changed
20
+
21
+ - Keep trying auto-correction up to 7 times until no offense detected.
22
+
23
+ ## 0.11.0 - 2022-01-10
24
+
25
+ ### Changed
26
+
27
+ - Detect offenses from code that containing `if`, `unless`, `do`, etc.
28
+
5
29
  ## 0.10.0 - 2022-01-06
6
30
 
7
31
  ### Added
data/Gemfile.lock CHANGED
@@ -1,10 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimcop (0.10.0)
5
- rainbow
6
- rubocop (>= 0.87)
4
+ slimcop (0.13.1)
7
5
  slimi (>= 0.5.1)
6
+ templatecop
8
7
 
9
8
  GEM
10
9
  remote: https://rubygems.org/
@@ -49,6 +48,9 @@ GEM
49
48
  temple
50
49
  thor
51
50
  tilt
51
+ templatecop (0.1.0)
52
+ parser
53
+ rubocop (>= 0.87)
52
54
  temple (0.8.2)
53
55
  thor (1.2.1)
54
56
  tilt (2.0.10)
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.10.0'
4
+ VERSION = '0.13.1'
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
@@ -27,11 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.bindir = 'exe'
28
28
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
29
  spec.require_paths = ['lib']
30
- spec.metadata = {
31
- 'rubygems_mfa_required' => 'true'
32
- }
33
30
 
34
- spec.add_dependency 'rainbow'
35
- spec.add_dependency 'rubocop', '>= 0.87'
36
31
  spec.add_dependency 'slimi', '>= 0.5.1'
32
+ spec.add_dependency 'templatecop'
37
33
  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.10.0
4
+ version: 0.13.1
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-05 00:00:00.000000000 Z
11
+ date: 2022-01-18 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,21 +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
63
  - lib/slimcop/ruby_extractor.rb
82
- - lib/slimcop/ruby_offense_collector.rb
83
- - lib/slimcop/slim_corrector.rb
84
- - lib/slimcop/slim_offense_collector.rb
85
64
  - lib/slimcop/version.rb
86
65
  - slimcop.gemspec
87
66
  homepage: https://github.com/r7kamura/slimcop
88
67
  licenses:
89
68
  - MIT
90
69
  metadata:
91
- rubygems_mfa_required: 'true'
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
92
73
  post_install_message:
93
74
  rdoc_options: []
94
75
  require_paths:
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: .slimcop.yml or .rubocop.yml)') 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,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,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,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[: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