Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address linting offenses #3666

Merged
merged 142 commits into from
Jan 16, 2018
Merged

Address linting offenses #3666

merged 142 commits into from
Jan 16, 2018

Conversation

andrewgarner
Copy link
Contributor

@andrewgarner andrewgarner commented Jan 9, 2018

This addresses all 8,109 offences detected by Rubocop.

Count Cop
2353 Layout/SpaceInsideHashLiteralBraces
1044 Layout/SpaceAfterComma
584 Lint/AmbiguousRegexpLiteral
508 Naming/VariableNumber
403 Layout/SpaceAroundOperators
381 Lint/UselessAssignment
253 Style/SymbolArray
149 Layout/EmptyLinesAroundClassBody
148 Layout/SpaceInsideBlockBraces
140 Layout/AccessModifierIndentation
121 Style/BracesAroundHashParameters
97 Layout/MultilineMethodCallBraceLayout
94 Layout/ExtraSpacing
86 Lint/UnusedBlockArgument
84 Layout/EmptyLinesAroundAccessModifier
74 Style/MutableConstant
63 Style/WordArray
58 Layout/MultilineOperationIndentation
57 Lint/UnusedMethodArgument
53 Metrics/BlockLength
51 Layout/TrailingWhitespace
51 Style/SymbolProc
46 Style/HashSyntax
42 Lint/AssignmentInCondition
40 Bundler/OrderedGems
40 Layout/IndentationWidth
38 Style/DateTime
38 Style/EmptyMethod
33 Style/NumericPredicate
32 Naming/HeredocDelimiterNaming
30 Layout/SpaceBeforeComma
30 Layout/SpaceInLambdaLiteral
30 Lint/AmbiguousBlockAssociation
27 Layout/EmptyLineBetweenDefs
27 Layout/EmptyLinesAroundModuleBody
27 Lint/EndAlignment
26 Layout/ElseAlignment
26 Layout/FirstParameterIndentation
24 Style/StabbyLambdaParentheses
24 Style/StringLiteralsInInterpolation
22 Layout/EmptyLineAfterMagicComment
21 Style/StructInheritance
20 Style/MethodCalledOnDoEndBlock
20 Style/ParallelAssignment
19 Layout/AlignArray
18 Layout/MultilineHashBraceLayout
18 Layout/SpaceAroundBlockParameters
17 Layout/CaseIndentation
17 Performance/HashEachMethods
17 Performance/RedundantMerge
17 Performance/RegexpMatch
17 Style/ConditionalAssignment
16 Layout/EmptyLinesAroundBlockBody
16 Performance/StringReplacement
16 Style/IfInsideElse
15 Lint/IneffectiveAccessModifier
15 Style/FormatStringToken
13 Layout/TrailingBlankLines
13 Lint/UnneededSplatExpansion
13 Style/NumericLiteralPrefix
12 Layout/SpaceAroundEqualsInParameterDefault
12 Lint/DuplicateMethods
12 Style/BarePercentLiterals
12 Style/RedundantParentheses
12 Style/RedundantReturn
11 Lint/ShadowingOuterLocalVariable
11 Style/AndOr
11 Style/MethodCallWithoutArgsParentheses
11 Style/MultilineIfModifier
11 Style/UnneededInterpolation
10 Layout/SpaceInsideBrackets
9 Lint/DeprecatedClassMethods
9 Lint/RescueWithoutErrorClass
9 Lint/StringConversionInInterpolation
9 Style/UnneededPercentQ
8 Layout/IndentationConsistency
8 Lint/AmbiguousOperator
8 Lint/ParenthesesAsGroupedExpression
7 Layout/MultilineBlockLayout
7 Style/TernaryParentheses
6 Layout/BlockEndNewline
6 Layout/SpaceInsideParens
6 Lint/UselessAccessModifier
6 Performance/RedundantBlockCall
6 Style/ClassCheck
5 Layout/SpaceInsidePercentLiteralDelimiters
5 Lint/HandleExceptions
5 Performance/RedundantMatch
5 Style/MultilineBlockChain
5 Style/NestedParenthesizedCalls
5 Style/ParenthesesAroundCondition
4 Layout/MultilineArrayBraceLayout
4 Layout/SpaceBeforeBlockBraces
4 Security/YAMLLoad
4 Style/EmptyElse
4 Style/TrailingUnderscoreVariable
4 Style/TrivialAccessors
4 Style/ZeroLengthPredicate
3 Bundler/DuplicatedGem
3 Lint/BlockAlignment
3 Lint/DefEndAlignment
3 Lint/Void
3 Performance/Casecmp
3 Style/InverseMethods
3 Style/MethodMissing
3 Style/MultilineTernaryOperator
2 Layout/IndentHeredoc
2 Lint/ScriptPermission
2 Style/CommentedKeyword
2 Style/EmptyCaseCondition
2 Style/IdenticalConditionalBranches
1 Layout/EmptyLinesAroundExceptionHandlingKeywords
1 Layout/EmptyLinesAroundMethodBody
1 Layout/MultilineMethodDefinitionBraceLayout
1 Layout/SpaceAfterColon
1 Lint/DuplicateCaseCondition
1 Lint/LiteralInInterpolation
1 Lint/RescueException
1 Naming/HeredocDelimiterCase
1 Performance/FixedSize
1 Performance/FlatMap
1 Style/ClassVars
1 Style/MixinUsage
1 Style/MultilineMemoization
1 Style/RedundantConditional
1 Style/StderrPuts
1 Style/YodaCondition

