Last active
March 27, 2024 15:46
-
-
Save louishuyng/3985a98807404bcee0c0cd87570a41b5 to your computer and use it in GitHub Desktop.
Custom Rubocop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require: | |
- ./lib/rubocop/cop/lint_rails_env | |
Lint/RailsEnv: | |
Enabled: true | |
Exclude: | |
- 'config/**/*.rb' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RuboCop | |
module Cop | |
module Lint | |
class RailsEnv < RuboCop::Cop::Cop | |
MSG = "Avoid Rails.env in application code, " \ | |
"use configuration parameters instead" | |
def_node_matcher :rails_env?, <<~PATTERN | |
(send {(const nil? :Rails) (const (cbase) | |
:Rails)} :env) | |
PATTERN | |
def on_send(node) | |
return unless rails_env?(node) | |
add_offense( | |
node.parent.type == :send ? node.parent : node, | |
location: :selector, message: MSG | |
) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment