From ce886699fcabd91c38d04f8684b59f8dede41f2b Mon Sep 17 00:00:00 2001 From: Jonas Arvidsson Date: Thu, 30 Apr 2020 18:47:37 +0200 Subject: [PATCH] [Fix #7885] Consider Layout/IndentationStyle:EnforcedStyle When calculating how wide the indentation is, we must look at the `EnforcedStyle` of the `Layout/IndentationStyle` cop. If it's `spaces`, then the indentation multiplier is 1. If it's `tabs` it's a bit more complicated, but that part was already handled correctly. The specs needed to be updated with correct configuration for all cases with `tabs` indentation. Whether or not we should consider the `Enabled` parameter is debatable. I've chosen to not care about `Enabled`, and to use the other parameter settings in any case. --- CHANGELOG.md | 1 + lib/rubocop/cop/mixin/statement_modifier.rb | 5 +++-- .../cop/style/if_unless_modifier_spec.rb | 17 ++++++++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6b475429a1..37294faec83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * [#7905](https://github.com/rubocop-hq/rubocop/pull/7905): Fix an error when running `rubocop --only` or `rubocop --except` options without cop name argument. ([@koic][]) * [#7903](https://github.com/rubocop-hq/rubocop/pull/7903): Fix an incorrect autocorrect for `Style/HashTransformKeys` and `Style/HashTransformValues` cops when line break before `to_h` method. ([@diogoosorio][], [@koic][]) * [#7899](https://github.com/rubocop-hq/rubocop/issues/7899): Fix an infinite loop error for `Layout/SpaceAroundOperators` with `Layout/ExtraSpacing` when using `ForceEqualSignAlignment: true`. ([@koic][]) +* [#7885](https://github.com/rubocop-hq/rubocop/issues/7885): Fix `Style/IfUnlessModifier` logic when tabs are used for indentation. ([@jonas054][]) ### Changes diff --git a/lib/rubocop/cop/mixin/statement_modifier.rb b/lib/rubocop/cop/mixin/statement_modifier.rb index dff5564c312..b6de1e8b3ff 100644 --- a/lib/rubocop/cop/mixin/statement_modifier.rb +++ b/lib/rubocop/cop/mixin/statement_modifier.rb @@ -58,10 +58,11 @@ def max_line_length end def indentation_multiplier - return 1 if config.for_cop('Layout/IndentationStyle')['Enabled'] + indentation_style_config = config.for_cop('Layout/IndentationStyle') + return 1 if indentation_style_config['EnforcedStyle'] == 'spaces' default_configuration = RuboCop::ConfigLoader.default_configuration - config.for_cop('Layout/IndentationStyle')['IndentationWidth'] || + indentation_style_config['IndentationWidth'] || config.for_cop('Layout/IndentationWidth')['Width'] || default_configuration .for_cop('Layout/IndentationStyle')['IndentationWidth'] || diff --git a/spec/rubocop/cop/style/if_unless_modifier_spec.rb b/spec/rubocop/cop/style/if_unless_modifier_spec.rb index c3900846ee2..43facaa71d0 100644 --- a/spec/rubocop/cop/style/if_unless_modifier_spec.rb +++ b/spec/rubocop/cop/style/if_unless_modifier_spec.rb @@ -6,7 +6,11 @@ subject(:cop) { described_class.new(config) } let(:config) do - RuboCop::Config.new('Layout/LineLength' => line_length_config) + RuboCop::Config.new('Layout/LineLength' => line_length_config, + 'Layout/IndentationStyle' => { + 'Enabled' => true, + 'EnforcedStyle' => 'spaces' + }) end let(:line_length_config) do { @@ -517,7 +521,7 @@ def f end end - context 'with disabled Layout/IndentationStyle cop' do + context 'with tabs used for indentation' do shared_examples 'with tabs indentation' do let(:source) do # Empty lines should make no difference. @@ -567,7 +571,8 @@ def f 'Width' => 1 }, 'Layout/IndentationStyle' => { - 'Enabled' => false, + 'Enabled' => true, + 'EnforcedStyle' => 'tabs', 'IndentationWidth' => 2 }, 'Layout/LineLength' => { 'Max' => 10 + 12 } # 12 is indentation @@ -584,7 +589,8 @@ def f 'Width' => 1 }, 'Layout/IndentationStyle' => { - 'Enabled' => false + 'Enabled' => false, + 'EnforcedStyle' => 'tabs' }, 'Layout/LineLength' => { 'Max' => 10 + 6 } # 6 is indentation ) @@ -597,7 +603,8 @@ def f let(:config) do RuboCop::Config.new( 'Layout/IndentationStyle' => { - 'Enabled' => false + 'Enabled' => false, + 'EnforcedStyle' => 'tabs' }, 'Layout/LineLength' => { 'Max' => 10 + 12 } # 12 is indentation )