Class: RuboCop::Cop::Style::SpaceAroundKeyword

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/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 `%s` is missing.'.freeze
MSG_AFTER =
'Space after keyword `%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::ASGN_NODES, Util::BYTE_ORDER_MARK, Util::EQUALS_ASGN_NODES, Util::LITERAL_REGEX, Util::OPERATOR_METHODS, Util::SHORTHAND_ASGN_NODES

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offenses, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offense, all, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, cop_type, #correct, #debug?, #details, #display_cop_names?, #display_style_guide?, #excluded_file?, #extra_details?, #highlights, inherited, #initialize, #join_force?, lint?, match?, #message, #messages, non_rails, #parse, qualified_cop_name, #reference_url, #relevant_file?, #style_guide_url, #target_ruby_version

Methods included from Sexp

#s

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search, #node_search_body

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?, block_length, comment_line?, directions, double_quotes_acceptable?, double_quotes_required?, effective_column, ends_its_line?, escape_string, first_part_of_call_chain, interpret_string_escapes, line_range, move_pos, needs_escaping?, numeric_range_size, on_node, operator?, parentheses?, parenthesized_call?, range_with_surrounding_comma, range_with_surrounding_space, source_range, strip_quotes, to_string_literal, to_symbol_literal, within_node?

Methods included from PathUtil

absolute?, match_path?, relative_path

Constructor Details

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

Instance Method Details

#on_and(node) ⇒ Object



39
40
41
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 39

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

#on_block(node) ⇒ Object



43
44
45
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 43

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

#on_break(node) ⇒ Object



47
48
49
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 47

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

#on_case(node) ⇒ Object



51
52
53
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 51

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

#on_defined?(node) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 127

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

#on_ensure(node) ⇒ Object



55
56
57
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 55

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

#on_for(node) ⇒ Object



59
60
61
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 59

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

#on_if(node) ⇒ Object



63
64
65
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 63

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

#on_kwbegin(node) ⇒ Object



67
68
69
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 67

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

#on_next(node) ⇒ Object



71
72
73
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 71

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

#on_or(node) ⇒ Object



75
76
77
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 75

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

#on_postexe(node) ⇒ Object



79
80
81
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 79

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

#on_preexe(node) ⇒ Object



83
84
85
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 83

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

#on_resbody(node) ⇒ Object



87
88
89
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 87

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

#on_rescue(node) ⇒ Object



91
92
93
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 91

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

#on_return(node) ⇒ Object



95
96
97
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 95

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

#on_send(node) ⇒ Object



99
100
101
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 99

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

#on_super(node) ⇒ Object



103
104
105
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 103

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

#on_until(node) ⇒ Object



111
112
113
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 111

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

#on_when(node) ⇒ Object



115
116
117
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 115

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

#on_while(node) ⇒ Object



119
120
121
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 119

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

#on_yield(node) ⇒ Object



123
124
125
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 123

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

#on_zsuper(node) ⇒ Object



107
108
109
# File 'lib/rubocop/cop/style/space_around_keyword.rb', line 107

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