lib/rubocop/cop/style/extra_spacing.rb in rubocop-0.34.1 vs lib/rubocop/cop/style/extra_spacing.rb in rubocop-0.34.2

- old
+ new

@@ -19,27 +19,45 @@ # website = "https://github.com/bbatsov/rubocop" class ExtraSpacing < Cop MSG = 'Unnecessary spacing detected.' def investigate(processed_source) + ast = processed_source.ast + ignored_ranges = ast ? ignored_ranges(ast) : [] + processed_source.tokens.each_cons(2) do |t1, t2| next if t2.type == :tNL next if t1.pos.line != t2.pos.line next if t2.pos.begin_pos - 1 <= t1.pos.end_pos next if allow_for_alignment? && aligned_with_something?(t2) start_pos = t1.pos.end_pos end_pos = t2.pos.begin_pos - 1 range = Parser::Source::Range.new(processed_source.buffer, start_pos, end_pos) - add_offense(range, range, MSG) + add_offense(range, range, MSG) unless ignored_ranges.include?(range) end end def autocorrect(range) ->(corrector) { corrector.remove(range) } end private + + # Returns an array of ranges that should not be reported. It's the + # extra spaces between the separators (: or =>) and values in a hash, + # since those are handled by the Style/AlignHash cop. + def ignored_ranges(ast) + ranges = [] + on_node(:pair, ast) do |pair| + _, value = *pair + ranges << + Parser::Source::Range.new(processed_source.buffer, + pair.loc.operator.end_pos, + value.loc.expression.begin_pos - 1) + end + ranges + end def allow_for_alignment? cop_config['AllowForAlignment'] end