Class: RuboCop::Cop::Layout::SpaceAroundKeyword

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/layout/space_around_keyword.rb

Overview

Checks the spacing around the keywords.

Examples:


# bad
something 'test'do|x|
end

while(something)
end

something = 123if test

# good
something 'test' do |x|
end

while (something)
end

something = 123 if test

Constant Summary collapse

MSG_BEFORE =
'Space before keyword `%<range>s` is missing.'
MSG_AFTER =
'Space after keyword `%<range>s` is missing.'
DO =
'do'
SAFE_NAVIGATION =
'&.'
NAMESPACE_OPERATOR =
'::'
ACCEPT_LEFT_PAREN =
%w[break defined? next not rescue return super yield].freeze
ACCEPT_LEFT_SQUARE_BRACKET =
%w[super yield].freeze
ACCEPT_NAMESPACE_OPERATOR =
'super'
RESTRICT_ON_SEND =
%i[!].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_and(node) ⇒ Object



41
42
43
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 41

def on_and(node)
  check(node, [:operator].freeze) if node.keyword?
end

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



45
46
47
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 45

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  check(node, %i[begin end].freeze)
end

#on_break(node) ⇒ Object



49
50
51
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 49

def on_break(node)
  check(node, [:keyword].freeze)
end

#on_case(node) ⇒ Object



53
54
55
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 53

def on_case(node)
  check(node, %i[keyword else].freeze)
end

#on_case_match(node) ⇒ Object



57
58
59
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 57

def on_case_match(node)
  check(node, %i[keyword else].freeze)
end

#on_defined?(node) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 157

def on_defined?(node)
  check(node, [:keyword].freeze)
end

#on_ensure(node) ⇒ Object



61
62
63
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 61

def on_ensure(node)
  check(node, [:keyword].freeze)
end

#on_for(node) ⇒ Object



65
66
67
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 65

def on_for(node)
  check(node, %i[begin end].freeze)
end

#on_if(node) ⇒ Object



69
70
71
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 69

def on_if(node)
  check(node, %i[keyword else begin end].freeze, 'then')
end

#on_if_guard(node) ⇒ Object



73
74
75
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 73

def on_if_guard(node)
  check(node, [:keyword].freeze)
end

#on_in_pattern(node) ⇒ Object



77
78
79
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 77

def on_in_pattern(node)
  check(node, [:keyword].freeze)
end

#on_kwbegin(node) ⇒ Object



81
82
83
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 81

def on_kwbegin(node)
  check(node, %i[begin end].freeze, nil)
end

#on_match_pattern(node) ⇒ Object

Handle one-line pattern matching syntax (‘in`) with `Parser::Ruby27`.



86
87
88
89
90
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 86

def on_match_pattern(node)
  return if target_ruby_version >= 3.0

  check(node, [:operator].freeze)
end

#on_match_pattern_p(node) ⇒ Object

Handle one-line pattern matching syntax (‘in`) with `Parser::Ruby30`.



93
94
95
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 93

def on_match_pattern_p(node)
  check(node, [:operator].freeze)
end

#on_next(node) ⇒ Object



97
98
99
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 97

def on_next(node)
  check(node, [:keyword].freeze)
end

#on_or(node) ⇒ Object



101
102
103
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 101

def on_or(node)
  check(node, [:operator].freeze) if node.keyword?
end

#on_postexe(node) ⇒ Object



105
106
107
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 105

def on_postexe(node)
  check(node, [:keyword].freeze)
end

#on_preexe(node) ⇒ Object



109
110
111
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 109

def on_preexe(node)
  check(node, [:keyword].freeze)
end

#on_resbody(node) ⇒ Object



113
114
115
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 113

def on_resbody(node)
  check(node, [:keyword].freeze)
end

#on_rescue(node) ⇒ Object



117
118
119
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 117

def on_rescue(node)
  check(node, [:else].freeze)
end

#on_return(node) ⇒ Object



121
122
123
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 121

def on_return(node)
  check(node, [:keyword].freeze)
end

#on_send(node) ⇒ Object



125
126
127
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 125

def on_send(node)
  check(node, [:selector].freeze) if node.prefix_not?
end

#on_super(node) ⇒ Object



129
130
131
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 129

def on_super(node)
  check(node, [:keyword].freeze)
end

#on_unless_guard(node) ⇒ Object



137
138
139
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 137

def on_unless_guard(node)
  check(node, [:keyword].freeze)
end

#on_until(node) ⇒ Object



141
142
143
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 141

def on_until(node)
  check(node, %i[begin end keyword].freeze)
end

#on_when(node) ⇒ Object



145
146
147
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 145

def on_when(node)
  check(node, [:keyword].freeze)
end

#on_while(node) ⇒ Object



149
150
151
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 149

def on_while(node)
  check(node, %i[begin end keyword].freeze)
end

#on_yield(node) ⇒ Object



153
154
155
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 153

def on_yield(node)
  check(node, [:keyword].freeze)
end

#on_zsuper(node) ⇒ Object



133
134
135
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 133

def on_zsuper(node)
  check(node, [:keyword].freeze)
end