rubocop-cobra 0.5.0 → 0.6.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: 4731595ce18637027a1ce473a78d16224e4adfff65ad3b4df3ced8013a64fb2d
4
- data.tar.gz: 152d821617a7465990c89ca7d7ff3b39b579b34893a478dfd1f4a413332bd783
3
+ metadata.gz: 03c327fb1c4b0fe9e5b5c4856b8e6bcbeb5675f00eaf4261b2d29ff6cd2b9b69
4
+ data.tar.gz: d0979cb16b82b638f8ef1279f2046ddd2c4f01f3d05653cf01b6d58926255491
5
5
  SHA512:
6
- metadata.gz: 6c6aa816a0d26bf6b30296b694c03b3fa9a651806a89a2588d9815c090bbc0c4e37a9304f76518f3985540d8ebdc9cb05906438cf46c46a5318d203616a12a94
7
- data.tar.gz: c022a417d7f7ca0eb024d166fcdcc27ceaf3fed0bfbc4395b6f0dede27d77b0e3a60d6dd7a1a267d74212a180bee9577d9da2f73eb6d060ead763cd81a6285a6
6
+ metadata.gz: 0a7fc3991550cac817267d34155c378d4a315210cae860431fea57c6492b39e145c1d789eeffe256dc0c9e23395850731230e5f2c9b430922175df8d2a673dd2
7
+ data.tar.gz: a0c9477c21304cf3876eb63d7d99810653f54d253859a0a8e47781ca14e199f5859c722b3743364425c654b605b9b25273933c6451599b0639d9d1067611c710
data/.rubocop.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
- require:
3
+ plugins:
4
4
  - rubocop-powerhome
5
5
 
6
6
  Naming/FileName:
data/CHANGELOG.md CHANGED
@@ -1,4 +1,16 @@
1
- ## [Unreleased]
1
+ # Changelog
2
+
3
+ ## [0.6.0] - 2026-11-23
4
+
5
+ - Add `Cobra/DeprecatedConstant`, which flags references to constants an application lists as deprecated under `Constants` (for example a class that moved into another component), including `class_name:` and `constantize` strings and associations whose class Rails infers from the name. Relative names are resolved through the enclosing namespaces as Ruby and Rails would, associations in concerns are flagged whenever the including model could reach a deprecated class, and serializer and resource classes are skipped. It does nothing until configured
6
+ - Add `Cobra/EngineIsolateNamespace`, which requires every Rails engine to call `isolate_namespace` with the module it is defined in, so its routes, helpers and models stay out of the host application
7
+ - rubocop-cobra is now a RuboCop plugin (lint_roller), replacing the `Inject` config monkey patch. Load it with `plugins: - rubocop-cobra` in `.rubocop.yml`; `require: - rubocop-cobra` still works but RuboCop prints a deprecation warning
8
+ - **Breaking:** Remove `Cobra/HelperFilePlacement`. It told developers to namespace helpers that `Style/NoHelpers` (rubocop-powerhome) says should not exist. Configuration that still names it now fails with a message pointing to `Style/NoHelpers`; delete those entries (e.g. from `.rubocop_todo.yml`)
9
+ - `Cobra/GemRequirement` now checks every `path ".."` block wherever it appears in the Gemfile, including gems nested in a `group` and gems with other options alongside `require: nil`. Previously it only checked a path block that was the last statement in the file
10
+ - Fix `Cobra/Inheritance` allowing a top-level `::ApplicationRecord` (or other protected constant) with a leading `::`
11
+ - Anchor the file placement cops on `components/<name>/<dir>/`. A `lib/` or `components/` elsewhere in the path (a parent directory, a component named `pdf_lib`, `app/lib/`) no longer triggers false offenses, and `Cobra/ViewComponentFilePlacement` ignores files outside a component
12
+ - Run the previously skipped `Cobra/DependencyVersion` spec, and reword its message: it applies to every gemspec dependency, not only external components
13
+ - Fix `rubocop --debug` crashing with `undefined method 'logger' for module RuboCop::Rails`
2
14
 
3
15
  ## [0.5.0] - 2026-07-28
4
16
 
data/Gemfile.lock CHANGED
@@ -1,24 +1,27 @@
1
1
  PATH
2
2
  remote: ../rubocop-powerhome
3
3
  specs:
4
- rubocop-powerhome (0.7.0)
4
+ rubocop-powerhome (0.8.0)
5
+ lint_roller (~> 1.1)
5
6
  rubocop (= 1.82.1)
