lib/skeptic/rules/spaces_around_operators.rb in skeptic-0.0.11 vs lib/skeptic/rules/spaces_around_operators.rb in skeptic-0.0.12

- old
+ new

@@ -5,10 +5,12 @@ include SexpVisitor OPERATORS_WITHOUT_SPACES_AROUND_THEM = ['**', '::', '...', '..'] IGNORED_TOKEN_TYPES = [:on_sp, :on_ignored_nl, :on_nl, :on_lparen, :on_symbeg, :on_lbracket, :on_lbrace] + LEFT_LIMIT_TOKEN_TYPES = [:on_lparen, :on_lbracket] + RIGHT_LIMIT_TOKEN_TYPES = [:on_rparen, :on_rbracket] def initialize(data) @violations = [] @special_tokens_locations = [] end @@ -17,12 +19,11 @@ visit sexp @violations = tokens.each_cons(3).select do |_, token, _| operator_expecting_spaces? token end.select do |left, operator, right| - no_spaces_on_left_of?(operator, left) or - no_spaces_on_right_of?(operator, right) + no_spaces_around? operator, left, right end.map do |_, operator, _| [operator.last, operator.first[0]] end self end @@ -42,21 +43,39 @@ def operator_expecting_spaces?(token) token[1] == :on_op and not OPERATORS_WITHOUT_SPACES_AROUND_THEM.include? token.last end + def no_spaces_around?(operator, left, right) + if LEFT_LIMIT_TOKEN_TYPES.include?(left[1]) + mark_special_tokens right.first + end + if range_operator?(left) + mark_special_tokens left.last + end + no_spaces_on_left_of?(operator, left) or + no_spaces_on_right_of?(operator, right) + end + def no_spaces_on_left_of?(operator, neighbour) - neighbour.first[0] == operator.first[0] and neighbour[1] != :on_lparen and - !special_token? neighbour + neighbour.first[0] == operator.first[0] and + !LEFT_LIMIT_TOKEN_TYPES.include?(neighbour[1]) and + !range_operator?(neighbour) and + !special_token?(neighbour) end def no_spaces_on_right_of?(operator, neighbour) - neighbour.first[0] == operator.first[0] and neighbour[1] != :on_rparen and - !special_token? neighbour + neighbour.first[0] == operator.first[0] and + !RIGHT_LIMIT_TOKEN_TYPES.include?(neighbour[1]) and + !special_token?(neighbour) end def whitespace_token?(token) token[1] == :on_sp or token[1] == :on_ignored_nl + end + + def range_operator?(operator) + operator.last[0..1] == '..' end def mark_special_tokens(*token_locations) @special_tokens_locations.concat(token_locations) end