mecha_style-rails 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bc2b28cfcdd1692dbea50a2d57bfcb17e33b74ee7e919b67805ccb101e81485a
4
+ data.tar.gz: ef04df650c42dbbb6630b81a7098df4b09a1afea0e2d9cc805f6aa1eab209126
5
+ SHA512:
6
+ metadata.gz: 1bb457bd841e99817c87654d16fa1625be8cfc7c4176ebb86a50f194a1198598cc14f449de39e2c9d048771955ad2712dbe42318a2b89df5d7c27077c052b66e
7
+ data.tar.gz: f114860fa2efded37ffeabd77fe1ca03da064954656229b5b535d6d6df887fc0d6df53e376ae0290b4ed9aa4a5314c8abe8b8cfdf6887c13d3027ce05dc1a697
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Jeff Lunt
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,38 @@
1
+ # mecha_style-rails
2
+
3
+ Custom [RuboCop](https://rubocop.org) plugin enforcing personal Ruby on Rails style and conventions.
4
+
5
+ ## Usage in a Rails App
6
+
7
+ Add the gem to your Rails application's `Gemfile`:
8
+
9
+ ```ruby
10
+ group :development, :test do
11
+ gem "mecha_style-rails", github: "jefflunt/mecha_style-rails", require: false
12
+ end
13
+ ```
14
+
15
+ Enable the plugin in `.rubocop.yml`:
16
+
17
+ ```yaml
18
+ plugins:
19
+ - rubocop-rails
20
+ - mecha_style-rails
21
+ ```
22
+
23
+ ## Adding Custom Cops
24
+
25
+ 1. Create a cop class in `lib/rubocop/cop/mecha_style_rails/<cop_name>.rb` under `RuboCop::Cop::MechaStyleRails` (all files in that directory are auto-required).
26
+ 2. Add its default configuration entry in `config/default.yml`.
27
+ 3. Add specs in `spec/rubocop/cop/mecha_style_rails/<cop_name>_spec.rb`.
28
+
29
+ You can also place personal overrides for built-in `Style/*`, `Layout/*`, `Lint/*`, or `Rails/*` cops directly in `config/default.yml`.
30
+
31
+ ## Development
32
+
33
+ ```bash
34
+ bin/setup
35
+ bundle exec rake # runs RSpec and RuboCop
36
+ bundle exec rspec # runs cop specs only
37
+ bundle exec rubocop # lints this repository
38
+ ```
@@ -0,0 +1,47 @@
1
+ # Default configuration for mecha_style-rails.
2
+
3
+ Layout/LineLength:
4
+ Max: 256
5
+
6
+ Style/GuardClause:
7
+ Enabled: true
8
+ MinBodyLength: 1
9
+
10
+ Rails/DefaultScope:
11
+ Enabled: true
12
+
13
+ MechaStyleRails/Example:
14
+ Description: "Starter template cop demonstrating AST matching and autocorrection."
15
+ Enabled: false
16
+ VersionAdded: "0.1.0"
17
+
18
+ MechaStyleRails/NamespaceBaseController:
19
+ Description: "Namespaced controllers must inherit from their namespace BaseController."
20
+ Enabled: true
21
+ VersionAdded: "0.2.0"
22
+
23
+ MechaStyleRails/NoVisibilityModifiers:
24
+ Description: "Disallow private/protected in favor of _method and __method naming."
25
+ Enabled: true
26
+ VersionAdded: "0.2.0"
27
+
28
+ MechaStyleRails/NoInlineJsonRender:
29
+ Description: "Disallow inline JSON hash/array rendering in controllers; use app/presenters/."
30
+ Enabled: true
31
+ AllowErrorHash: true
32
+ VersionAdded: "0.2.0"
33
+
34
+ MechaStyleRails/NoInlineHashMapping:
35
+ Description: "Disallow inline .map { |x| { ... } } hash formatting in controllers; use app/presenters/."
36
+ Enabled: true
37
+ VersionAdded: "0.2.0"
38
+
39
+ MechaStyleRails/NoFallbackParamLookup:
40
+ Description: "Disallow fallback params lookups and string-to-boolean params comparisons."
41
+ Enabled: true
42
+ VersionAdded: "0.2.0"
43
+
44
+ MechaStyleRails/NoPresenceTernary:
45
+ Description: "Disallow noisy params[:x].present? ? ... : ... ternaries in controllers."
46
+ Enabled: true
47
+ VersionAdded: "0.2.0"
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rubocop/mecha_style_rails"
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module MechaStyleRails
6
+ # Starter template cop showing AST node pattern matching and autocorrection.
7
+ # Disabled by default in `config/default.yml`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # legacy_helper(user)
12
+ #
13
+ # # good
14
+ # modern_helper(user)
15
+ class Example < Base
16
+ extend AutoCorrector
17
+
18
+ MSG = "Prefer `modern_helper` over `legacy_helper`."
19
+ RESTRICT_ON_SEND = %i[legacy_helper].freeze
20
+
21
+ def_node_matcher :legacy_helper_call?, <<~PATTERN
22
+ (send nil? :legacy_helper ...)
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ return unless legacy_helper_call?(node)
27
+
28
+ add_offense(node.loc.selector) do |corrector|
29
+ corrector.replace(node.loc.selector, "modern_helper")
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module MechaStyleRails
6
+ # Enforces that namespaced controllers inherit from their namespace's
7
+ # `BaseController` (e.g. `Api::BaseController`, `Admin::BaseController`)
8
+ # instead of `ApplicationController`.
9
+ #
10
+ # @example
11
+ # # bad (in app/controllers/api/play_histories_controller.rb)
12
+ # module Api
13
+ # class PlayHistoriesController < ApplicationController
14
+ # end
15
+ # end
16
+ #
17
+ # # good
18
+ # module Api
19
+ # class PlayHistoriesController < Api::BaseController
20
+ # end
21
+ # end
22
+ #
23
+ # # good
24
+ # module Api
25
+ # class PlayHistoriesController < BaseController
26
+ # end
27
+ # end
28
+ class NamespaceBaseController < Base
29
+ MSG = "Namespaced controllers must inherit from `%<expected>s` (or `BaseController`), not `%<actual>s`."
30
+
31
+ def on_class(node)
32
+ namespace = __controller_namespace
33
+ return unless namespace
34
+ return if node.identifier.short_name == :BaseController
35
+
36
+ parent = node.parent_class
37
+ return unless parent
38
+
39
+ expected = "#{namespace}::BaseController"
40
+ return if __valid_parent?(parent, expected)
41
+
42
+ add_offense(parent, message: format(MSG, expected: expected, actual: parent.source))
43
+ end
44
+
45
+ def __controller_namespace
46
+ path = processed_source.file_path.tr("\\", "/")
47
+ match = path.match(%r{(?:^|/)app/controllers/([^/]+)/[^/]+_controller\.rb\z})
48
+ return unless match
49
+
50
+ __camelize(match[1])
51
+ end
52
+
53
+ def __valid_parent?(parent, expected)
54
+ [expected, "BaseController"].include?(parent.source)
55
+ end
56
+
57
+ def __camelize(segment)
58
+ segment.split("_").map(&:capitalize).join
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module MechaStyleRails
6
+ # Disallows fallback param key lookups and string-to-boolean param checks.
7
+ # Use a single canonical param key and strict JSON types instead.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # params[:album_id].presence || params[:id].presence
12
+ #
13
+ # # bad
14
+ # params[:full_listen].to_s == "true"
15
+ #
16
+ # # good
17
+ # params.expect(:album_id)
18
+ # params[:full_listen] == true
19
+ class NoFallbackParamLookup < Base
20
+ MSG_FALLBACK = "Avoid fallback `params` lookups; use one canonical parameter key."
21
+ MSG_STRING_BOOL = "Avoid string-to-boolean `params` comparisons; expect strict JSON boolean types."
22
+
23
+ def_node_matcher :param_access?, <<~PATTERN
24
+ (send (send nil? :params) :[] ...)
25
+ PATTERN
26
+
27
+ def_node_matcher :string_bool_comparison?, <<~PATTERN
28
+ {
29
+ (send (send (send (send nil? :params) :[] ...) :to_s) :== (str {"true" "false"}))
30
+ (send (send (send nil? :params) :[] ...) :== (str {"true" "false"}))
31
+ }
32
+ PATTERN
33
+
34
+ def on_or(node)
35
+ return unless __controller_file?
36
+ return if node.parent&.or_type?
37
+ return unless __contains_param_access?(node.lhs) && __contains_param_access?(node.rhs)
38
+
39
+ add_offense(node, message: MSG_FALLBACK)
40
+ end
41
+
42
+ def on_send(node)
43
+ return unless __controller_file?
44
+ return unless string_bool_comparison?(node)
45
+
46
+ add_offense(node, message: MSG_STRING_BOOL)
47
+ end
48
+
49
+ def __controller_file?
50
+ processed_source.file_path.tr("\\", "/").include?("app/controllers/")
51
+ end
52
+
53
+ def __contains_param_access?(node)
54
+ return true if param_access?(node)
55
+
56
+ node.each_descendant(:send).any? { |send_node| param_access?(send_node) }
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module MechaStyleRails
6
+ # Disallows inline `.map { |x| { ... } }` hash formatting in controllers.
7
+ # Extract serialization into `app/presenters/` (or model formatting methods).
8
+ #
9
+ # @example
10
+ # # bad
11
+ # tracks.map { |t| { "id" => t.identifier, "title" => t.title } }
12
+ #
13
+ # # good
14
+ # tracks.map { |t| TrackPresenter.new(t).as_json }
15
+ class NoInlineHashMapping < Base
16
+ MSG = "Avoid inline hash formatting inside controllers; extract into `app/presenters/`."
17
+
18
+ def_node_matcher :inline_hash_map?, <<~PATTERN
19
+ (block (send _ {:map :collect}) _ (hash ...))
20
+ PATTERN
21
+
22
+ def on_block(node)
23
+ return unless __controller_file?
24
+ return unless inline_hash_map?(node)
25
+
26
+ add_offense(node)
27
+ end
28
+
29
+ def __controller_file?
30
+ processed_source.file_path.tr("\\", "/").include?("app/controllers/")
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module MechaStyleRails
6
+ # Disallows inline JSON hash/map rendering in controllers.
7
+ # Extract serialization into `app/presenters/` instead.
8
+ #
9
+ # Single-key `{ error: ... }` payloads are allowed by default
10
+ # (`AllowErrorHash: true`).
11
+ #
12
+ # @example
13
+ # # bad
14
+ # render json: { status: "recorded", id: play.id }
15
+ #
16
+ # # good
17
+ # render json: PlayHistoryPresenter.new(play).as_json
18
+ class NoInlineJsonRender < Base
19
+ MSG = "Avoid inline JSON formatting in controllers; use a presenter in `app/presenters/`."
20
+
21
+ def_node_matcher :render_json_node, <<~PATTERN
22
+ (send nil? :render (hash <$(pair (sym :json) $_) ...>))
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ return unless __controller_file?
27
+
28
+ render_json_node(node) do |pair_node, value_node|
29
+ next unless __inline_formatting?(value_node)
30
+ next if __allow_error_hash? && __error_hash?(value_node)
31
+
32
+ add_offense(pair_node)
33
+ end
34
+ end
35
+
36
+ def __controller_file?
37
+ processed_source.file_path.tr("\\", "/").include?("app/controllers/")
38
+ end
39
+
40
+ def __inline_formatting?(value_node)
41
+ value_node.hash_type? || value_node.array_type?
42
+ end
43
+
44
+ def __allow_error_hash?
45
+ cop_config.fetch("AllowErrorHash", true)
46
+ end
47
+
48
+ def __error_hash?(value_node)
49
+ return false unless value_node.hash_type? && value_node.pairs.size == 1
50
+
51
+ key = value_node.pairs.first.key
52
+ key.sym_type? && key.value == :error
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module MechaStyleRails
6
+ # Disallows noisy `params[:x].present? ? ... : ...` ternaries in controllers.
7
+ # Use strict JSON types or direct coercion instead.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # params[:seconds_listened].present? ? params[:seconds_listened].to_i : 0
12
+ #
13
+ # # good
14
+ # params[:seconds_listened].to_i
15
+ class NoPresenceTernary < Base
16
+ MSG = "Avoid `params[...].present?` ternary checks; use strict JSON types or direct coercion."
17
+
18
+ def_node_matcher :presence_ternary?, <<~PATTERN
19
+ (if (send (send (send nil? :params) :[] ...) :present?) _ _)
20
+ PATTERN
21
+
22
+ def on_if(node)
23
+ return unless __controller_file?
24
+ return unless node.ternary?
25
+ return unless presence_ternary?(node)
26
+
27
+ add_offense(node)
28
+ end
29
+
30
+ def __controller_file?
31
+ processed_source.file_path.tr("\\", "/").include?("app/controllers/")
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module MechaStyleRails
6
+ # Disallows `private` and `protected` visibility modifiers.
7
+ #
8
+ # Instead of hiding methods behind visibility modifiers (which makes unit
9
+ # testing harder), communicate intent by naming convention:
10
+ # - `_method_name` for protected-intent methods
11
+ # - `__method_name` for private-intent methods
12
+ #
13
+ # @example
14
+ # # bad
15
+ # class Foo
16
+ # private
17
+ #
18
+ # def helper
19
+ # end
20
+ # end
21
+ #
22
+ # # good
23
+ # class Foo
24
+ # def __helper
25
+ # end
26
+ # end
27
+ class NoVisibilityModifiers < Base
28
+ MSG = "Avoid `%<modifier>s`; use `_method` (protected intent) or `__method` (private intent) naming instead."
29
+
30
+ def_node_matcher :visibility_modifier?, <<~PATTERN
31
+ (send nil? ${:private :protected} ...)
32
+ PATTERN
33
+
34
+ def on_send(node)
35
+ visibility_modifier?(node) do |modifier|
36
+ add_offense(node, message: format(MSG, modifier: modifier))
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lint_roller"
4
+
5
+ module RuboCop
6
+ module MechaStyleRails
7
+ # LintRoller plugin integration for RuboCop 1.72+.
8
+ class Plugin < LintRoller::Plugin
9
+ def about
10
+ LintRoller::About.new(
11
+ name: "mecha_style-rails",
12
+ version: VERSION,
13
+ homepage: "https://github.com/jefflunt/mecha_style-rails",
14
+ description: "Custom RuboCop rules enforcing personal Ruby on Rails style."
15
+ )
16
+ end
17
+
18
+ def supported?(context)
19
+ context.engine == :rubocop
20
+ end
21
+
22
+ def rules(_context)
23
+ LintRoller::Rules.new(
24
+ type: :path,
25
+ config_format: :rubocop,
26
+ value: CONFIG_DEFAULT
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module MechaStyleRails
5
+ VERSION = "0.2.0"
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require "rubocop"
5
+ require_relative "mecha_style_rails/version"
6
+ require_relative "mecha_style_rails/plugin"
7
+
8
+ module RuboCop
9
+ # Namespace for Jeff's custom Rails RuboCop plugin and rules.
10
+ module MechaStyleRails
11
+ PROJECT_ROOT = Pathname.new(__dir__).join("../..").expand_path.freeze
12
+ CONFIG_DEFAULT = PROJECT_ROOT.join("config/default.yml").freeze
13
+ end
14
+ end
15
+
16
+ Dir.glob(File.join(__dir__, "cop/mecha_style_rails/**/*.rb")).each do |cop_file|
17
+ require cop_file
18
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mecha_style-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lunt
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: lint_roller
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.1'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rubocop
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.72'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.72'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rubocop-rails
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.28'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.28'
54
+ description: A RuboCop plugin containing custom cops and configuration for Jeff's
55
+ Ruby on Rails conventions.
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - LICENSE.txt
61
+ - README.md
62
+ - config/default.yml
63
+ - lib/mecha_style-rails.rb
64
+ - lib/rubocop/cop/mecha_style_rails/example.rb
65
+ - lib/rubocop/cop/mecha_style_rails/namespace_base_controller.rb
66
+ - lib/rubocop/cop/mecha_style_rails/no_fallback_param_lookup.rb
67
+ - lib/rubocop/cop/mecha_style_rails/no_inline_hash_mapping.rb
68
+ - lib/rubocop/cop/mecha_style_rails/no_inline_json_render.rb
69
+ - lib/rubocop/cop/mecha_style_rails/no_presence_ternary.rb
70
+ - lib/rubocop/cop/mecha_style_rails/no_visibility_modifiers.rb
71
+ - lib/rubocop/mecha_style_rails.rb
72
+ - lib/rubocop/mecha_style_rails/plugin.rb
73
+ - lib/rubocop/mecha_style_rails/version.rb
74
+ homepage: https://github.com/jefflunt/mecha_style-rails
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/jefflunt/mecha_style-rails
79
+ source_code_uri: https://github.com/jefflunt/mecha_style-rails
80
+ default_lint_roller_plugin: RuboCop::MechaStyleRails::Plugin
81
+ rubygems_mfa_required: 'true'
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.2.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 4.0.20
97
+ specification_version: 4
98
+ summary: Custom RuboCop rules enforcing personal Ruby on Rails style.
99
+ test_files: []