rubocop-cobra 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1ed15eb1f676621c8de867b79b277b407fb36c862ef88291a732d84859799dda
4
+ data.tar.gz: 9e9de6b0bcc5c13b049b86ac861d5986fe0b79d016b7b14cf3d3dc14ea098a17
5
+ SHA512:
6
+ metadata.gz: bea53660dadb8c6518e1bfd9da50655db656aa0780aac9c0414fc28d54da82484726cd4934ee9d293c618ea1043a5e2a762922873319fcefd946abc76847a2f2
7
+ data.tar.gz: edd88e7a7d8224e3c2bb968fbeb5fa519c21971978d97e92adb901f955e1526b159c348cbf93d1875627f752f1371ac5f194ffe891edb6ce60287b11278190bb
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require:
4
+ - rubocop-powerhome
5
+
6
+ Naming/FileName:
7
+ Exclude:
8
+ - lib/rubocop-cobra.rb
9
+
10
+ Rails:
11
+ Enabled: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,23 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2022-05-18 20:49:51 UTC using RuboCop version 1.29.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
11
+ Metrics/AbcSize:
12
+ Max: 19
13
+
14
+ # Offense count: 14
15
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
16
+ # IgnoredMethods: refine
17
+ Metrics/BlockLength:
18
+ Max: 97
19
+
20
+ # Offense count: 2
21
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
22
+ Metrics/MethodLength:
23
+ Max: 11
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-05-18
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rubocop-cobra.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 1.29.1"
11
+
12
+ gem "rubocop-powerhome", path: "../rubocop-powerhome"
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: ../rubocop-powerhome
3
+ specs:
4
+ rubocop-powerhome (0.3.0)
5
+ rubocop
6
+ rubocop-performance
7
+ rubocop-rails
8
+ rubocop-rake
9
+ rubocop-rspec
10
+
11
+ PATH
12
+ remote: .
13
+ specs:
14
+ rubocop-cobra (0.3.0)
15
+ rubocop
16
+ rubocop-powerhome
17
+
18
+ GEM
19
+ remote: https://rubygems.org/
20
+ specs:
21
+ activesupport (7.0.3)
22
+ concurrent-ruby (~> 1.0, >= 1.0.2)
23
+ i18n (>= 1.6, < 2)
24
+ minitest (>= 5.1)
25
+ tzinfo (~> 2.0)
26
+ ast (2.4.2)
27
+ concurrent-ruby (1.1.10)
28
+ diff-lcs (1.5.0)
29
+ i18n (1.10.0)
30
+ concurrent-ruby (~> 1.0)
31
+ minitest (5.15.0)
32
+ parallel (1.22.1)
33
+ parser (3.1.2.0)
34
+ ast (~> 2.4.1)
35
+ rack (2.2.3)
36
+ rainbow (3.1.1)
37
+ rake (13.0.6)
38
+ regexp_parser (2.4.0)
39
+ rexml (3.2.5)
40
+ rspec (3.11.0)
41
+ rspec-core (~> 3.11.0)
42
+ rspec-expectations (~> 3.11.0)
43
+ rspec-mocks (~> 3.11.0)
44
+ rspec-core (3.11.0)
45
+ rspec-support (~> 3.11.0)
46
+ rspec-expectations (3.11.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.11.0)
49
+ rspec-mocks (3.11.1)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.11.0)
52
+ rspec-support (3.11.0)
53
+ rubocop (1.29.1)
54
+ parallel (~> 1.10)
55
+ parser (>= 3.1.0.0)
56
+ rainbow (>= 2.2.2, < 4.0)
57
+ regexp_parser (>= 1.8, < 3.0)
58
+ rexml (>= 3.2.5, < 4.0)
59
+ rubocop-ast (>= 1.17.0, < 2.0)
60
+ ruby-progressbar (~> 1.7)
61
+ unicode-display_width (>= 1.4.0, < 3.0)
62
+ rubocop-ast (1.18.0)
63
+ parser (>= 3.1.1.0)
64
+ rubocop-performance (1.13.3)
65
+ rubocop (>= 1.7.0, < 2.0)
66
+ rubocop-ast (>= 0.4.0)
67
+ rubocop-rails (2.14.2)
68
+ activesupport (>= 4.2.0)
69
+ rack (>= 1.1)
70
+ rubocop (>= 1.7.0, < 2.0)
71
+ rubocop-rake (0.6.0)
72
+ rubocop (~> 1.0)
73
+ rubocop-rspec (2.11.1)
74
+ rubocop (~> 1.19)
75
+ ruby-progressbar (1.11.0)
76
+ tzinfo (2.0.4)
77
+ concurrent-ruby (~> 1.0)
78
+ unicode-display_width (2.1.0)
79
+
80
+ PLATFORMS
81
+ arm64-darwin-21
82
+ x86_64-linux
83
+
84
+ DEPENDENCIES
85
+ rake (~> 13.0)
86
+ rspec (~> 3.0)
87
+ rubocop (~> 1.29.1)
88
+ rubocop-cobra!
89
+ rubocop-powerhome!
90
+
91
+ BUNDLED WITH
92
+ 2.3.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright Power Home Remodeling Group, LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Rubocop::Cobra
2
+
3
+ This gem is focused on providing Cops to support a healthy cobra app development (see https://cbra.info and https://github.com/powerhome/cobra_commander).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile under development:
8
+
9
+ ```ruby
10
+ gem "rubocop-cobra", require: false
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ ## Usage
18
+
19
+ Add a `require` line to your `.rubocop.yml`:
20
+
21
+ ```yml
22
+ require:
23
+ - rubocop-cobra
24
+ ```
25
+
26
+ That's it! You can override the standard configuration after that.
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/powerhome/power_linting/rubocop-cobra.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rspec/core/rake_task"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+ RuboCop::RakeTask.new(:rubocop)
10
+
11
+ task default: %i[spec rubocop]
12
+
13
+ desc "Generate a new cop with a template"
14
+ task :new_cop, [:cop] do |_task, args|
15
+ require "rubocop"
16
+
17
+ cop_name = args.fetch(:cop) do
18
+ warn "usage: bundle exec rake new_cop[Department/Name]"
19
+ exit!
20
+ end
21
+
22
+ generator = RuboCop::Cop::Generator.new(cop_name)
23
+
24
+ generator.write_source
25
+ generator.write_spec
26
+ generator.inject_require(root_file_path: "lib/rubocop/cop/cobra_cops.rb")
27
+ generator.inject_config(config_file_path: "config/default.yml")
28
+
29
+ puts generator.todo
30
+ end
@@ -0,0 +1,57 @@
1
+ Cobra:
2
+ Enabled: true
3
+
4
+ Cobra/CommandFilePlacement:
5
+ Description: 'This cop disallows adding global helpers to the `app/commands` directory.'
6
+ Enabled: true
7
+ VersionAdded: '0.1.0'
8
+
9
+ Cobra/ControllerFilePlacement:
10
+ Description: 'This cop disallows adding global controllers to the `app/controllers` directory.'
11
+ Enabled: true
12
+ VersionAdded: '0.1.0'
13
+
14
+ Cobra/DependencyVersion:
15
+ Description: 'External component dependencies should be declared with a version'
16
+ Enabled: true
17
+ VersionAdded: '0.1.0'
18
+
19
+ Cobra/GemRequirement:
20
+ Description: 'Component Gemfile dependencies must specify "require: nil"'
21
+ Enabled: true
22
+ VersionAdded: '0.1.0'
23
+
24
+ Cobra/HelperFilePlacement:
25
+ Description: 'This cop disallows adding global helpers to the `app/helpers` directory.'
26
+ Enabled: true
27
+ VersionAdded: '0.1.0'
28
+
29
+ Cobra/Inheritance:
30
+ Description: 'Enforce classes inherit from their own modularized parent classes'
31
+ Enabled: true
32
+ VersionAdded: '0.1.0'
33
+
34
+ Cobra/JobFilePlacement:
35
+ Description: 'This cop disallows adding global jobs to the `app/jobs` directory.'
36
+ Enabled: true
37
+ VersionAdded: '0.1.0'
38
+
39
+ Cobra/LibFilePlacement:
40
+ Description: 'This cop disallows adding library files directly into the `lib/` directory.'
41
+ Enabled: true
42
+ VersionAdded: '0.1.0'
43
+
44
+ Cobra/MailerFilePlacement:
45
+ Description: 'This cop disallows adding global helpers to the `app/mailers` directory.'
46
+ Enabled: true
47
+ VersionAdded: '0.1.0'
48
+
49
+ Cobra/ModelFilePlacement:
50
+ Description: 'This cop disallows adding global models to the `app/models` directory.'
51
+ Enabled: true
52
+ VersionAdded: '0.1.0'
53
+
54
+ Cobra/PresenterFilePlacement:
55
+ Description: 'This cop disallows adding global presenters to the `app/presenters` directory.'
56
+ Enabled: true
57
+ VersionAdded: '0.1.0'
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The original code is from https://github.com/rubocop/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
4
+ # See https://github.com/rubocop/rubocop-rspec/blob/master/MIT-LICENSE.md
5
+ module RuboCop
6
+ module Cobra
7
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
8
+ # bit of our configuration.
9
+ module Inject
10
+ def self.defaults!
11
+ path = CONFIG_DEFAULT.to_s
12
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
13
+ config = Config.new(hash, path).tap(&:make_excludes_absolute)
14
+ Rails.logger.debug { "configuration from #{path}" } if ConfigLoader.debug?
15
+ config = ConfigLoader.merge_with_default(config, path)
16
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cobra
5
+ VERSION = "0.3.0"
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # @private
5
+ module Cobra
6
+ class Error < StandardError; end
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
11
+
12
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
13
+
14
+ require_relative "cobra/inject"
15
+ require_relative "cobra/version"
16
+ end
17
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ # This cop disallows adding global helpers to the `app/commands` directory.
7
+ #
8
+ # The goal is to encourage developers to put new command files inside the correct
9
+ # namespace, where they can be more modularly isolated and ownership is clear.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # # path: components/my_component/app/commands/foo.rb
14
+ # class Foo
15
+ # # ...
16
+ # end
17
+ #
18
+ # # good
19
+ # # path: components/my_component/app/commands/my_component/foo.rb
20
+ # module MyComponent
21
+ # class Foo
22
+ # # ...
23
+ # end
24
+ # end
25
+ #
26
+ class CommandFilePlacement < RuboCop::Cop::Cop
27
+ include FilePlacementHelp
28
+
29
+ def investigate(processed_source)
30
+ return if processed_source.blank?
31
+
32
+ path = processed_source.file_path
33
+ return unless applicable_component_path?(path, commands_path)
34
+ return if namespaced_correctly?(path, commands_path)
35
+
36
+ add_offense(processed_source.ast,
37
+ message: file_placement_msg(path, commands_path))
38
+ end
39
+
40
+ private
41
+
42
+ def commands_path
43
+ "app/commands/"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ # This cop disallows adding global controllers to the `app/controllers` directory.
7
+ #
8
+ # The goal is to encourage developers to put new controllers inside the correct
9
+ # namespace, where they can be more modularly isolated and ownership is clear.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # # path: components/my_component/app/controllers/foo_controller.rb
14
+ # class FooController < ApplicationController
15
+ # # ...
16
+ # end
17
+ #
18
+ # # good
19
+ # # path: components/my_component/app/controllers/my_component/foo_controller.rb
20
+ # module MyComponent
21
+ # class FooController < MyComponent::ApplicationController
22
+ # # ...
23
+ # end
24
+ # end
25
+ #
26
+ class ControllerFilePlacement < RuboCop::Cop::Cop
27
+ include FilePlacementHelp
28
+
29
+ def investigate(processed_source)
30
+ return if processed_source.blank?
31
+
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)
42
+
43
+ add_offense(processed_source.ast,
44
+ message: file_placement_msg(path, controllers_path))
45
+ end
46
+
47
+ private
48
+
49
+ def controllers_path
50
+ "app/controllers/"
51
+ end
52
+
53
+ def controller_concerns_path
54
+ "app/controllers/concerns/"
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ class DependencyVersion < RuboCop::Cop::Cop
7
+ extend NodePattern::Macros
8
+
9
+ MSG = "External component dependencies should be declared with a version"
10
+
11
+ def investigate(processed_source)
12
+ return if processed_source.blank?
13
+
14
+ path = processed_source.file_path
15
+ return unless path.end_with?(".gemspec")
16
+
17
+ dependency_declarations(processed_source.ast).each do |dep|
18
+ next if declares_version?(dep)
19
+
20
+ add_offense(dep, message: MSG)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def_node_search :dependency_declarations, <<~PATTERN
27
+ (send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} (str _) ...)
28
+ PATTERN
29
+
30
+ def declares_version?(node)
31
+ node.first_argument != node.last_argument
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Methods that help analyzing file placement within Nitro components
6
+ module FilePlacementHelp
7
+ FILE_PLACEMENT_MSG =
8
+ "Do not add top-level files into `%<matcher_path>s`. " \
9
+ "Namespace them like `%<correct_path>s`"
10
+
11
+ def applicable_component_path?(path, matcher)
12
+ in_a_component?(path) && path_contains_matcher?(path, matcher)
13
+ end
14
+
15
+ def namespaced_correctly?(path, matcher)
16
+ potential_component_name = component_name(path, matcher)
17
+ component_path = File.join(
18
+ potential_component_name,
19
+ matcher,
20
+ potential_component_name
21
+ )
22
+ path.include?("#{component_path}/") || path.include?("#{component_path}.rb")
23
+ end
24
+
25
+ def file_placement_msg(path, matcher)
26
+ format(FILE_PLACEMENT_MSG,
27
+ matcher_path: matcher,
28
+ correct_path: correct_path(path, matcher))
29
+ end
30
+
31
+ private
32
+
33
+ def correct_path(path, matcher)
34
+ file = path.split(matcher).last
35
+ "#{matcher}#{component_name(path, matcher)}/#{file}"
36
+ end
37
+
38
+ def component_name(path, matcher)
39
+ path.split(matcher).first.split("/").last
40
+ end
41
+
42
+ def in_a_component?(path)
43
+ path.include?("components/")
44
+ end
45
+
46
+ def path_contains_matcher?(path, matcher)
47
+ path.include?(matcher)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ class GemRequirement < RuboCop::Cop::Cop
7
+ MSG = "Component Gemfile dependencies must specify " \
8
+ "'require: nil'."
9
+
10
+ def investigate(processed_source)
11
+ return if processed_source.blank?
12
+
13
+ gem_block = component_gem_block(processed_source.ast)&.first
14
+ return unless gem_block
15
+
16
+ process_component_declarations(gem_block)
17
+ end
18
+
19
+ private
20
+
21
+ def process_component_declarations(gem_block)
22
+ if gem_block.send_type?
23
+ add_gem_offenses(gem_block)
24
+ else
25
+ gem_listings(gem_block).each do |gem_node|
26
+ add_gem_offenses(gem_node)
27
+ end
28
+ end
29
+ end
30
+
31
+ def add_gem_offenses(gem_node)
32
+ component_options = gem_options(gem_node).first
33
+ return if component_options && not_required?(component_options)
34
+
35
+ add_offense(gem_node, message: MSG)
36
+ end
37
+
38
+ def_node_matcher :component_gem_block, <<~PATTERN
39
+ (:begin ...
40
+ (:block
41
+ (:send nil? :path (:str ".."))
42
+ (:args)
43
+ $...
44
+ )
45
+ )
46
+ PATTERN
47
+
48
+ def_node_matcher :gem_options, "(:send nil? :gem _ $...)"
49
+ def_node_matcher :gem_listings, "(:begin $...)"
50
+
51
+ def_node_matcher :not_required?, <<~PATTERN
52
+ (:hash
53
+ (:pair
54
+ (:sym :require)
55
+ (${nil false})
56
+ )
57
+ )
58
+ PATTERN
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ # This cop disallows adding global helpers to the `app/helpers` directory.
7
+ #
8
+ # The goal is to encourage developers to put new helpers inside the correct
9
+ # namespace, where they can be more modularly isolated and ownership is clear.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # # path: components/my_component/app/helpers/foo.rb
14
+ # class Foo
15
+ # # ...
16
+ # end
17
+ #
18
+ # # good
19
+ # # path: components/my_component/app/helpers/my_component/foo.rb
20
+ # module MyComponent
21
+ # class Foo
22
+ # # ...
23
+ # end
24
+ # end
25
+ #
26
+ class HelperFilePlacement < RuboCop::Cop::Cop
27
+ include FilePlacementHelp
28
+
29
+ def investigate(processed_source)
30
+ return if processed_source.blank?
31
+
32
+ path = processed_source.file_path
33
+ return unless applicable_component_path?(path, helpers_path)
34
+ return if namespaced_correctly?(path, helpers_path)
35
+
36
+ add_offense(processed_source.ast,
37
+ message: file_placement_msg(path, helpers_path))
38
+ end
39
+
40
+ private
41
+
42
+ def helpers_path
43
+ "app/helpers/"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ class Inheritance < RuboCop::Cop::Cop
7
+ PROTECTED_GLOBAL_CONSTANTS = %w[
8
+ ApplicationController
9
+ ApplicationRecord
10
+ ApiController
11
+ ].freeze
12
+
13
+ MSG = "Do not directly inherit from a global %<class>s. " \
14
+ "Instead, inherit from your component's modularized " \
15
+ "%<class>s, such as MyComponent::%<class>s."
16
+
17
+ def on_class(node)
18
+ inheritance_constant = node.node_parts[1]
19
+ inheritance_class = inheritance_constant&.source
20
+ return unless PROTECTED_GLOBAL_CONSTANTS.include?(inheritance_class)
21
+
22
+ add_offense(inheritance_constant,
23
+ message: format(MSG, class: inheritance_class))
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ # This cop disallows adding global jobs to the `app/jobs` directory.
7
+ #
8
+ # The goal is to encourage developers to put new jobs inside the correct
9
+ # namespace, where they can be more modularly isolated and ownership is clear.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # # path: components/my_component/app/jobs/foo_job.rb
14
+ # class FooJob
15
+ # # ...
16
+ # end
17
+ #
18
+ # # good
19
+ # # path: components/my_component/app/jobs/my_component/foo_job.rb
20
+ # module MyComponent
21
+ # class FooJob
22
+ # # ...
23
+ # end
24
+ # end
25
+ #
26
+ class JobFilePlacement < RuboCop::Cop::Cop
27
+ include FilePlacementHelp
28
+
29
+ def investigate(processed_source)
30
+ return if processed_source.blank?
31
+
32
+ path = processed_source.file_path
33
+ return unless applicable_component_path?(path, jobs_path)
34
+ return if namespaced_correctly?(path, jobs_path)
35
+
36
+ add_offense(processed_source.ast,
37
+ message: file_placement_msg(path, jobs_path))
38
+ end
39
+
40
+ private
41
+
42
+ def jobs_path
43
+ "app/jobs/"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ # This cop disallows adding library files directly into the `lib/` directory.
7
+ #
8
+ # The goal is to encourage developers to put new library files inside the correct
9
+ # namespace, where they can be more modularly isolated and ownership is clear.
10
+ #
11
+ # Exceptions to this rule are `spec/lib/*` and `lib/tasks/*` file patterns.
12
+ #
13
+ # @example
14
+ # # bad
15
+ # # path: components/my_component/lib/foo.rb
16
+ # class Foo
17
+ # # ...
18
+ # end
19
+ #
20
+ # # good
21
+ # # path: components/my_component/lib/my_component/foo.rb
22
+ # module MyComponent
23
+ # class Foo
24
+ # # ...
25
+ # end
26
+ # end
27
+ #
28
+ class LibFilePlacement < RuboCop::Cop::Cop
29
+ include FilePlacementHelp
30
+
31
+ def investigate(processed_source)
32
+ return if processed_source.blank?
33
+
34
+ path = processed_source.file_path
35
+ return unless applicable_component_path?(path, lib_path)
36
+ return if acceptable_lib_path?(path) || namespaced_correctly?(path, lib_path)
37
+
38
+ add_offense(processed_source.ast,
39
+ message: file_placement_msg(path, lib_path))
40
+ end
41
+
42
+ private
43
+
44
+ def acceptable_lib_path?(path)
45
+ path.include?("lib/tasks/") || path.include?("spec/lib/")
46
+ end
47
+
48
+ def lib_path
49
+ "lib/"
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ # This cop disallows adding global helpers to the `app/mailers` directory.
7
+ #
8
+ # The goal is to encourage developers to put new mailer files inside the correct
9
+ # namespace, where they can be more modularly isolated and ownership is clear.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # # path: components/my_component/app/mailers/foo.rb
14
+ # class Foo
15
+ # # ...
16
+ # end
17
+ #
18
+ # # good
19
+ # # path: components/my_component/app/mailers/my_component/foo.rb
20
+ # module MyComponent
21
+ # class Foo
22
+ # # ...
23
+ # end
24
+ # end
25
+ #
26
+ class MailerFilePlacement < RuboCop::Cop::Cop
27
+ include FilePlacementHelp
28
+
29
+ def investigate(processed_source)
30
+ return if processed_source.blank?
31
+
32
+ path = processed_source.file_path
33
+ return unless applicable_component_path?(path, mailers_path)
34
+ return if namespaced_correctly?(path, mailers_path)
35
+
36
+ add_offense(processed_source.ast,
37
+ message: file_placement_msg(path, mailers_path))
38
+ end
39
+
40
+ private
41
+
42
+ def mailers_path
43
+ "app/mailers/"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ # This cop disallows adding global models to the `app/models` directory.
7
+ #
8
+ # The goal is to encourage developers to put new models inside the correct
9
+ # namespace, where they can be more modularly isolated and ownership is clear.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # # path: components/my_component/app/models/foo.rb
14
+ # class Foo < ApplicationRecord
15
+ # # ...
16
+ # end
17
+ #
18
+ # # good
19
+ # # path: components/my_component/app/models/my_component/foo.rb
20
+ # module MyComponent
21
+ # class Foo < MyComponent::ApplicationRecord
22
+ # # ...
23
+ # end
24
+ # end
25
+ #
26
+ class ModelFilePlacement < RuboCop::Cop::Cop
27
+ include FilePlacementHelp
28
+
29
+ def investigate(processed_source)
30
+ return if processed_source.blank?
31
+
32
+ path = processed_source.file_path
33
+ return unless applicable_component_path?(path, models_path)
34
+
35
+ if path.include?(model_concerns_path)
36
+ return if namespaced_correctly?(path, model_concerns_path)
37
+
38
+ add_offense(processed_source.ast,
39
+ message: file_placement_msg(path, model_concerns_path))
40
+ end
41
+ return if namespaced_correctly?(path, models_path)
42
+
43
+ add_offense(processed_source.ast,
44
+ message: file_placement_msg(path, models_path))
45
+ end
46
+
47
+ private
48
+
49
+ def models_path
50
+ "app/models/"
51
+ end
52
+
53
+ def model_concerns_path
54
+ "app/models/concerns/"
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ # This cop disallows adding global presenters to the `app/presenters` directory.
7
+ #
8
+ # The goal is to encourage developers to put new presenters inside the correct
9
+ # namespace, where they can be more modularly isolated and ownership is clear.
10
+ #
11
+ # @example
12
+ # # bad
13
+ # # path: components/my_component/app/presenters/foo_presenter.rb
14
+ # class FooPresenter
15
+ # # ...
16
+ # end
17
+ #
18
+ # # good
19
+ # # path: components/my_component/app/presenters/my_component/foo_presenter.rb
20
+ # module MyComponent
21
+ # class FooPresenter
22
+ # # ...
23
+ # end
24
+ # end
25
+ #
26
+ class PresenterFilePlacement < RuboCop::Cop::Cop
27
+ include FilePlacementHelp
28
+
29
+ def investigate(processed_source)
30
+ return if processed_source.blank?
31
+
32
+ path = processed_source.file_path
33
+ return unless applicable_component_path?(path, presenters_path)
34
+ return if namespaced_correctly?(path, presenters_path)
35
+
36
+ add_offense(processed_source.ast,
37
+ message: file_placement_msg(path, presenters_path))
38
+ end
39
+
40
+ private
41
+
42
+ def presenters_path
43
+ "app/presenters/"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Cobra
6
+ require_relative "cobra/file_placement_help"
7
+
8
+ require_relative "cobra/command_file_placement"
9
+ require_relative "cobra/controller_file_placement"
10
+ require_relative "cobra/dependency_version"
11
+ require_relative "cobra/gem_requirement"
12
+ require_relative "cobra/helper_file_placement"
13
+ require_relative "cobra/inheritance"
14
+ require_relative "cobra/job_file_placement"
15
+ require_relative "cobra/lib_file_placement"
16
+ require_relative "cobra/mailer_file_placement"
17
+ require_relative "cobra/model_file_placement"
18
+ require_relative "cobra/presenter_file_placement"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ require_relative "rubocop/cobra"
6
+
7
+ RuboCop::Cobra::Inject.defaults!
8
+
9
+ require_relative "rubocop/cop/cobra_cops"
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rubocop/cobra/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rubocop-cobra"
7
+ spec.version = RuboCop::Cobra::VERSION
8
+ spec.authors = ["Carlos Palhares"]
9
+ spec.email = ["chjunior@gmail.com"]
10
+
11
+ spec.summary = "Cobra rubocop linters"
12
+ spec.description = "Cobra rubocop linters"
13
+ spec.homepage = "https://github.com/powerhome/power_linting"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, check out our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+
38
+ spec.add_runtime_dependency "rubocop"
39
+ spec.add_runtime_dependency "rubocop-powerhome"
40
+ spec.metadata["rubygems_mfa_required"] = "true"
41
+ end
@@ -0,0 +1,6 @@
1
+ module Rubocop
2
+ module Cobra
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-cobra
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Palhares
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
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-powerhome
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Cobra rubocop linters
42
+ email:
43
+ - chjunior@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rubocop.yml"
49
+ - ".rubocop_todo.yml"
50
+ - CHANGELOG.md
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - config/default.yml
57
+ - lib/rubocop-cobra.rb
58
+ - lib/rubocop/cobra.rb
59
+ - lib/rubocop/cobra/inject.rb
60
+ - lib/rubocop/cobra/version.rb
61
+ - lib/rubocop/cop/cobra/command_file_placement.rb
62
+ - lib/rubocop/cop/cobra/controller_file_placement.rb
63
+ - lib/rubocop/cop/cobra/dependency_version.rb
64
+ - lib/rubocop/cop/cobra/file_placement_help.rb
65
+ - lib/rubocop/cop/cobra/gem_requirement.rb
66
+ - lib/rubocop/cop/cobra/helper_file_placement.rb
67
+ - lib/rubocop/cop/cobra/inheritance.rb
68
+ - lib/rubocop/cop/cobra/job_file_placement.rb
69
+ - lib/rubocop/cop/cobra/lib_file_placement.rb
70
+ - lib/rubocop/cop/cobra/mailer_file_placement.rb
71
+ - lib/rubocop/cop/cobra/model_file_placement.rb
72
+ - lib/rubocop/cop/cobra/presenter_file_placement.rb
73
+ - lib/rubocop/cop/cobra_cops.rb
74
+ - rubocop-cobra.gemspec
75
+ - sig/rubocop/cobra.rbs
76
+ homepage: https://github.com/powerhome/power_linting
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/powerhome/power_linting
81
+ source_code_uri: https://github.com/powerhome/power_linting
82
+ changelog_uri: https://github.com/powerhome/power_linting/CHANGELOG.md
83
+ rubygems_mfa_required: 'true'
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.7.0
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.3.7
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Cobra rubocop linters
103
+ test_files: []