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

Inherits:
Cop
  • Object
show all
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.'.freeze
MSG_AFTER =
'Space after keyword `%<range>s` is missing.'.freeze
DO =
'do'.freeze
SAFE_NAVIGATION =
'&.'.freeze
ACCEPT_LEFT_PAREN =
%w[break defined? next not rescue return super yield].freeze
ACCEPT_LEFT_SQUARE_BRACKET =
%w[super yield].freeze

Constants included from Util

Util::LITERAL_REGEX

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offenses, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offense, all, autocorrect_incompatible_with, badge, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, #correct, department, #duplicate_location?, #excluded_file?, #find_location, #highlights, inherited, #initialize, #join_force?, lint?, match?, #message, #messages, non_rails, #parse, qualified_cop_name, #relevant_file?, #target_rails_version, #target_ruby_version

Methods included from AST::Sexp

#s

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search, #node_search, #node_search_all, #node_search_body, #node_search_first

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

begins_its_line?, comment_line?, double_quotes_required?, escape_string, first_part_of_call_chain, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, tokens

Methods included from PathUtil

absolute?, hidden_dir?, hidden_file_in_not_hidden_dir?, match_path?, pwd, relative_path, reset_pwd, smart_path

Constructor Details

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

Instance Method Details

#autocorrect(range) ⇒ Object



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

def autocorrect(range)
  if space_before_missing?(range)
    ->(corrector) { corrector.insert_before(range, ' '.freeze) }
  else
    ->(corrector) { corrector.insert_after(range, ' '.freeze) }
  end
end

#on_and(node) ⇒ Object



38
39
40
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 38

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

#on_block(node) ⇒ Object



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

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

#on_break(node) ⇒ Object



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

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

#on_case(node) ⇒ Object



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

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

#on_defined?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#on_ensure(node) ⇒ Object



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

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

#on_for(node) ⇒ Object



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

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

#on_if(node) ⇒ Object



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

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

#on_kwbegin(node) ⇒ Object



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

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

#on_next(node) ⇒ Object



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

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

#on_or(node) ⇒ Object



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

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

#on_postexe(node) ⇒ Object



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

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

#on_preexe(node) ⇒ Object



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

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

#on_resbody(node) ⇒ Object



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

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

#on_rescue(node) ⇒ Object



90
91
92
# File 'lib/rubocop/cop/layout/space_around_keyword.rb', line 90

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

#on_return(node) ⇒ Object



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

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

#on_send(node) ⇒ Object



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

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

#on_super(node) ⇒ Object



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

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

#on_until(node) ⇒ Object



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

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

#on_when(node) ⇒ Object



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

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

#on_while(node) ⇒ Object



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

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

#on_yield(node) ⇒ Object



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

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

#on_zsuper(node) ⇒ Object



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

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