lib/skeptic/rules/spaces_around_operators.rb in skeptic-0.0.12 vs lib/skeptic/rules/spaces_around_operators.rb in skeptic-0.0.14
- old
+ new
@@ -7,19 +7,23 @@
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]
+ WHITESPACE_TOKEN_TYPES = [:on_sp, :on_nl, :on_ignored_nl]
def initialize(data)
@violations = []
@special_tokens_locations = []
+ @unary_token_locations = []
end
def apply_to(code, tokens, sexp)
visit sexp
+ mark_unary tokens
+
@violations = tokens.each_cons(3).select do |_, token, _|
operator_expecting_spaces? token
end.select do |left, operator, right|
no_spaces_around? operator, left, right
end.map do |_, operator, _|
@@ -44,10 +48,12 @@
token[1] == :on_op and
not OPERATORS_WITHOUT_SPACES_AROUND_THEM.include? token.last
end
def no_spaces_around?(operator, left, right)
+ return false if unary_operator?(operator)
+
if LEFT_LIMIT_TOKEN_TYPES.include?(left[1])
mark_special_tokens right.first
end
if range_operator?(left)
mark_special_tokens left.last
@@ -67,14 +73,10 @@
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)
@@ -82,9 +84,28 @@
end
def special_token?(token)
IGNORED_TOKEN_TYPES.include? token[1] or
@special_tokens_locations.include? token.first
+ end
+
+ def unary_operator?(token)
+ token[1] == :on_op and @unary_token_locations.include?(token[0])
+ end
+
+ def mark_unary(tokens)
+ @unary_token_locations = []
+ last_significant_token = nil
+ tokens.each do |token|
+ if token[1] == :on_op
+ if last_significant_token == :on_op
+ @unary_token_locations << token[0]
+ end
+ last_significant_token = :on_op
+ elsif !WHITESPACE_TOKEN_TYPES.include?(token[1])
+ last_significant_token = token[1]
+ end
+ end
end
on :blockarg, :rest_param do |ident|
mark_special_tokens ident.last
end