Skip to content

Commit

Permalink
[Fix rubocop#7885] Consider Layout/IndentationStyle:EnforcedStyle
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonas054 committed May 1, 2020
1 parent 4f3cb38 commit ce88669
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/mixin/statement_modifier.rb
Expand Up @@ -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'] ||
Expand Down
17 changes: 12 additions & 5 deletions spec/rubocop/cop/style/if_unless_modifier_spec.rb
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -584,7 +589,8 @@ def f
'Width' => 1
},
'Layout/IndentationStyle' => {
'Enabled' => false
'Enabled' => false,
'EnforcedStyle' => 'tabs'
},
'Layout/LineLength' => { 'Max' => 10 + 6 } # 6 is indentation
)
Expand All @@ -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
)
Expand Down

0 comments on commit ce88669

Please sign in to comment.