Copy link
Contributor

@h-lame h-lame left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is amazing. Thanks for taking this on!

However, rubocop can sometimes do strange things when running the Layout and Style cops, particularly in respect to indentation. It seems that the order the cops are run can leave things in a strange state. For example the changes introduced by Fix Style/ConditionalAssignment offenses have left some strange indenting around if/else/end blocks. I think they'd be cleaned up by one of the Layout/Indent* cops, but they were run in a previous commit. We may need to run multiple passes of the linter over the code in order to get a clean report from it. We will also want to hand review the changes even when we have a clean build because we sometimes get strange results that pass (I've highlighted a few in separate comments).

@@ -25,7 +25,7 @@ def is_home_page_list_controller_for(list_name, opts)
redirect_to redirect_proc.call(home_page_list_container, home_page_list_item), notice: %{#{plural_name.titleize} on home page reordered successfully}
end

protected
protected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right.

@@ -93,11 +93,11 @@ def has_home_page_list_of(list_type)
plural_name = list_type.to_s
list_name = list_type.to_s
home_page_list_methods = Module.new do
protected
protected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indenting here seems unusual, perhaps the method definitions should also be indented as much as this protected statement, or the Module.new do should be on a new line?

@@ -21,7 +21,7 @@ def interpolate_address_property(property_name)

def replace_newlines_with_break_tags(string)
string.
gsub(/^\n/, ''). # get rid of blank lines
gsub(/^\n/, ''). # get rid of blank lines
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention here is that this comment should line up with the comments on the next two lines. It seems rubocop understood that about the next two, but somehow messed up this one. Probably best to re-indent this one so it lines up.

@@ -7,8 +7,8 @@

unless html_attachment.govspeak_content.present?
govspeak_content = html_attachment.build_govspeak_content(
body: html_attachment.attributes['body'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the changes in this commit would benefit from going over by hand to line things up more readibly. Rubocop seems to have given us:

something = some_method_call(
  argument_1,
              argument_2,
              argument_3
            )

when something like:

something = some_method_call(
  argument_1,
  argument_2,
  argument_3
)

or:

something = 
  some_method_call(
    argument_1,
    argument_2,
    argument_3
  )

would be more readable.

@@ -126,7 +126,7 @@ def edition_type_options_for_select(user, selected)
[edition_type.model_name.human.pluralize, edition_type.model_name.singular]
end
end
ActiveModel::Name
ActiveModel::Name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems unusual, we're just referencing an ActiveModel constant that should already be loaded as part of rails. I think it could be deleted without any problems (worth checking git blame to see if there are hints as to why it's here, but I suspect it's a copy-paste error.


vendored_cucumber_bin = Dir["#{Rails.root}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
vendored_cucumber_bin = Dir["#{Rails.root}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it feels dangerous to make any changes to this file, because it makes it harder to upgrade cucumber and easily see the diff between the new version from the gem and our version. I wonder if we could disable rubocop for this file? If we decide to lint it though, the indent added here should be applied to the whole file as it's all inside the unless statement on line 8.

create(:nation_inapplicability, nation_id: Nation.scotland.id, alternative_url: "http://scot.gov.uk")]
)
create(:nation_inapplicability, nation_id: Nation.scotland.id, alternative_url: "http://scot.gov.uk")
])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to break up and indent this ]) so the ] lines up with the nation_inapplicabilities: [ that opened it and the ) lines up with the create( that opened it (as they're on the same line we might want to break them up too).

consultation
publication
detailed_guide
speech].each do |document_with_policies|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be better expressed as:

  %i[
    news_article
    #...
    speech
  ].each do |document_with_policites|
    test "....." do
      # whatever
    end
  end

I think the single char indents have confused rubocop

test 'cannot modify historic editions' do
refute enforcer_for(managing_editor, historic_edition).can?(:modify)
test 'cannot modify historic editions' do
refute enforcer_for(managing_editor, historic_edition).can?(:modify)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to indent the contents of this test too.

tmp_spec.rb Outdated
@@ -0,0 +1,38 @@
require("rails_helper")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is introduced as the only change in a commit called Fix Performance/HashEachMethods offenses. Either the commit should be re-written to explain what this test is, or we shoudl drop the commit, and this test.

@andrewgarner
Copy link
Contributor Author

@h-lame thanks for the comments! I'm currently going through each of these commits one by one to sanity check the Rubocop autofixes (the commits for which I initially automated). You're right in spotting that some of the fixes have introduced additional style violations as a side effect so I'm addressing those at the same time.

@andrewgarner andrewgarner force-pushed the fix-govuk-lint-offenses branch 25 times, most recently from 6d6d79f to 255535c Compare January 12, 2018 11:45
@andrewgarner
Copy link
Contributor Author

The excluded files in the Robocop configuration file contain a
`method_missing` method without also defining
`respond_to_missing?`. It would be preferable to introduce the
`respond_to_missing?` in each of the excluded files, however due to
time constraints I was unable to do so in this commit.

The cop will apply if any further `method_missing` methods are
introduced however additional work will be needed to address the
excluded files.
@andrewgarner andrewgarner merged commit fc62edc into master Jan 16, 2018
@andrewgarner andrewgarner deleted the fix-govuk-lint-offenses branch January 16, 2018 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants