lib/rubocop/cop/style/extra_spacing.rb in rubocop-0.33.0 vs lib/rubocop/cop/style/extra_spacing.rb in rubocop-0.34.0

- old
+ new

@@ -47,11 +47,17 @@ def aligned_with_something?(token) return aligned_comments?(token) if token.type == :tCOMMENT pre = (token.pos.line - 2).downto(0) post = token.pos.line.upto(processed_source.lines.size - 1) - aligned_with?(pre, token) || aligned_with?(post, token) + return true if aligned_with?(pre, token) || aligned_with?(post, token) + + # If no aligned token was found, search for an aligned token on the + # nearest line with the same indentation as the checked line. + base_indentation = processed_source.lines[token.pos.line - 1] =~ /\S/ + aligned_with?(pre, token, base_indentation) || + aligned_with?(post, token, base_indentation) end def aligned_comments?(token) ix = processed_source.comments.index do |c| c.loc.expression.begin_pos == token.pos.begin_pos @@ -70,15 +76,23 @@ def comment_column(ix) processed_source.comments[ix].loc.column end - def aligned_with?(indices_to_check, token) + # Returns true if the previous or next line, not counting empty or + # comment lines, contains a token that's aligned with the given + # token. If base_indentation is given, lines with different indentation + # than the base indentation are also skipped. + def aligned_with?(indices_to_check, token, base_indentation = nil) indices_to_check.each do |ix| next if comment_lines.include?(ix + 1) line = processed_source.lines[ix] next if line.strip.empty? + if base_indentation + indentation = line =~ /\S/ + next if indentation != base_indentation + end return (aligned_words?(token, line) || aligned_assignments?(token, line) || aligned_same_character?(token, line)) end false # No line to check was found. @@ -102,10 +116,10 @@ token.type == :tOP_ASGN && line[token.pos.column + token.text.length] == '=' end def aligned_same_character?(token, line) - line[token.pos.column] == token.text[0] + line[token.pos.column] == token.text.to_s[0] end end end end end