class RubbyCop::Cop::Layout::SpaceAroundKeyword
Checks the spacing around the keywords.
@example
# 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
Constants
- ACCEPT_LEFT_PAREN
- ACCEPT_LEFT_SQUARE_BRACKET
- DO
- MSG_AFTER
- MSG_BEFORE
- SAFE_NAVIGATION
Public Instance Methods
on_and(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 38 def on_and(node) check(node, [:operator].freeze) if node.keyword? end
on_block(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 42 def on_block(node) check(node, %i[begin end].freeze) end
on_break(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 46 def on_break(node) check(node, [:keyword].freeze) end
on_case(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 50 def on_case(node) check(node, %i[keyword else].freeze) end
on_defined?(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 126 def on_defined?(node) check(node, [:keyword].freeze) end
on_ensure(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 54 def on_ensure(node) check(node, [:keyword].freeze) end
on_for(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 58 def on_for(node) check(node, %i[begin end].freeze) end
on_if(node)
click to toggle source
# File lib/rubbycop/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)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 66 def on_kwbegin(node) check(node, %i[begin end].freeze, nil) end
on_next(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 70 def on_next(node) check(node, [:keyword].freeze) end
on_or(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 74 def on_or(node) check(node, [:operator].freeze) if node.keyword? end
on_postexe(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 78 def on_postexe(node) check(node, [:keyword].freeze) end
on_preexe(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 82 def on_preexe(node) check(node, [:keyword].freeze) end
on_resbody(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 86 def on_resbody(node) check(node, [:keyword].freeze) end
on_rescue(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 90 def on_rescue(node) check(node, [:else].freeze) end
on_return(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 94 def on_return(node) check(node, [:keyword].freeze) end
on_send(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 98 def on_send(node) check(node, [:selector].freeze) if node.keyword_not? end
on_super(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 102 def on_super(node) check(node, [:keyword].freeze) end
on_until(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 110 def on_until(node) check(node, %i[begin end keyword].freeze) end
on_when(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 114 def on_when(node) check(node, [:keyword].freeze) end
on_while(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 118 def on_while(node) check(node, %i[begin end keyword].freeze) end
on_yield(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 122 def on_yield(node) check(node, [:keyword].freeze) end
on_zsuper(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 106 def on_zsuper(node) check(node, [:keyword].freeze) end
Private Instance Methods
accept_left_parenthesis?(range)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 191 def accept_left_parenthesis?(range) ACCEPT_LEFT_PAREN.include?(range.source) end
accept_left_square_bracket?(range)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 195 def accept_left_square_bracket?(range) ACCEPT_LEFT_SQUARE_BRACKET.include?(range.source) end
autocorrect(range)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 214 def autocorrect(range) if space_before_missing?(range) ->(corrector) { corrector.insert_before(range, ' '.freeze) } else ->(corrector) { corrector.insert_after(range, ' '.freeze) } end end
check(node, locations, begin_keyword = DO)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 132 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)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 146 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)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 152 def check_end(node, range, begin_keyword) return if begin_keyword == DO && !do?(node) offense(range, MSG_BEFORE) if space_before_missing?(range) end
check_keyword(node, range)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 162 def check_keyword(node, range) offense(range, MSG_BEFORE) if space_before_missing?(range) && !preceded_by_operator?(node, range) offense(range, MSG_AFTER) if space_after_missing?(range) end
do?(node)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 158 def do?(node) node.loc.begin && node.loc.begin.is?(DO) end
offense(range, msg)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 168 def offense(range, msg) add_offense(range, range, msg % range.source) end
preceded_by_operator?(node, _range)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 203 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.and_type? || ancestor.or_type? return false unless ancestor.send_type? return true if operator?(ancestor.method_name) end false end
space_after_missing?(range)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 178 def space_after_missing?(range) pos = range.end_pos char = range.source_buffer.source[pos] return false unless char return false if accept_left_parenthesis?(range) && char == '('.freeze return false if accept_left_square_bracket?(range) && char == '['.freeze return false if safe_navigation_call?(range, pos) char !~ /[\s;,#\\\)\}\]\.]/ end
space_before_missing?(range)
click to toggle source
# File lib/rubbycop/cop/layout/space_around_keyword.rb, line 172 def space_before_missing?(range) pos = range.begin_pos - 1 return false if pos < 0 range.source_buffer.source[pos] !~ /[\s\(\|\{\[;,\*\=]/ end