templatecop 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f45182757cc404cba39cb3bc1ab455fedfd4c0d66d2082279825015a6df89b3
4
- data.tar.gz: 31d1044762cac8c8996f760b5c047c396538033a05a4c6ac8334f9afbaa73359
3
+ metadata.gz: cec282db3c407d288d9bba6ac5122424c5f52a14096046fb469918821bfbdbc4
4
+ data.tar.gz: f977c7d4134cd2fa82a2c6b35cba52a6cfaf58eb5a614f58f41224c25abe1b43
5
5
  SHA512:
6
- metadata.gz: 13cad1ba40ae63a8604e66b64567f172e8f63e2094eb4d7baf4e3d43c708bcbcdfba35af46f67349666b8578a1bfba31126b82550c98ef54475fb9fafb923cd1
7
- data.tar.gz: 0ee17fa0e595a9abcf0355bf3992db7ee6b90e10883718fd25bdd1e2f4065069affb349b9b9727ce6b680a31f207e6bcdd05d87f74028280a70024b3d4d0af97
6
+ metadata.gz: 25b20e224432836b15eec614aee67fa722ce3dca5a61642f8bba5b52607890db83b230d993a0ed48665427f9eb53a1f7f44a893a0b398fd868e56f0c24516903
7
+ data.tar.gz: 82b810eae2b1ed05b1d461f249fac065c2129d6f041561926919d8d0fd6d54760a958ea4b8f727c637d57ceba6742abbcf6a256d6cb1fed0219834687e983278
data/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.4.0 - 2023-01-10
6
+
7
+ ### Added
8
+
9
+ - Add CLI switches for debug mode.
10
+ - Add directory support for PathFinder.
11
+
12
+ ### Fixed
13
+
14
+ - Add compatibility for RuboCop v1.38.0 and above.
15
+ - Display correct label when autocorrecting.
16
+ - Respect AllCops Exclude pattern.
17
+
5
18
  ## 0.3.0 - 2022-03-14
6
19
 
7
20
  ### Added
@@ -65,11 +65,13 @@ module Templatecop
65
65
  ).call
66
66
  file_paths = PathFinder.new(
67
67
  default_patterns: @default_path_patterns,
68
+ exclude_patterns: rubocop_config.for_all_cops['Exclude'],
68
69
  patterns: @argv
69
70
  ).call
70
71
 