6
- rubocop-performance
7
- rubocop-rails
8
- rubocop-rake
9
- rubocop-rspec
7
+ rubocop-performance (>= 1.24)
8
+ rubocop-rails (>= 2.28)
9
+ rubocop-rake (>= 0.7)
10
+ rubocop-rspec (>= 3.5)
10
11
 
11
12
  PATH
12
13
  remote: .
13
14
  specs:
14
- rubocop-cobra (0.5.0)
15
+ rubocop-cobra (0.6.0)
16
+ activesupport (>= 7.1)
17
+ lint_roller (~> 1.1)
15
18
  rubocop (= 1.82.1)
16
19
  rubocop-powerhome
17
20
 
18
21
  GEM
19
22
  remote: https://rubygems.org/
20
23
  specs:
21
- activesupport (8.1.3)
24
+ activesupport (8.1.4.1)
22
25
  base64
23
26
  bigdecimal
24
27
  concurrent-ruby (~> 1.0, >= 1.3.1)
@@ -37,17 +40,17 @@ GEM
37
40
  thor (>= 0.14.0)
38
41
  ast (2.4.3)
39
42
  base64 (0.3.0)
40
- bigdecimal (4.1.1)
43
+ bigdecimal (4.1.3)
41
44
  byebug (11.1.3)
42
45
  coderay (1.1.3)
43
- concurrent-ruby (1.3.6)
44
- connection_pool (3.0.2)
46
+ concurrent-ruby (1.3.8)
47
+ connection_pool (3.0.3)
45
48
  csv (3.3.5)
46
49
  diff-lcs (1.6.2)
47
50
  drb (2.2.3)
48
51
  i18n (1.14.8)
49
52
  concurrent-ruby (~> 1.0)
50
- json (2.19.3)
53
+ json (2.19.9)
51
54
  language_server-protocol (3.17.0.5)
52
55
  license_finder (7.2.1)
53
56
  bundler
@@ -60,7 +63,7 @@ GEM
60
63
  lint_roller (1.1.0)
61
64
  logger (1.7.0)
62
65
  method_source (1.1.0)
63
- minitest (6.0.3)
66
+ minitest (6.0.6)
64
67
  drb (~> 2.0)
65
68
  prism (~> 1.5)
66
69
  parallel (1.28.0)
@@ -133,7 +136,7 @@ GEM
133
136
  unicode-display_width (3.2.0)
134
137
  unicode-emoji (~> 4.1)
135
138
  unicode-emoji (4.2.0)
136
- uri (1.1.1)
139
+ uri (1.1.2)
137
140
  with_env (1.1.0)
138
141
  xml-simple (1.1.9)
139
142
  rexml
data/config/default.yml CHANGED
@@ -12,17 +12,29 @@ Cobra/ControllerFilePlacement:
12
12
  VersionAdded: '0.1.0'
13
13
 
14
14
  Cobra/DependencyVersion:
15
- Description: 'External component dependencies should be declared with a version'
15
+ Description: 'Gemspec dependencies should be declared with a version'
16
16
  Enabled: true
17
17
  VersionAdded: '0.1.0'
18
18
 
19
- Cobra/GemRequirement:
20
- Description: 'Component Gemfile dependencies must specify "require: nil"'
19
+ Cobra/DeprecatedConstant:
20
+ Description: 'Flags references to constants the application has deprecated, such as a class left behind as a shim after moving to another component.'
21
21
  Enabled: true
22
- VersionAdded: '0.1.0'
22
+ VersionAdded: '0.6.0'
23
+ # The replacement only behaves the same once the deprecated class is a bare
24
+ # shim, so autocorrect runs only with `rubocop -A`.
25
+ SafeAutoCorrect: false
26
+ # Each deprecated constant mapped to its replacement, e.g.
27
+ # Territory: Directory::Territory
28
+ # The cop does nothing until the application lists at least one.
29
+ Constants: {}
23
30
 
24
- Cobra/HelperFilePlacement:
25
- Description: 'This cop disallows adding global helpers to the `app/helpers` directory.'
31
+ Cobra/EngineIsolateNamespace:
32
+ Description: 'Rails engines must call `isolate_namespace` with their own namespace'
33
+ Enabled: true
34
+ VersionAdded: '0.6.0'
35
+
36
+ Cobra/GemRequirement:
37
+ Description: 'Component Gemfile dependencies must specify "require: nil"'
26
38
  Enabled: true
