Class: RuboCop::Cop::Layout::SpaceAroundKeyword
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::RuboCop::Cop::AutoCorrector ,
::RuboCop::Cop::Base ,
::RuboCop::ExcludeLimit ,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
Instance Chain:
self,
::RuboCop::Cop::Base ,
::RuboCop::Cop::AutocorrectLogic ,
::RuboCop::Cop::IgnoredNode ,
::RuboCop::Util ,
RuboCop::AST::Sexp
|
|
Inherits: |
RuboCop::Cop::Base
|
Defined in: | lib/rubocop/cop/layout/space_around_keyword.rb |
Overview
Checks the spacing around the keywords.
Constant Summary
-
ACCEPT_LEFT_PAREN =
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 36%w[break defined? next not rescue return super yield].freeze
-
ACCEPT_LEFT_SQUARE_BRACKET =
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 37%w[super yield].freeze
-
ACCEPT_NAMESPACE_OPERATOR =
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 38'super'
-
DO =
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 33'do'
-
MSG_AFTER =
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 31'Space after keyword `%<range>s` is missing.'
-
MSG_BEFORE =
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 30'Space before keyword `%<range>s` is missing.'
-
NAMESPACE_OPERATOR =
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 35'::'
-
SAFE_NAVIGATION =
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 34'&.'
::RuboCop::Cop::Base
- Inherited
Class Attribute Summary
::RuboCop::Cop::AutoCorrector
- Extended
::RuboCop::Cop::Base
- Inherited
.gem_requirements, .lint?, | |
.support_autocorrect? | Returns if class supports autocorrect. |
.support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to |
Class Method Summary
::RuboCop::Cop::Base
- Inherited
.autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
.badge | Naming. |
.callbacks_needed, .cop_name, .department, | |
.documentation_url | Returns a url to view this cops documentation online. |
.exclude_from_registry | Call for abstract Cop classes. |
.inherited, | |
.joining_forces | Override and return the Force class(es) you need to join. |
.match? | Returns true if the cop name or the cop namespace matches any of the given names. |
.new, | |
.requires_gem | Register a version requirement for the given gem name. |
.restrict_on_send |
::RuboCop::ExcludeLimit
- Extended
exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
transform |
Instance Attribute Summary
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #on_and(node)
- #on_block(node)
- #on_break(node)
- #on_case(node)
- #on_case_match(node)
- #on_defined?(node) ⇒ Boolean
- #on_ensure(node)
- #on_for(node)
- #on_if(node)
- #on_if_guard(node)
- #on_in_pattern(node)
- #on_kwbegin(node)
-
#on_match_pattern(node)
Handle one-line pattern matching syntax (
in
) withParser::Ruby27
. -
#on_match_pattern_p(node)
Handle one-line pattern matching syntax (
in
) withParser::Ruby30
. - #on_next(node)
- #on_or(node)
- #on_postexe(node)
- #on_preexe(node)
- #on_resbody(node)
- #on_rescue(node)
- #on_return(node)
- #on_send(node)
- #on_super(node)
- #on_unless_guard(node)
- #on_until(node)
- #on_when(node)
- #on_while(node)
- #on_yield(node)
- #on_zsuper(node)
- #accept_left_parenthesis?(range) ⇒ Boolean private
- #accept_left_square_bracket?(range) ⇒ Boolean private
- #accept_namespace_operator?(range) ⇒ Boolean private
- #accepted_opening_delimiter?(range, char) ⇒ Boolean private
- #check(node, locations, begin_keyword = DO) private
- #check_begin(node, range, begin_keyword) private
- #check_end(node, range, begin_keyword) private
- #check_keyword(node, range) private
- #do?(node) ⇒ Boolean private
- #namespace_operator?(range, pos) ⇒ Boolean private
- #preceded_by_operator?(node, _range) ⇒ Boolean private
- #safe_navigation_call?(range, pos) ⇒ Boolean private
- #space_after_missing?(range) ⇒ Boolean private
- #space_before_missing?(range) ⇒ Boolean private
::RuboCop::Cop::Base
- Inherited
#add_global_offense | Adds an offense that has no particular location. |
#add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
#begin_investigation | Called before any investigation. |
#callbacks_needed, | |
#cop_config | Configuration Helpers. |
#cop_name, #excluded_file?, | |
#external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
#inspect, | |
#message | Gets called if no message is specified when calling |
#name | Alias for Base#cop_name. |
#offenses, | |
#on_investigation_end | Called after all on_… |
#on_new_investigation | Called before all on_… |
#on_other_file | Called instead of all on_… |
#parse | There should be very limited reasons for a Cop to do it’s own parsing. |
#parser_engine, | |
#ready | Called between investigations. |
#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
#callback_argument | Reserved for Cop::Cop. |
#complete_investigation | Called to complete an investigation. |
#correct, #current_corrector, | |
#current_offense_locations | Reserved for Commissioner: |
#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range, | |
#reset_investigation | Actually private methods. |
#use_corrector |
::RuboCop::Cop::AutocorrectLogic
- Included
#disable_offense, #disable_offense_at_end_of_line, #disable_offense_before_and_after, #disable_offense_with_eol_or_surround_comment, #max_line_length, | |
#range_by_lines | Expand the given range to include all of any lines it covers. |
#range_of_first_line, #range_overlaps_offense?, #string_continuation, #string_continuation?, #surrounding_heredoc, #surrounding_percent_array |
::RuboCop::Cop::IgnoredNode
- Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#accept_left_parenthesis?(range) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 235
def accept_left_parenthesis?(range) ACCEPT_LEFT_PAREN.include?(range.source) end
#accept_left_square_bracket?(range) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 239
def accept_left_square_bracket?(range) ACCEPT_LEFT_SQUARE_BRACKET.include?(range.source) end
#accept_namespace_operator?(range) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 243
def accept_namespace_operator?(range) range.source == ACCEPT_NAMESPACE_OPERATOR end
#accepted_opening_delimiter?(range, char) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 228
def accepted_opening_delimiter?(range, char) return true unless char (accept_left_square_bracket?(range) && char == '[') || (accept_left_parenthesis?(range) && char == '(') end
#check(node, locations, begin_keyword = DO) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 162
def check(node, locations, begin_keyword = DO) locations.each do |loc| next unless node.loc.respond_to?(loc) range = node.loc.public_send(loc) next unless range case loc when :begin then check_begin(node, range, begin_keyword) when :end then check_end(node, range, begin_keyword) else check_keyword(node, range) end end end
#check_begin(node, range, begin_keyword) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 177
def check_begin(node, range, begin_keyword) return if begin_keyword && !range.is?(begin_keyword) check_keyword(node, range) end
#check_end(node, range, begin_keyword) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 183
def check_end(node, range, begin_keyword) return if begin_keyword == DO && !do?(node) return unless space_before_missing?(range) add_offense(range, message: format(MSG_BEFORE, range: range.source)) do |corrector| corrector.insert_before(range, ' ') end end
#check_keyword(node, range) (private)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 196
def check_keyword(node, range) if space_before_missing?(range) && !preceded_by_operator?(node, range) add_offense(range, message: format(MSG_BEFORE, range: range.source)) do |corrector| corrector.insert_before(range, ' ') end end return unless space_after_missing?(range) add_offense(range, message: format(MSG_AFTER, range: range.source)) do |corrector| corrector.insert_after(range, ' ') end end
#do?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 192
def do?(node) node.loc.begin&.is?(DO) end
#namespace_operator?(range, pos) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 251
def namespace_operator?(range, pos) range.source_buffer.source[pos, 2].start_with?(NAMESPACE_OPERATOR) end
#on_and(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 40
def on_and(node) check(node, [:operator].freeze) if node.keyword? end
#on_block(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 44
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler check(node, %i[begin end].freeze) end
#on_break(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 48
def on_break(node) check(node, [:keyword].freeze) end
#on_case(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 52
def on_case(node) check(node, %i[keyword else].freeze) end
#on_case_match(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 56
def on_case_match(node) check(node, %i[keyword else].freeze) end
#on_defined?(node) ⇒ Boolean
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 156
def on_defined?(node) check(node, [:keyword].freeze) end
#on_ensure(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 60
def on_ensure(node) check(node, [:keyword].freeze) end
#on_for(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 64
def on_for(node) check(node, %i[begin end].freeze) end
#on_if(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 68
def on_if(node) check(node, %i[keyword else begin end].freeze, 'then') end
#on_if_guard(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 72
def on_if_guard(node) check(node, [:keyword].freeze) end
#on_in_pattern(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 76
def on_in_pattern(node) check(node, [:keyword].freeze) end
#on_kwbegin(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 80
def on_kwbegin(node) check(node, %i[begin end].freeze, nil) end
#on_match_pattern(node)
Handle one-line pattern matching syntax (in
) with Parser::Ruby27
.
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 85
def on_match_pattern(node) return if target_ruby_version >= 3.0 check(node, [:operator].freeze) end
#on_match_pattern_p(node)
Handle one-line pattern matching syntax (in
) with Parser::Ruby30
.
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 92
def on_match_pattern_p(node) check(node, [:operator].freeze) end
#on_next(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 96
def on_next(node) check(node, [:keyword].freeze) end
#on_or(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 100
def on_or(node) check(node, [:operator].freeze) if node.keyword? end
#on_postexe(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 104
def on_postexe(node) check(node, [:keyword].freeze) end
#on_preexe(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 108
def on_preexe(node) check(node, [:keyword].freeze) end
#on_resbody(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 112
def on_resbody(node) check(node, [:keyword].freeze) end
#on_rescue(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 116
def on_rescue(node) check(node, [:else].freeze) end
#on_return(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 120
def on_return(node) check(node, [:keyword].freeze) end
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 124
def on_send(node) check(node, [:selector].freeze) if node.prefix_not? end
#on_super(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 128
def on_super(node) check(node, [:keyword].freeze) end
#on_unless_guard(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 136
def on_unless_guard(node) check(node, [:keyword].freeze) end
#on_until(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 140
def on_until(node) check(node, %i[begin end keyword].freeze) end
#on_when(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 144
def on_when(node) check(node, [:keyword].freeze) end
#on_while(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 148
def on_while(node) check(node, %i[begin end keyword].freeze) end
#on_yield(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 152
def on_yield(node) check(node, [:keyword].freeze) end
#on_zsuper(node)
[ GitHub ]# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 132
def on_zsuper(node) check(node, [:keyword].freeze) end
#preceded_by_operator?(node, _range) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 255
def preceded_by_operator?(node, _range) # regular dotted method calls bind more tightly than operators # so we need to climb up the AST past them node.each_ancestor do |ancestor| return true if ancestor.operator_keyword? || ancestor.range_type? return false unless ancestor.send_type? return true if ancestor.operator_method? end false end
#space_after_missing?(range) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 217
def space_after_missing?(range) pos = range.end_pos char = range.source_buffer.source[pos] return false if accepted_opening_delimiter?(range, char) return false if (range, pos) return false if accept_namespace_operator?(range) && namespace_operator?(range, pos) !/[\s;,#\\)}\].]/.match?(char) end
#space_before_missing?(range) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 210
def space_before_missing?(range) pos = range.begin_pos - 1 return false if pos.negative? !/[\s(|{\[;,*=]/.match?(range.source_buffer.source[pos]) end