lib/rubocop/cop/surrounding_space.rb in rubocop-0.2.1 vs lib/rubocop/cop/surrounding_space.rb in rubocop-0.3.0
- old
+ new
@@ -2,66 +2,16 @@
require_relative 'grammar'
module Rubocop
module Cop
- class SurroundingSpace < Cop
- ERROR_MESSAGE = 'Surrounding space missing for '
-
+ module SurroundingSpace
def inspect(file, source, tokens, sexp)
@correlations.sort.each do |ix, grammar_path|
- t = tokens[ix]
- case t.type
- when :on_op
- unless surrounded_by_whitespace?(tokens[ix - 1, 3])
- unless ok_without_spaces?(grammar_path)
- add_offence(:convention, t.pos.lineno,
- ERROR_MESSAGE + "operator '#{t.text}'.")
- end
- end
- when :on_lbrace
- unless surrounded_by_whitespace?(tokens[ix - 1, 3])
- add_offence(:convention, t.pos.lineno, ERROR_MESSAGE + "'{'.")
- end
- when :on_rbrace
- unless whitespace?(tokens[ix - 1])
- add_offence(:convention, t.pos.lineno,
- "Space missing to the left of '}'.")
- end
- end
+ check_missing(tokens, ix, grammar_path)
end
- tokens.each_index do |ix|
- t = tokens[ix]
- prev, nxt = tokens.values_at(ix - 1, ix + 1)
- offence_detected = case t.type
- when :on_lbracket, :on_lparen
- nxt.type == :on_sp
- when :on_rbracket, :on_rparen
- if prev.type == :on_sp
- prev_ns = previous_non_space(tokens, ix)
- prev_ns && tokens_on_same_row?(prev_ns,
- tokens[ix]) &&
- # Avoid double repoting of [ ] and ( )
- prev_ns.type != :on_lbracket &&
- prev_ns.type != :on_lparen
- end
- when :on_op
- t.text == '**' &&
- (whitespace?(prev) || whitespace?(nxt))
- end
- if offence_detected
- kind = case t.type
- when :on_lparen, :on_rparen
- 'inside parentheses'
- when :on_lbracket, :on_rbracket
- 'inside square brackets'
- when :on_op
- "around operator #{t.text}"
- end
- add_offence(:convention, t.pos.lineno, "Space #{kind} detected.")
- end
- end
+ tokens.each_index { |ix| check_extra(tokens, ix) }
end
private
def tokens_on_same_row?(t1, t2)
@@ -85,9 +35,108 @@
end
def surrounded_by_whitespace?(nearby_tokens)
left, _, right = nearby_tokens
whitespace?(left) && whitespace?(right)
+ end
+
+ # Default implementation for classes that don't need it.
+ def check_missing(tokens, ix, grammar_path)
+ end
+ end
+
+ class SpaceAroundOperators < Cop
+ include SurroundingSpace
+ ERROR_MESSAGE = 'Surrounding space missing for operator '
+
+ def check_missing(tokens, ix, grammar_path)
+ t = tokens[ix]
+ if t.type == :on_op
+ unless surrounded_by_whitespace?(tokens[ix - 1, 3])
+ unless ok_without_spaces?(grammar_path)
+ add_offence(:convention, t.pos.lineno,
+ ERROR_MESSAGE + "'#{t.text}'.")
+ end
+ end
+ end
+ end
+
+ def check_extra(tokens, ix)
+ prev, t, nxt = tokens.values_at(ix - 1, ix, ix + 1)
+ if t.type == :on_op && t.text == '**' &&
+ (whitespace?(prev) || whitespace?(nxt))
+ add_offence(:convention, t.pos.lineno,
+ "Space around operator #{t.text} detected.")
+ end
+ end
+ end
+
+ class SpaceAroundBraces < Cop
+ include SurroundingSpace
+
+ def check_extra(tokens, ix)
+ end
+
+ def check_missing(tokens, ix, grammar_path)
+ t = tokens[ix]
+ case t.type
+ when :on_lbrace
+ unless surrounded_by_whitespace?(tokens[ix - 1, 3])
+ add_offence(:convention, t.pos.lineno,
+ "Surrounding space missing for '{'.")
+ end
+ when :on_rbrace
+ unless whitespace?(tokens[ix - 1])
+ add_offence(:convention, t.pos.lineno,
+ "Space missing to the left of '}'.")
+ end
+ end
+ end
+ end
+
+ class SpaceInsideParens < Cop
+ include SurroundingSpace
+ def check_extra(tokens, ix)
+ prev, t, nxt = tokens.values_at(ix - 1, ix, ix + 1)
+ offence_detected = case t.type
+ when :on_lparen
+ nxt.type == :on_sp
+ when :on_rparen
+ if prev.type == :on_sp
+ prev_ns = previous_non_space(tokens, ix)
+ prev_ns && tokens_on_same_row?(prev_ns,
+ tokens[ix]) &&
+ # Avoid double repoting of ( )
+ prev_ns.type != :on_lparen
+ end
+ end
+ if offence_detected
+ add_offence(:convention, t.pos.lineno,
+ 'Space inside parentheses detected.')
+ end
+ end
+ end
+
+ class SpaceInsideBrackets < Cop
+ include SurroundingSpace
+ def check_extra(tokens, ix)
+ prev, t, nxt = tokens.values_at(ix - 1, ix, ix + 1)
+ offence_detected = case t.type
+ when :on_lbracket
+ nxt.type == :on_sp
+ when :on_rbracket
+ if prev.type == :on_sp
+ prev_ns = previous_non_space(tokens, ix)
+ prev_ns && tokens_on_same_row?(prev_ns,
+ tokens[ix]) &&
+ # Avoid double repoting of [ ] and ( )
+ prev_ns.type != :on_lbracket
+ end
+ end
+ if offence_detected
+ add_offence(:convention, t.pos.lineno,
+ 'Space inside square brackets detected.')
+ end
end
end
end
end