71
72
  offenses = Runner.new(
72
- auto_correct: options[:auto_correct],
73
+ autocorrect: options[:autocorrect],
74
+ debug: options[:debug],
73
75
  file_paths: file_paths,
74
76
  formatter: formatter,
75
77
  rubocop_config: rubocop_config,
@@ -88,7 +90,7 @@ module Templatecop
88
90
  parser.banner = "Usage: #{@executable_name} [options] [file1, file2, ...]"
89
91
  parser.version = VERSION
90
92
  parser.on('-a', '--auto-correct', 'Auto-correct offenses.') do
91
- options[:auto_correct] = true
93
+ options[:autocorrect] = true
92
94
  end
93
95
  parser.on('-c', '--config=', "Specify configuration file. (default: #{@default_configuration_path} or .rubocop.yml)") do |file_path|
94
96
  options[:forced_configuration_path] = file_path
@@ -96,6 +98,9 @@ module Templatecop
96
98
  parser.on('--[no-]color', 'Force color output on or off.') do |value|
97
99
  options[:color] = value
98
100
  end
101
+ parser.on('-d', '--debug', 'Display debug info.') do |value|
102
+ options[:debug] = value
103
+ end
99
104
  parser.parse!(@argv)
100
105
  options
101
106
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'pathname'
4
+ require 'set'
4
5
 
5
6
  module Templatecop
6
7
  # Collect file paths from given path patterns.
@@ -9,29 +10,55 @@ module Templatecop
9
10
  # @param [Array<String>] patterns Patterns normally given as CLI arguments (e.g. `["app/views/**/*.html.template"]`).
10
11
  def initialize(
11
12
  default_patterns:,
13
+ exclude_patterns:,
12
14
  patterns:
13
15
  )
14
16
  @default_patterns = default_patterns
17
+ @exclude_patterns = exclude_patterns || []
15
18
  @patterns = patterns
16
19
  end
17
20
 
18
21
  # @return [Array<String>]
19
22
  def call
20
- patterns.flat_map do |pattern|
21
- ::Pathname.glob(pattern).select(&:file?).map do |pathname|
22
- pathname.expand_path.to_s
23
- end
24
- end.uniq.sort
23
+ matching_paths(patterns) do |path|
24
+ !excluded?(path)
25
+ end.sort
25
26
  end
26
27
 
27
28
  private
28
29
 
30
+ # @return [Set<String>]
31
+ def excluded
32
+ @excluded ||= matching_paths(@exclude_patterns)
33
+ end
34
+
35
+ # @return [TrueClass,FalseClass]
36
+ def excluded?(path)
37
+ excluded.include?(path)
38
+ end
39
+
40
+ # @return [Set<String>]
41
+ def matching_paths(patterns, &block)
42
+ patterns.each_with_object(Set.new) do |pattern, set|
43
+ ::Pathname.glob(pattern) do |pathname|
44
+ next unless pathname.file?
45
+
46
+ path = pathname.expand_path.to_s
47
+ set.add(path) if block.nil? || block.call(path)
48
+ end
49
+ end
50
+ end
51
+
29
52
  # @return [Array<String>]
30
53
  def patterns
31
- if @patterns.empty?
32
- @default_patterns
33
- else
34
- @patterns
54
+ return @default_patterns if @patterns.empty?
55
+
56
+ @patterns.map do |pattern|
57
+ next pattern unless File.directory?(pattern)
58
+
59
+ @default_patterns.map do |default|
60
+ File.join(pattern, default)
61
+ end.flatten
35
62
  end
36
63
  end
37
64
  end
@@ -5,12 +5,20 @@ require 'rubocop'
5
5
  module Templatecop
6
6
  # Collect RuboCop offenses from Ruby code.
7
7
  class RubyOffenseCollector
8
- # @param [Boolean] auto_correct
8
+ # @param [Boolean] autocorrect
9
+ # @param [Boolean] debug
9
10
  # @param [String] file_path
10
11
  # @param [RuboCop::Config] rubocop_config
11
12
  # @param [String] source
12
- def initialize(auto_correct:, file_path:, rubocop_config:, source:)
13
- @auto_correct = auto_correct
13
+ def initialize(
14
+ autocorrect:,
15
+ file_path:,
16
+ rubocop_config:,
17
+ source:,
18
+ debug: false
19
+ )
20
+ @autocorrect = autocorrect
21
+ @debug = debug
14
22
  @file_path = file_path
15
23
  @rubocop_config = rubocop_config
16
24
  @source = source
@@ -26,21 +34,39 @@ module Templatecop
26
34
 
27
35
  private
28
36
 
37
+ # @return [RuboCop::Cop::Registry]
38
+ def registry
39
+ @registry ||= begin
40
+ all_cops = if ::RuboCop::Cop::Registry.respond_to?(:all)
41
+ ::RuboCop::Cop::Registry.all
42
+ else
43
+ ::RuboCop::Cop::Cop.all
44
+ end
45
+
46
+ ::RuboCop::Cop::Registry.new(all_cops)
47
+ end
48
+ end
49
+
29
50
  # @return [RuboCop::ProcessedSource]
30
51
  def rubocop_processed_source
31
52
  @rubocop_processed_source ||= ::RuboCop::ProcessedSource.new(
32
53
  @source,
33
54
  @rubocop_config.target_ruby_version,
34
55
  @file_path
35
- )
56
+ ).tap do |processed_source|
57
+ processed_source.config = @rubocop_config if processed_source.respond_to?(:config)
58
+ processed_source.registry = registry if processed_source.respond_to?(:registry)
59
+ end
36
60
  end
37
61
 
38
62
  # @return [RuboCop::Cop::Team]
39
63
  def rubocop_team
