class RuboCop::Cop::Naming::VariableName
Checks that the configured style (snake_case or camelCase) is used for all variable names. This includes local variables, instance variables, class variables, method arguments (positional, keyword, rest or block), and block arguments.
The cop can also be configured to forbid using specific names for variables, using ‘ForbiddenIdentifiers` or `ForbiddenPatterns`. In addition to the above, this applies to global variables as well.
Method definitions and method calls are not affected by this cop.
@example EnforcedStyle: snake_case (default)
# bad fooBar = 1 # good foo_bar = 1
@example EnforcedStyle: camelCase
# bad foo_bar = 1 # good fooBar = 1
@example AllowedIdentifiers: [‘fooBar’]
# good (with EnforcedStyle: snake_case) fooBar = 1
@example AllowedPatterns: [‘_vd+z’]
# good (with EnforcedStyle: camelCase) release_v1 = true
@example ForbiddenIdentifiers: [‘fooBar’]
# bad (in all cases) fooBar = 1 @fooBar = 1 @@fooBar = 1 $fooBar = 1
@example ForbiddenPatterns: [‘_vd+z’]
# bad (in all cases) release_v1 = true @release_v1 = true @@release_v1 = true $release_v1 = true
Constants
- MSG
- MSG_FORBIDDEN
Public Instance Methods
Source
# File lib/rubocop/cop/naming/variable_name.rb, line 88 def on_gvasgn(node) return unless (name = node.name) return unless forbidden_name?(name) register_forbidden_name(node) end
Only forbidden names are checked for global variable assignment
Source
# File lib/rubocop/cop/naming/variable_name.rb, line 66 def on_lvasgn(node) return unless (name = node.name) return if allowed_identifier?(name) if forbidden_name?(name) register_forbidden_name(node) else check_name(node, name, node.loc.name) end end
Source
# File lib/rubocop/cop/naming/variable_name.rb, line 62 def valid_name?(node, name, given_style = style) super || matches_allowed_pattern?(name) end
Private Instance Methods
Source
# File lib/rubocop/cop/naming/variable_name.rb, line 97 def forbidden_name?(name) forbidden_identifier?(name) || forbidden_pattern?(name) end
Source
# File lib/rubocop/cop/naming/variable_name.rb, line 101 def message(style) format(MSG, style: style) end
Source
# File lib/rubocop/cop/naming/variable_name.rb, line 105 def register_forbidden_name(node) message = format(MSG_FORBIDDEN, identifier: node.name) add_offense(node.loc.name, message: message) end