27
39
  VersionAdded: '0.1.0'
28
40
 
@@ -0,0 +1,5 @@
1
+ # Cops removed from rubocop-cobra. RuboCop reports configuration that still
2
+ # references them with the alternative listed here.
3
+ removed:
4
+ Cobra/HelperFilePlacement:
5
+ alternatives: Style/NoHelpers
data/docs/README.md CHANGED
@@ -16,13 +16,15 @@ And then execute:
16
16
 
17
17
  ## Usage
18
18
 
19
- Add a `require` line to your `.rubocop.yml`:
19
+ Add the plugin to your `.rubocop.yml`:
20
20
 
21
21
  ```yml
22
- require:
22
+ plugins:
23
23
  - rubocop-cobra
24
24
  ```
25
25
 
26
+ rubocop-cobra is a [RuboCop plugin](https://docs.rubocop.org/rubocop/plugin_migration_guide.html), so it needs RuboCop 1.72 or newer. Listing it under `require:` still works but RuboCop prints a deprecation warning.
27
+
26
28
  That's it! You can override the standard configuration after that.
27
29
 
28
30
  ## Development
@@ -1,17 +1,20 @@
1
1
  PATH
2
2
  remote: ../../rubocop-powerhome
3
3
  specs:
4
- rubocop-powerhome (0.7.0)
4
+ rubocop-powerhome (0.8.0)
5
+ lint_roller (~> 1.1)
5
6
  rubocop (= 1.82.1)
6
- rubocop-performance
7
- rubocop-rails
8
- rubocop-rake
9
- rubocop-rspec
7
+ rubocop-performance (>= 1.24)
8
+ rubocop-rails (>= 2.28)
9
+ rubocop-rake (>= 0.7)
10
+ rubocop-rspec (>= 3.5)
10
11
 
11
12
  PATH
12
13
  remote: ..
13
14
  specs:
14
- rubocop-cobra (0.5.0)
15
+ rubocop-cobra (0.6.0)
16
+ activesupport (>= 7.1)
17
+ lint_roller (~> 1.1)
15
18
  rubocop (= 1.82.1)
16
19
  rubocop-powerhome
17
20
 
@@ -1,17 +1,20 @@
1
1
  PATH
2
2
  remote: ../../rubocop-powerhome
3
3
  specs:
4
- rubocop-powerhome (0.7.0)
4
+ rubocop-powerhome (0.8.0)
5
+ lint_roller (~> 1.1)
5
6
  rubocop (= 1.82.1)
6
- rubocop-performance
7
- rubocop-rails
8
- rubocop-rake
9
- rubocop-rspec
7
+ rubocop-performance (>= 1.24)
8
+ rubocop-rails (>= 2.28)
9
+ rubocop-rake (>= 0.7)
10
+ rubocop-rspec (>= 3.5)
10
11
 
11
12
  PATH
12
13
  remote: ..
13
14
  specs:
14
- rubocop-cobra (0.5.0)
15
+ rubocop-cobra (0.6.0)
16
+ activesupport (>= 7.1)
17
+ lint_roller (~> 1.1)
15
18
  rubocop (= 1.82.1)
16
19
  rubocop-powerhome
17
20
 
@@ -1,17 +1,20 @@
1
1
  PATH
2
2
  remote: ../../rubocop-powerhome
3
3
  specs:
4
- rubocop-powerhome (0.7.0)
4
+ rubocop-powerhome (0.8.0)
5
+ lint_roller (~> 1.1)
5
6
  rubocop (= 1.82.1)
6
- rubocop-performance
7
- rubocop-rails
8
- rubocop-rake
9
- rubocop-rspec
7
+ rubocop-performance (>= 1.24)
8
+ rubocop-rails (>= 2.28)
9
+ rubocop-rake (>= 0.7)
10
+ rubocop-rspec (>= 3.5)
10
11
 
11
12
  PATH
12
13
  remote: ..
13
14
  specs:
14
- rubocop-cobra (0.5.0)
15
+ rubocop-cobra (0.6.0)
16
+ activesupport (>= 7.1)
17
+ lint_roller (~> 1.1)
15
18
  rubocop (= 1.82.1)
16
19
  rubocop-powerhome
17
20
 
@@ -1,17 +1,20 @@
1
1
  PATH
2
2
  remote: ../../rubocop-powerhome
3
3
  specs:
4
- rubocop-powerhome (0.7.0)
4
+ rubocop-powerhome (0.8.0)
5
+ lint_roller (~> 1.1)
5
6
  rubocop (= 1.82.1)
6
- rubocop-performance
7
- rubocop-rails
8
- rubocop-rake
9
- rubocop-rspec
7
+ rubocop-performance (>= 1.24)
8
+ rubocop-rails (>= 2.28)
9
+ rubocop-rake (>= 0.7)
10
+ rubocop-rspec (>= 3.5)
10
11
 
11
12
  PATH
12
13
  remote: ..
13
14
  specs:
14
- rubocop-cobra (0.5.0)
15
+ rubocop-cobra (0.6.0)
16
+ activesupport (>= 7.1)
17
+ lint_roller (~> 1.1)
15
18
  rubocop (= 1.82.1)
16
19
  rubocop-powerhome
17
20
 
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lint_roller"
4
+
5
+ module RuboCop
6
+ module Cobra
7
+ # Integrates rubocop-cobra with RuboCop's plugin system.
8
+ class Plugin < LintRoller::Plugin
9
+ def about
10
+ LintRoller::About.new(
11
+ name: "rubocop-cobra",
12
+ version: VERSION,
13
+ homepage: "https://github.com/powerhome/power-tools/blob/main/packages/rubocop-cobra/",
14
+ description: "Cops that keep cobra (component-based Rails) apps modular."
15
+ )
16
+ end
17
+
18
+ def supported?(context)
19
+ context.engine == :rubocop
20
+ end
21
+
22
+ def rules(_context)
23
+ ConfigObsoletion.files << CONFIG_OBSOLETION unless ConfigObsoletion.files.include?(CONFIG_OBSOLETION)
24
+
25
+ LintRoller::Rules.new(type: :path, config_format: :rubocop, value: CONFIG_DEFAULT)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Cobra
5
- VERSION = "0.5.0"
5
+ VERSION = "0.6.0"
6
6
  end
7
7
  end
data/lib/rubocop/cobra.rb CHANGED
@@ -5,13 +5,14 @@ module RuboCop
5
5
  module Cobra
6
6
  class Error < StandardError; end
7
7
 
8
- PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
9
- CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
10
- CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
8
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
9
+ CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
10
+ CONFIG_OBSOLETION = PROJECT_ROOT.join("config", "obsoletion.yml").freeze
11
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
11
12
 
12
- private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
13
+ private_constant(:CONFIG_DEFAULT, :CONFIG_OBSOLETION, :PROJECT_ROOT)
13
14
 
14
- require_relative "cobra/inject"
15
15
  require_relative "cobra/version"
16
+ require_relative "cobra/plugin"
16
17
  end
17
18
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/inflector"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Cobra
8
+ # Reads an ActiveRecord association call (`belongs_to :territory, ...`)
9
+ # for the class Rails infers from its name.
10
+ module AssociationClass
11
+ COLLECTION_ASSOCIATIONS = %i[has_many has_and_belongs_to_many].freeze
12
+ NON_MODEL_CLASS = /(?:Serializer|Resource)\z/
13
+ MODEL_PARENT = /(?:Record|ActiveRecord::Base)\z/
14
+
15
+ # Options that name the class or stop Rails inferring it from the name.
16
+ def explicit_class?(node)
17
+ options(node)&.pairs&.any? do |pair|
18
+ pair.key.sym_type? && %i[class_name through polymorphic].include?(pair.key.value)
19
+ end
20
+ end
21
+
22
+ # Serializer and API resource DSLs (ActiveModel::Serializers,
23
+ # jsonapi-serializer, Graphiti) borrow the association method names but
24
+ # do not resolve a class from them or take `class_name:`. A class counts
25
+ # when its parent ends in Serializer or Resource, or when its own name
26
+ # does and its parent is not a model base class, so a model such as
27
+ # `AdditionalResource < ApplicationRecord` is still checked.
28
+ def non_model_class?(node)
29
+ klass = node.each_ancestor(:class).first
30
+ return false unless klass
31
+
32
+ parent = klass.parent_class&.source
33
+ return true if parent&.match?(NON_MODEL_CLASS)
34
+ return false if parent&.match?(MODEL_PARENT)
35
+
36
+ klass.identifier.source.match?(NON_MODEL_CLASS)
37
+ end
38
+
39
+ def options(node)
40
+ node.last_argument if node.last_argument.hash_type?
41
+ end
42
+
43
+ def inferred_class_name(method, association_name)
44
+ name = association_name.to_s
45
+ name = name.singularize if COLLECTION_ASSOCIATIONS.include?(method)
46
+ name.camelize
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ # Works out, from a node's enclosing classes and modules, which full
7
+ # names Ruby or Rails would try, in order, for a relative constant name.
8
+ module ConstantResolution
9
+ # A relative constant is searched for in each enclosing class or
10
+ # module, innermost first, then at the top level. `module A; class B`
11
+ # searches `A::B` then `A`, while the compact `class A::B` searches
12
+ # only `A::B`.
13
+ def ruby_candidates(node, name)
14
+ lexical_scopes(node).map { |scope| "#{scope}::#{name}" } << name
15
+ end
16
+
17
+ # Rails resolves a relative class name (an association's inferred or
18
+ # `class_name:` class) by trying each of the model's own namespaces,
19
+ # longest first (`A::B::Name`, then `A::Name`), then the top level.
20
+ # Outside a class body (a concern's `included` block, say) the model is
21
+ # not known, so the enclosing module's namespaces stand in for it.
22
+ def rails_candidates(node, name)
23
+ parts = lexical_scopes(node).first.to_s.split("::")
24
+ parts.size.downto(1).map { |size| "#{parts.first(size).join('::')}::#{name}" } << name
25
+ end
26
+
27
+ # Whether the node sits in a class body, so the model Rails resolves
28
+ # names from is the enclosing class rather than whatever includes it.
29
+ def model_known?(node)
30
+ node.each_ancestor(:class, :module).first&.class_type? || false
31
+ end
32
+
33
+ # Full names of the classes, modules and constants a source defines.
34
+ def defined_constants(ast)
35
+ return [] unless ast
36
+
37
+ ast.each_node(:class, :module, :casgn).filter_map do |node|
38
+ if node.casgn_type?
39
+ [lexical_scopes(node).first, node.name].compact.join("::") unless node.namespace
40
+ else
41
+ lexical_scopes(node.identifier).first
42
+ end
43
+ end
44
+ end
45
+
46
+ # Full names of the enclosing classes and modules, innermost first.
47
+ def lexical_scopes(node)
48
+ names = node.each_ancestor(:class, :module).map { |scope| scope.identifier.source }
49
+ names.each_index.map do |index|
50
+ chain = []
51
+ names[index..].each do |name|
52
+ chain.unshift(name.delete_prefix("::"))
53
+ break if name.start_with?("::")
54
+ end
55
+ chain.join("::")
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -30,22 +30,20 @@ module RuboCop
30
30
  return if processed_source.blank?
31
31
 
32
32
  path = processed_source.file_path
33
- return unless applicable_component_path?(path, controllers_path)
34
-
35
- if path.include?(controller_concerns_path)
36
- return if namespaced_correctly?(path, controller_concerns_path)
37
-
38
- add_offense(processed_source.ast,
39
- message: file_placement_msg(path, controller_concerns_path))
40
- end
41
- return if namespaced_correctly?(path, controllers_path)
33
+ matcher = matcher_for(path)
34
+ return unless matcher
35
+ return if namespaced_correctly?(path, matcher)
42
36
 
43
37
  add_offense(processed_source.ast,
44
- message: file_placement_msg(path, controllers_path))
38
+ message: file_placement_msg(path, matcher))
45
39
  end
46
40
 
47
41
  private
48
42
 
43
+ def matcher_for(path)
44
+ [controller_concerns_path, controllers_path].find { |matcher| applicable_component_path?(path, matcher) }
45
+ end
46
+
49
47
  def controllers_path
50
48
  "app/controllers/"
51
49
  end
@@ -3,10 +3,19 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Cobra
6
+ # Requires every gemspec dependency to declare a version requirement.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # spec.add_dependency "some_lib"
11
+ #
12
+ # # good
13
+ # spec.add_dependency "some_lib", "1.0.1"
14
+ #
6
15
  class DependencyVersion < RuboCop::Cop::Base
7
16
  extend NodePattern::Macros
8
17
 
9
- MSG = "External component dependencies should be declared with a version"
18
+ MSG = "Gemspec dependencies should be declared with a version"
10
19
 
11
20
  def on_new_investigation
12
21
  return if processed_source.blank?