40
64
  ::RuboCop::Cop::Team.new(
41
- ::RuboCop::Cop::Registry.new(::RuboCop::Cop::Cop.all),
65
+ registry,
42
66
  @rubocop_config,
43
- auto_correct: @auto_correct,
67
+ auto_correct: @autocorrect, # DEPRECATED
68
+ autocorrect: @autocorrect,
69
+ debug: @debug,
44
70
  display_cop_names: true,
45
71
  extra_details: true,
46
72
  stdin: ''
@@ -4,21 +4,24 @@ require 'parallel'
4
4
  require 'stringio'
5
5
 
6
6
  module Templatecop
7
- # Run investigation and auto-correcttion.
7
+ # Run investigation and auto-correction.
8
8
  class Runner
9
- # @param [Boolean] auto_correct
9
+ # @param [Boolean] autocorrect
10
+ # @param [Boolean] debug
10
11
  # @param [Array<String>] file_paths
11
12
  # @param [Object] formatter
12
13
  # @param [RuboCop::Config] rubocop_config
13
14
  # @param [#call] ruby_extractor
14
15
  def initialize(
15
- auto_correct:,
16
+ autocorrect:,
16
17
  file_paths:,
17
18
  formatter:,
18
19
  rubocop_config:,
19
- ruby_extractor:
20
+ ruby_extractor:,
21
+ debug: false
20
22
  )
21
- @auto_correct = auto_correct
23
+ @autocorrect = autocorrect
24
+ @debug = debug
22
25
  @file_paths = file_paths
23
26
  @formatter = formatter
24
27
  @rubocop_config = rubocop_config
@@ -49,14 +52,22 @@ module Templatecop
49
52
  ::File.write(file_path, rewritten_source)
50
53
  end
51
54
 
52
- # @param [Boolean] auto_correct
55
+ # @param [Boolean] autocorrect
56
+ # @param [Boolean] debug
53
57
  # @param [String] file_path
54
58
  # @param [String] rubocop_config
55
59
  # @param [String] source
56
60
  # @return [Array<Templatecop::Offense>]
57
- def investigate(auto_correct:, file_path:, rubocop_config:, source:)
61
+ def investigate(
62
+ autocorrect:,
63
+ debug:,
64
+ file_path:,
65
+ rubocop_config:,
66
+ source:
67
+ )
58
68
  TemplateOffenseCollector.new(
59
- auto_correct: auto_correct,
69
+ autocorrect: autocorrect,
70
+ debug: debug,
60
71
  file_path: file_path,
61
72
  rubocop_config: rubocop_config,
62
73
  ruby_extractor: @ruby_extractor,
@@ -72,7 +83,8 @@ module Templatecop
72
83
  on_file_started(file_path)
73
84
  source = ::File.read(file_path)
74
85
  offenses = investigate(
75
- auto_correct: @auto_correct,
86
+ autocorrect: @autocorrect,
87
+ debug: @debug,
76
88
  file_path: file_path,
77
89
  rubocop_config: @rubocop_config,
78
90
  source: source
@@ -80,7 +92,7 @@ module Templatecop
80
92
  offenses_per_file |= offenses
81
93
  break if offenses.select(&:correctable?).empty?
82
94
 
83
- next unless @auto_correct
95
+ next unless @autocorrect
84
96
 
85
97
  correct(
86
98
  file_path: file_path,
@@ -95,7 +107,7 @@ module Templatecop
95
107
 
96
108
  # @return [Integer]
97
109
  def max_trials_count
98
- if @auto_correct
110
+ if @autocorrect
99
111
  7 # What a heuristic number.
100
112
  else
101
113
  1
@@ -3,19 +3,22 @@
3
3
  module Templatecop
4
4
  # Collect RuboCop offenses from Template code.
5
5
  class TemplateOffenseCollector
6
- # @param [Boolean] auto_correct
6
+ # @param [Boolean] autocorrect
7
+ # @param [Boolean] debug
7
8
  # @param [String] file_path Template file path
8
9
  # @param [RuboCop::Config] rubocop_config
9
10
  # @param [#call] ruby_extractor
10
11
  # @param [String] source Template code
11
12
  def initialize(
12
- auto_correct:,
13
+ autocorrect:,
14
+ debug:,
13
15
  file_path:,
14
16
  rubocop_config:,
15
17
  ruby_extractor:,
16
18
  source:
17
19
  )
18
- @auto_correct = auto_correct
20
+ @autocorrect = autocorrect
21
+ @debug = debug
19
22
  @file_path = file_path
20
23
  @rubocop_config = rubocop_config
21
24
  @ruby_extractor = ruby_extractor
@@ -26,7 +29,8 @@ module Templatecop
26
29
  def call
27
30
  snippets.flat_map do |snippet|
28
31
  RubyOffenseCollector.new(
29
- auto_correct: @auto_correct,
32
+ autocorrect: @autocorrect,
33
+ debug: @debug,
30
34
  file_path: @file_path,
31
35
  rubocop_config: @rubocop_config,
32
36
  source: snippet[:code]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Templatecop
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: templatecop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-14 00:00:00.000000000 Z
11
+ date: 2023-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parallel
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0.87'
55
- description:
55
+ description:
56
56
  email:
57
57
  - r7kamura@gmail.com
58
58
  executables: []
@@ -64,7 +64,6 @@ files:
64
64
  - CHANGELOG.md
65
65
  - CODE_OF_CONDUCT.md
66
66
  - Gemfile
67
- - Gemfile.lock
68
67
  - LICENSE.txt
69
68
  - README.md
70
69
  - Rakefile
@@ -88,7 +87,7 @@ metadata:
88
87
  source_code_uri: https://github.com/r7kamura/templatecop
89
88
  changelog_uri: https://github.com/r7kamura/templatecop/blob/mainCHANGELOG.md
90
89
  rubygems_mfa_required: 'true'
91
- post_install_message:
90
+ post_install_message:
92
91
  rdoc_options: []
93
92
  require_paths:
94
93
  - lib
@@ -103,8 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
102
  - !ruby/object:Gem::Version
104
103
  version: '0'
105
104
  requirements: []
106
- rubygems_version: 3.1.4
107
- signing_key:
105
+ rubygems_version: 3.3.7
106
+ signing_key:
108
107
  specification_version: 4
109
108
  summary: RuboCop runner framework for template language.
110
109
  test_files: []
data/Gemfile.lock DELETED
@@ -1,72 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- templatecop (0.3.0)
5
- parallel
6
- parser
7
- rubocop (>= 0.87)
8
-
9
- GEM
10
- remote: https://rubygems.org/
11
- specs:
12
- ast (2.4.2)
13
- diff-lcs (1.5.0)
14
- parallel (1.21.0)
15
- parser (3.1.0.0)
16
- ast (~> 2.4.1)
17
- rainbow (3.1.1)
18
- rake (13.0.6)
19
- regexp_parser (2.2.0)
20
- rexml (3.2.5)
21
- rspec (3.10.0)
22
- rspec-core (~> 3.10.0)
23
- rspec-expectations (~> 3.10.0)
24
- rspec-mocks (~> 3.10.0)
25
- rspec-core (3.10.1)
26
- rspec-support (~> 3.10.0)
27
- rspec-expectations (3.10.2)
28
- diff-lcs (>= 1.2.0, < 2.0)
29
- rspec-support (~> 3.10.0)
30
- rspec-mocks (3.10.2)
31
- diff-lcs (>= 1.2.0, < 2.0)
32
- rspec-support (~> 3.10.0)
33
- rspec-support (3.10.3)
34
- rubocop (1.24.1)
35
- parallel (~> 1.10)
36
- parser (>= 3.0.0.0)
37
- rainbow (>= 2.2.2, < 4.0)
38
- regexp_parser (>= 1.8, < 3.0)
39
- rexml
40
- rubocop-ast (>= 1.15.1, < 2.0)
41
- ruby-progressbar (~> 1.7)
42
- unicode-display_width (>= 1.4.0, < 3.0)
43
- rubocop-ast (1.15.1)
44
- parser (>= 3.0.1.1)
45
- rubocop-rspec (2.7.0)
46
- rubocop (~> 1.19)
47
- ruby-progressbar (1.11.0)
48
- slimcop (0.13.1)
49
- slimi (>= 0.5.1)
50
- templatecop
51
- slimi (0.7.1)
52
- temple
53
- thor
54
- tilt
55
- temple (0.8.2)
56
- thor (1.2.1)
57
- tilt (2.0.10)
58
- unicode-display_width (2.1.0)
59
-
60
- PLATFORMS
61
- x86_64-linux
62
-
63
- DEPENDENCIES
64
- rake
65
- rspec
66
- rubocop
67
- rubocop-rspec
68
- slimcop
69
- templatecop!
70
-
71
- BUNDLED WITH
72
- 2.3.5