From e050d7cff841c2bf4e8327cda558645983acce31 Mon Sep 17 00:00:00 2001 From: Alex Dowad Date: Tue, 31 May 2016 07:57:09 +0200 Subject: [PATCH] [Fix #2722] ExtraSpacing doesn't align optarg equals with assignment equals --- CHANGELOG.md | 1 + lib/rubocop/cop/style/extra_spacing.rb | 13 +++++++++++++ spec/rubocop/cop/style/extra_spacing_spec.rb | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cdcfddc27d4..b377433d0c53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Bug fixes +* [#2722](https://github.com/bbatsov/rubocop/issues/2722): `Style/ExtraSpacing` does not attempt to align an equals sign in an argument list with one in an assignment statement. ([@alexdowad][]) * [#3133](https://github.com/bbatsov/rubocop/issues/3133): `Style/MultilineMethodCallBraceLayout` does not register offenses for single-line calls. ([@alexdowad][]) * [#3170](https://github.com/bbatsov/rubocop/issues/3170): `Style/MutableConstant` does not infinite-loop when correcting an array with no brackets. ([@alexdowad][]) * [#3150](https://github.com/bbatsov/rubocop/issues/3150): Fix auto-correct for Style/MultilineArrayBraceLayout. ([@jspanjers][]) diff --git a/lib/rubocop/cop/style/extra_spacing.rb b/lib/rubocop/cop/style/extra_spacing.rb index b1c936b9dbb7..b6a88de0f849 100644 --- a/lib/rubocop/cop/style/extra_spacing.rb +++ b/lib/rubocop/cop/style/extra_spacing.rb @@ -27,8 +27,15 @@ class ExtraSpacing < Cop MSG_UNALIGNED_ASGN = '`=` is not aligned with the %s assignment.'.freeze def investigate(processed_source) + return if processed_source.ast.nil? + if force_equal_sign_alignment? @asgn_tokens = processed_source.tokens.select { |t| equal_sign?(t) } + # we don't want to operate on equals signs which are part of an + # optarg in a method definition + # e.g.: def method(optarg = default_val); end + @asgn_tokens = remove_optarg_equals(@asgn_tokens, processed_source) + # Only attempt to align the first = on each line @asgn_tokens = Set.new(@asgn_tokens.uniq { |t| t.pos.line }) @asgn_lines = @asgn_tokens.map { |t| t.pos.line } @@ -205,6 +212,12 @@ def align_column(asgn_token) spaces = leading.size - (leading =~ / *\Z/) asgn_token.pos.last_column - spaces + 1 end + + def remove_optarg_equals(asgn_tokens, processed_source) + optargs = processed_source.ast.each_node(:optarg) + optarg_eql = optargs.map { |o| o.loc.operator.begin_pos }.to_set + asgn_tokens.reject { |t| optarg_eql.include?(t.pos.begin_pos) } + end end end end diff --git a/spec/rubocop/cop/style/extra_spacing_spec.rb b/spec/rubocop/cop/style/extra_spacing_spec.rb index 87892d40a00e..29dab55798cd 100644 --- a/spec/rubocop/cop/style/extra_spacing_spec.rb +++ b/spec/rubocop/cop/style/extra_spacing_spec.rb @@ -329,5 +329,13 @@ 'a.attribute_name = 2', 'abc[1] = 3'].join("\n")) end + + it 'does not register an offense when optarg equals is not aligned with ' \ + 'assignment equals sign' do + inspect_source(cop, ['def method(arg = 1)', + ' var = arg', + 'end']) + expect(cop.offenses).to be_empty + end end end