From f874c430c0c33afedee2a683227164377ac1fe6c Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 11:40:10 +0000 Subject: [PATCH 01/21] config/default: Stop disabling all cops by default - This is _incredibly_ confusing behaviour, it turns out: https://github.com/github/code-scanning/issues/7361, https://github.com/github/github/pull/241104. - People have been adding cops to `github/github` and they've not been running because with `DisabledByDefault`, nothing runs unless there's an explicit config stanza or it's already declared upstream in this gem. - Since `github/github`'s RuboCop config also had `DisabledByDefault` and we wanted to set `EnabledByDefault` there to alleviate the above confusion, we couldn't: "cops cannot be both enabled by default and disabled by default". - This flips `DisabledByDefault: true` to `EnabledByDefault: true`, so that RuboCop config is easier to understand and the behaviour is more in line with what normal users expect. - I understand that `DisabledByDefault` was set to avoid RuboCop versions changing which have new cops set up, or existing cop config changes, that cause linting violations everywhere, but it's way worse for understanding how things work, as the issues/PRs above reveal! If there's something that causes a lot of violations, we can disable it either here or in the downstream app codebase. This shouldn't be too much of an issue because bumping this gem version or a RuboCop version requires an explicit PR/approval/deploy workflow with CI, so we can catch the really noisy things. --- config/default.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/default.yml b/config/default.yml index 67af72a5..0807758b 100644 --- a/config/default.yml +++ b/config/default.yml @@ -3,7 +3,7 @@ require: - rubocop-performance AllCops: - DisabledByDefault: true + EnabledByDefault: true Bundler/DuplicatedGem: Enabled: true From 87eaa0cd2ae51bce272727519ed4b32583f50e30 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 12:08:46 +0000 Subject: [PATCH 02/21] config/default: Remove redundant `Enabled: true` for cops - Since `EnabledByDefault: true`, we can slim this down to only the ones we want to disable either because the styleguide doesn't like the rules, or because the styleguide has no opinion yet. - I added some more `Enabled: false` here for cops that are failing in the code in this gem and others, based on the information or lack of in our styleguide. --- config/default.yml | 291 ++++++--------------------------------------- 1 file changed, 39 insertions(+), 252 deletions(-) diff --git a/config/default.yml b/config/default.yml index 0807758b..ca0907bc 100644 --- a/config/default.yml +++ b/config/default.yml @@ -5,180 +5,40 @@ require: AllCops: EnabledByDefault: true -Bundler/DuplicatedGem: - Enabled: true - -Bundler/OrderedGems: - Enabled: true - -GitHub/InsecureHashAlgorithm: - Enabled: true - -Layout/BlockAlignment: - Enabled: true - -Layout/BlockEndNewline: - Enabled: true +Gemspec/DependencyVersion: + Enabled: false -Layout/ConditionPosition: - Enabled: true +# TODO: This is too new to be written in the styleguide. +Gemspec/RequireMFA: + Enabled: false -Layout/DefEndAlignment: - Enabled: true +Gemspec/RequiredRubyVersion: + Enabled: false Layout/EndAlignment: Enabled: false -Layout/EndOfLine: - Enabled: true - Layout/IndentationStyle: - Enabled: true EnforcedStyle: spaces IndentationWidth: 2 Layout/IndentationWidth: - Enabled: true Width: 2 -Layout/InitialIndentation: - Enabled: true - Layout/LineLength: Enabled: false -Layout/SpaceAfterColon: - Enabled: true - -Layout/SpaceAfterComma: - Enabled: true - -Layout/SpaceAfterMethodName: - Enabled: true - -Layout/SpaceAfterNot: - Enabled: true - -Layout/SpaceAfterSemicolon: - Enabled: true - -Layout/SpaceAroundBlockParameters: - Enabled: true - -Layout/SpaceAroundEqualsInParameterDefault: - Enabled: true - -Layout/SpaceBeforeBlockBraces: - Enabled: true +Layout/SingleLineBlockChain: + Enabled: false Layout/SpaceInsideArrayLiteralBrackets: - Enabled: true EnforcedStyle: no_space -Layout/SpaceInsideArrayPercentLiteral: - Enabled: true - -Layout/SpaceInsideBlockBraces: - Enabled: true - -Layout/SpaceInsideParens: - Enabled: true - -Layout/SpaceInsideRangeLiteral: - Enabled: true - -Layout/SpaceInsideReferenceBrackets: - Enabled: true - -Layout/TrailingEmptyLines: - Enabled: true - -Layout/TrailingWhitespace: - Enabled: true - -Lint/CircularArgumentReference: - Enabled: true - -Lint/Debugger: - Enabled: true - -Lint/DeprecatedClassMethods: - Enabled: true - -Lint/DuplicateMethods: - Enabled: true - -Lint/DuplicateHashKey: - Enabled: true - -Lint/EachWithObjectArgument: - Enabled: true - -Lint/ElseLayout: - Enabled: true - -Lint/EmptyEnsure: - Enabled: true - -Lint/EmptyInterpolation: - Enabled: true - -Lint/EnsureReturn: - Enabled: true - -Lint/FlipFlop: - Enabled: true - -Lint/FloatOutOfRange: - Enabled: true - -Lint/FormatParameterMismatch: - Enabled: true - -Lint/LiteralAsCondition: - Enabled: true - -Lint/LiteralInInterpolation: - Enabled: true - -Lint/Loop: - Enabled: true - -Lint/NextWithoutAccumulator: - Enabled: true - -Lint/RandOne: - Enabled: true - -Lint/RequireParentheses: - Enabled: true - -Lint/RescueException: - Enabled: true - -Lint/RedundantStringCoercion: - Enabled: true - -Lint/UnderscorePrefixedVariableName: - Enabled: true - -Lint/RedundantCopDisableDirective: - Enabled: true - -Lint/RedundantSplatExpansion: - Enabled: true - -Lint/UnreachableCode: - Enabled: true - -Lint/BinaryOperatorWithIdenticalOperands: - Enabled: true - -Lint/UselessSetterCall: - Enabled: true +Lint/ConstantResolution: + Enabled: false -Lint/Void: - Enabled: true +Lint/NumberConversion: + Enabled: false Metrics/AbcSize: Enabled: false @@ -207,128 +67,55 @@ Metrics/ParameterLists: Metrics/PerceivedComplexity: Enabled: false -Naming/AsciiIdentifiers: - Enabled: true - -Naming/ClassAndModuleCamelCase: - Enabled: true - -Naming/FileName: - Enabled: true - -Naming/MethodName: - Enabled: true - Performance/CaseWhenSplat: Enabled: false -Performance/Count: - Enabled: true - -Performance/Detect: - Enabled: true - -Performance/DoubleStartEndWith: - Enabled: true - -Performance/EndWith: - Enabled: true - -Performance/FlatMap: - Enabled: true - -Performance/RangeInclude: - Enabled: false - -Performance/RedundantMatch: +Performance/ChainArrayAllocation: Enabled: false Performance/RedundantMerge: - Enabled: true MaxKeyValuePairs: 1 -Performance/ReverseEach: - Enabled: true - -Performance/Size: - Enabled: true - -Performance/StartWith: - Enabled: true - -Security/Eval: - Enabled: true - -Style/ArrayJoin: - Enabled: true - -Style/BeginBlock: - Enabled: true - -Style/BlockComments: - Enabled: true - -Style/CaseEquality: - Enabled: true - -Style/CharacterLiteral: - Enabled: true +Style/CollectionMethods: + Enabled: false -Style/ClassMethods: - Enabled: true +Style/ConstantVisibility: + Enabled: false Style/Copyright: Enabled: false -Style/DefWithParentheses: - Enabled: true - -Style/EndBlock: - Enabled: true - -Style/For: - Enabled: true +# TODO: The styleguide doesn't have an opinion on this yet. +Style/Documentation: + Enabled: false -Style/FrozenStringLiteralComment: - Enabled: true +Style/DocumentationMethod: + Enabled: false Style/HashSyntax: - Enabled: true EnforcedStyle: ruby19_no_mixed_keys -Style/LambdaCall: - Enabled: true - -Style/MethodCallWithoutArgsParentheses: - Enabled: true - -Style/MethodDefParentheses: - Enabled: true - -Style/MultilineIfThen: - Enabled: true - -Style/NilComparison: - Enabled: true - -Style/Not: - Enabled: true +Style/InlineComment: + Enabled: false -Style/OneLineConditional: - Enabled: true +Style/MethodCallWithArgsParentheses: + Enabled: false -Style/RedundantSortBy: - Enabled: true +Style/MultilineBlockChain: + Enabled: false -Style/Sample: - Enabled: true +Style/MissingElse: + Enabled: false -Style/StabbyLambdaParentheses: - Enabled: true +Style/SoleNestedConditional: + Enabled: false -Style/Strip: - Enabled: true +Style/StringHashKeys: + Enabled: false Style/StringLiterals: - Enabled: true EnforcedStyle: double_quotes + +# TODO: The styleguide doesn't have an opinion on this yet. +Style/SymbolArray: + Enabled: false From 638d41878ca067ee3f885a23e6169e37b62fb861 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 12:31:03 +0000 Subject: [PATCH 03/21] Autocorrect RuboCop `Layout/EmptyLines` offenses --- test/test_rails_controller_render_literal.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_rails_controller_render_literal.rb b/test/test_rails_controller_render_literal.rb index 9da2f16c..bd52c7f1 100644 --- a/test/test_rails_controller_render_literal.rb +++ b/test/test_rails_controller_render_literal.rb @@ -442,7 +442,6 @@ def index assert_equal 1, offenses.count end - def test_render_literal_dynamic_local_key_offense offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base From 0ca62276a99f778fc4b589a4a0667322e909a5f1 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 12:31:41 +0000 Subject: [PATCH 04/21] Autocorrect RuboCop `Layout/SpaceInsideHashLiteralBraces` offenses --- test/test_insecure_hash_algorithm.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_insecure_hash_algorithm.rb b/test/test_insecure_hash_algorithm.rb index 597450b8..3e48aa90 100644 --- a/test/test_insecure_hash_algorithm.rb +++ b/test/test_insecure_hash_algorithm.rb @@ -10,7 +10,7 @@ def cop_class end def make_cop(allowed:) - config = RuboCop::Config.new({"GitHub/InsecureHashAlgorithm" => {"Allowed" => allowed}}) + config = RuboCop::Config.new({ "GitHub/InsecureHashAlgorithm" => { "Allowed" => allowed } }) cop_class.new(config) end From f1070080b8bc73cbfc74e7447f086b2ffc18e6c1 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 12:33:23 +0000 Subject: [PATCH 05/21] Autocorrect RuboCop `Style/StringConcatenation` offenses --- lib/rubocop/cop/github/rails_controller_render_paths_exist.rb | 2 +- lib/rubocop/cop/github/rails_view_render_paths_exist.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb b/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb index 126738a0..ee88d006 100644 --- a/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb +++ b/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb @@ -50,7 +50,7 @@ def on_send(node) def resolve_template(path) cop_config["ViewPath"].each do |view_path| - if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first + if m = Dir["#{File.join(config.path_relative_to_config(view_path), path)}*"].first return m end end diff --git a/lib/rubocop/cop/github/rails_view_render_paths_exist.rb b/lib/rubocop/cop/github/rails_view_render_paths_exist.rb index 49a40f2c..bf0fc5ae 100644 --- a/lib/rubocop/cop/github/rails_view_render_paths_exist.rb +++ b/lib/rubocop/cop/github/rails_view_render_paths_exist.rb @@ -47,7 +47,7 @@ def resolve_partial(path) path = parts.join(File::SEPARATOR) cop_config["ViewPath"].each do |view_path| - if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first + if m = Dir["#{File.join(config.path_relative_to_config(view_path), path)}*"].first return m end end From d7ddb5761643be84f81fa25c8bfa12d77f698cf5 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 12:36:57 +0000 Subject: [PATCH 06/21] Autocorrect RuboCop `Style/IfUnlessModifier` offenses - This works because the RuboCop rule for line length isn't enabled by default (it never was in this gem for some reason). Some of these lines are pretty long now! --- .../cop/github/accessibility/link_has_href.rb | 4 +--- .../github/accessibility/no_positive_tabindex.rb | 4 +--- .../accessibility/no_redundant_image_alt.rb | 8 ++------ .../cop/github/insecure_hash_algorithm.rb | 16 ++++------------ .../cop/github/rails_application_record.rb | 4 +--- .../github/rails_controller_render_literal.rb | 8 ++------ .../rails_controller_render_paths_exist.rb | 12 +++--------- lib/rubocop/cop/github/rails_render_inline.rb | 4 +--- .../cop/github/rails_render_object_collection.rb | 4 +--- .../cop/github/rails_view_render_literal.rb | 8 ++------ .../cop/github/rails_view_render_paths_exist.rb | 8 ++------ 11 files changed, 20 insertions(+), 60 deletions(-) diff --git a/lib/rubocop/cop/github/accessibility/link_has_href.rb b/lib/rubocop/cop/github/accessibility/link_has_href.rb index 751c564c..0d8ecc52 100644 --- a/lib/rubocop/cop/github/accessibility/link_has_href.rb +++ b/lib/rubocop/cop/github/accessibility/link_has_href.rb @@ -13,9 +13,7 @@ def on_send(node) receiver, method_name, *args = *node if receiver.nil? && method_name == :link_to - if args.length == 1 || (args.length > 1 && args[1].type == :str && args[1].children.first == "#") - add_offense(node.loc.selector) - end + add_offense(node.loc.selector) if args.length == 1 || (args.length > 1 && args[1].type == :str && args[1].children.first == "#") end end end diff --git a/lib/rubocop/cop/github/accessibility/no_positive_tabindex.rb b/lib/rubocop/cop/github/accessibility/no_positive_tabindex.rb index 2a63fbe1..748abcf1 100644 --- a/lib/rubocop/cop/github/accessibility/no_positive_tabindex.rb +++ b/lib/rubocop/cop/github/accessibility/no_positive_tabindex.rb @@ -18,9 +18,7 @@ def on_send(node) hash.each_pair do |key, value| next if key.type == :dsym next unless key.respond_to?(:value) - if key.value == :tabindex && value.source.to_i > 0 - add_offense(hash) - end + add_offense(hash) if key.value == :tabindex && value.source.to_i > 0 end end end diff --git a/lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb b/lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb index 0f84a872..80bd44d0 100644 --- a/lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb +++ b/lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb @@ -16,9 +16,7 @@ def on_send(node) receiver, method_name, _= *node if receiver.nil? && method_name == :image_tag - if redundant_alt?(node) - add_offense(node.loc.selector) - end + add_offense(node.loc.selector) if redundant_alt?(node) end end @@ -27,9 +25,7 @@ def on_send(node) def contains_redundant_alt_text?(string) return false if string.empty? - if (string.downcase.split & REDUNDANT_ALT_WORDS).any? - return true - end + return true if (string.downcase.split & REDUNDANT_ALT_WORDS).any? end end end diff --git a/lib/rubocop/cop/github/insecure_hash_algorithm.rb b/lib/rubocop/cop/github/insecure_hash_algorithm.rb index de1b321d..dcbc6664 100644 --- a/lib/rubocop/cop/github/insecure_hash_algorithm.rb +++ b/lib/rubocop/cop/github/insecure_hash_algorithm.rb @@ -108,25 +108,17 @@ def alg_name(val) end def on_const(const_node) - if insecure_const?(const_node) && !digest_uuid?(const_node) - add_offense(const_node, message: MSG) - end + add_offense(const_node, message: MSG) if insecure_const?(const_node) && !digest_uuid?(const_node) end def on_send(send_node) case when uuid_v3?(send_node) - unless allowed_hash_functions.include?("md5") - add_offense(send_node, message: UUID_V3_MSG) - end + add_offense(send_node, message: UUID_V3_MSG) unless allowed_hash_functions.include?("md5") when uuid_v5?(send_node) - unless allowed_hash_functions.include?("sha1") - add_offense(send_node, message: UUID_V5_MSG) - end + add_offense(send_node, message: UUID_V5_MSG) unless allowed_hash_functions.include?("sha1") when openssl_hmac_new?(send_node) - if openssl_hmac_new_insecure?(send_node) - add_offense(send_node, message: MSG) - end + add_offense(send_node, message: MSG) if openssl_hmac_new_insecure?(send_node) when insecure_digest?(send_node) add_offense(send_node, message: MSG) when insecure_hash_lookup?(send_node) diff --git a/lib/rubocop/cop/github/rails_application_record.rb b/lib/rubocop/cop/github/rails_application_record.rb index 1ec30cf9..4bfbbbac 100644 --- a/lib/rubocop/cop/github/rails_application_record.rb +++ b/lib/rubocop/cop/github/rails_application_record.rb @@ -19,9 +19,7 @@ class RailsApplicationRecord < Base def on_class(node) klass, superclass, _ = *node - if active_record_base_const?(superclass) && !(application_record_const?(klass)) - add_offense(superclass) - end + add_offense(superclass) if active_record_base_const?(superclass) && !(application_record_const?(klass)) end end end diff --git a/lib/rubocop/cop/github/rails_controller_render_literal.rb b/lib/rubocop/cop/github/rails_controller_render_literal.rb index 0f2efd39..8847e53d 100644 --- a/lib/rubocop/cop/github/rails_controller_render_literal.rb +++ b/lib/rubocop/cop/github/rails_controller_render_literal.rb @@ -60,9 +60,7 @@ def on_send(node) elsif option_pairs = render_with_options?(node) option_pairs = option_pairs.reject { |pair| options_key?(pair) } - if option_pairs.any? { |pair| ignore_key?(pair) } - return - end + return if option_pairs.any? { |pair| ignore_key?(pair) } if template_node = option_pairs.map { |pair| template_key?(pair) }.compact.first if !literal?(template_node) @@ -98,9 +96,7 @@ def on_send(node) if option_pairs locals = option_pairs.map { |pair| locals_key?(pair) }.compact.first - if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals)) - add_offense(node) - end + add_offense(node) if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals)) end end end diff --git a/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb b/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb index ee88d006..3f84ce2f 100644 --- a/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb +++ b/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb @@ -27,22 +27,16 @@ def on_send(node) if args = render_str?(node) node, path = args - unless resolve_template(path.to_s) - add_offense(node, message: "Template could not be found") - end + add_offense(node, message: "Template could not be found") unless resolve_template(path.to_s) elsif pairs = render_options?(node) if pair = pairs.detect { |p| render_key?(p) } key, node, path = render_key?(pair) case key when :action, :template - unless resolve_template(path.to_s) - add_offense(node, message: "Template could not be found") - end + add_offense(node, message: "Template could not be found") unless resolve_template(path.to_s) when :partial - unless resolve_partial(path.to_s) - add_offense(node, message: "Partial template could not be found") - end + add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s) end end end diff --git a/lib/rubocop/cop/github/rails_render_inline.rb b/lib/rubocop/cop/github/rails_render_inline.rb index 8f76fb2f..65c8fb5e 100644 --- a/lib/rubocop/cop/github/rails_render_inline.rb +++ b/lib/rubocop/cop/github/rails_render_inline.rb @@ -18,9 +18,7 @@ class RailsRenderInline < Base def on_send(node) if option_pairs = render_with_options?(node) - if option_pairs.detect { |pair| inline_key?(pair) } - add_offense(node) - end + add_offense(node) if option_pairs.detect { |pair| inline_key?(pair) } end end end diff --git a/lib/rubocop/cop/github/rails_render_object_collection.rb b/lib/rubocop/cop/github/rails_render_object_collection.rb index 38a27fa5..b5a13b53 100644 --- a/lib/rubocop/cop/github/rails_render_object_collection.rb +++ b/lib/rubocop/cop/github/rails_render_object_collection.rb @@ -31,9 +31,7 @@ def on_send(node) case object_sym when :object - if partial_name.children[0].is_a?(String) - suggestion = ", instead `render partial: #{partial_name.source}, locals: { #{File.basename(partial_name.children[0], '.html.erb')}: #{object_node.source} }`" - end + suggestion = ", instead `render partial: #{partial_name.source}, locals: { #{File.basename(partial_name.children[0], '.html.erb')}: #{object_node.source} }`" if partial_name.children[0].is_a?(String) add_offense(node, message: "Avoid `render object:`#{suggestion}") when :collection, :spacer_template add_offense(node, message: "Avoid `render collection:`") diff --git a/lib/rubocop/cop/github/rails_view_render_literal.rb b/lib/rubocop/cop/github/rails_view_render_literal.rb index c8563868..6f443e52 100644 --- a/lib/rubocop/cop/github/rails_view_render_literal.rb +++ b/lib/rubocop/cop/github/rails_view_render_literal.rb @@ -34,9 +34,7 @@ def on_send(node) if render_literal?(node) elsif option_pairs = render_with_options?(node) - if option_pairs.any? { |pair| ignore_key?(pair) } - return - end + return if option_pairs.any? { |pair| ignore_key?(pair) } if partial_node = option_pairs.map { |pair| partial_key?(pair) }.compact.first if !literal?(partial_node) @@ -60,9 +58,7 @@ def on_send(node) if locals if locals.hash_type? - if !hash_with_literal_keys?(locals) - add_offense(node) - end + add_offense(node) if !hash_with_literal_keys?(locals) else add_offense(node) end diff --git a/lib/rubocop/cop/github/rails_view_render_paths_exist.rb b/lib/rubocop/cop/github/rails_view_render_paths_exist.rb index bf0fc5ae..64234167 100644 --- a/lib/rubocop/cop/github/rails_view_render_paths_exist.rb +++ b/lib/rubocop/cop/github/rails_view_render_paths_exist.rb @@ -27,16 +27,12 @@ def on_send(node) if args = render_str?(node) node, path = args - unless resolve_partial(path.to_s) - add_offense(node, message: "Partial template could not be found") - end + add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s) elsif pairs = render_options?(node) if pair = pairs.detect { |p| partial_key?(p) } node, path = partial_key?(pair) - unless resolve_partial(path.to_s) - add_offense(node, message: "Partial template could not be found") - end + add_offense(node, message: "Partial template could not be found") unless resolve_partial(path.to_s) end end end From cc0a4b8cbfd0f4e19c547949c4092bde2181c665 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 12:42:39 +0000 Subject: [PATCH 07/21] config/default: Disable some of the default rules - Either the styleguide has no opinion on them (yet), or it doesn't like them, or they'd be quite disruptive. --- config/default.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/config/default.yml b/config/default.yml index ca0907bc..f9224c43 100644 --- a/config/default.yml +++ b/config/default.yml @@ -15,6 +15,9 @@ Gemspec/RequireMFA: Gemspec/RequiredRubyVersion: Enabled: false +Layout/EmptyLineAfterMagicComment: + Enabled: false + Layout/EndAlignment: Enabled: false @@ -25,9 +28,15 @@ Layout/IndentationStyle: Layout/IndentationWidth: Width: 2 +# TODO: Enable this with line length of 118 (the GitHub UI's text wrapping length) +# since it's specified in https://github.com/github/rubocop-github/blob/master/STYLEGUIDE.md#maximum-line-length. Layout/LineLength: Enabled: false +# TODO: The styleguide has no opinions on this yet. +Layout/MultilineMethodCallIndentation: + Enabled: false + Layout/SingleLineBlockChain: Enabled: false @@ -37,6 +46,9 @@ Layout/SpaceInsideArrayLiteralBrackets: Lint/ConstantResolution: Enabled: false +Lint/EmptyConditionalBody: + Enabled: false + Lint/NumberConversion: Enabled: false @@ -92,6 +104,9 @@ Style/Documentation: Style/DocumentationMethod: Enabled: false +Style/GuardClause: + Enabled: false + Style/HashSyntax: EnforcedStyle: ruby19_no_mixed_keys @@ -107,6 +122,12 @@ Style/MultilineBlockChain: Style/MissingElse: Enabled: false +Style/NegatedIf: + Enabled: false + +Style/NumericPredicate: + Enabled: false + Style/SoleNestedConditional: Enabled: false From 8d8c5781e657c3c2007f8b9f89a750ec1bb50d49 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 12:58:08 +0000 Subject: [PATCH 08/21] Configure and autocorrect RuboCop `Style/PercentLiteralDelimiters` --- config/default.yml | 4 ++++ lib/rubocop/cop/github/insecure_hash_algorithm.rb | 4 ++-- test/test_insecure_hash_algorithm.rb | 8 ++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/config/default.yml b/config/default.yml index f9224c43..7091b25c 100644 --- a/config/default.yml +++ b/config/default.yml @@ -128,6 +128,10 @@ Style/NegatedIf: Style/NumericPredicate: Enabled: false +Style/PercentLiteralDelimiters: + PreferredDelimiters: + '%w': '()' + Style/SoleNestedConditional: Enabled: false diff --git a/lib/rubocop/cop/github/insecure_hash_algorithm.rb b/lib/rubocop/cop/github/insecure_hash_algorithm.rb index dcbc6664..6fcf0d74 100644 --- a/lib/rubocop/cop/github/insecure_hash_algorithm.rb +++ b/lib/rubocop/cop/github/insecure_hash_algorithm.rb @@ -86,11 +86,11 @@ def just_encoding?(val) # Built-in hash functions are listed in these docs: # https://ruby-doc.org/stdlib-2.7.0/libdoc/digest/rdoc/Digest.html # https://ruby-doc.org/stdlib-2.7.0/libdoc/openssl/rdoc/OpenSSL/Digest.html - DEFAULT_ALLOWED = %w[ + DEFAULT_ALLOWED = %w( SHA256 SHA384 SHA512 - ].freeze + ).freeze def allowed_hash_functions @allowed_algorithms ||= cop_config.fetch("Allowed", DEFAULT_ALLOWED).map(&:downcase) diff --git a/test/test_insecure_hash_algorithm.rb b/test/test_insecure_hash_algorithm.rb index 3e48aa90..104137e0 100644 --- a/test/test_insecure_hash_algorithm.rb +++ b/test/test_insecure_hash_algorithm.rb @@ -371,7 +371,7 @@ def uuid end def test_uuid_v3_with_md5_allowed - cop = make_cop(allowed: %w[MD5]) + cop = make_cop(allowed: %w(MD5)) offenses = investigate(cop, <<-RUBY) class Something def uuid @@ -409,7 +409,7 @@ def uuid end def test_uuid_v5_with_sha1_allowed - cop = make_cop(allowed: %w[SHA1]) + cop = make_cop(allowed: %w(SHA1)) offenses = investigate(cop, <<-RUBY) class Something def uuid @@ -422,7 +422,7 @@ def uuid end def test_allow_sha512_only - cop = make_cop(allowed: %w[SHA512]) + cop = make_cop(allowed: %w(SHA512)) offenses = investigate(cop, <<-RUBY) class Something HASH = Digest::SHA256 @@ -432,7 +432,7 @@ class Something end def test_allow_lots_of_hashes - cop = make_cop(allowed: %w[SHA1 SHA256 SHA384 SHA512]) + cop = make_cop(allowed: %w(SHA1 SHA256 SHA384 SHA512)) offenses = investigate(cop, <<-RUBY) class Something HASH = Digest::SHA1 From 5241eb0711b12bc5b1343807ede10dcf7b42fd49 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:10:27 +0000 Subject: [PATCH 09/21] Autocorrect RuboCop `Style/RedundantParentheses` offenses --- lib/rubocop/cop/github/rails_application_record.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rubocop/cop/github/rails_application_record.rb b/lib/rubocop/cop/github/rails_application_record.rb index 4bfbbbac..ca45460d 100644 --- a/lib/rubocop/cop/github/rails_application_record.rb +++ b/lib/rubocop/cop/github/rails_application_record.rb @@ -19,7 +19,7 @@ class RailsApplicationRecord < Base def on_class(node) klass, superclass, _ = *node - add_offense(superclass) if active_record_base_const?(superclass) && !(application_record_const?(klass)) + add_offense(superclass) if active_record_base_const?(superclass) && !application_record_const?(klass) end end end From 89ac5268a249ed7520f839a3025251b23c70e062 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:16:31 +0000 Subject: [PATCH 10/21] config/default: Disable more rules - These either seemed arbitrary or had no accompanying styleguide rule that we've written yet. --- config/default.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/config/default.yml b/config/default.yml index 7091b25c..8a45a3b4 100644 --- a/config/default.yml +++ b/config/default.yml @@ -15,6 +15,10 @@ Gemspec/RequireMFA: Gemspec/RequiredRubyVersion: Enabled: false +# TOOD: This seems arbitrary, but we don't have a styleguide opinion on it? +Layout/ClassStructure: + Enabled: false + Layout/EmptyLineAfterMagicComment: Enabled: false @@ -128,6 +132,9 @@ Style/NegatedIf: Style/NumericPredicate: Enabled: false +Style/Next: + Enabled: false + Style/PercentLiteralDelimiters: PreferredDelimiters: '%w': '()' @@ -144,3 +151,7 @@ Style/StringLiterals: # TODO: The styleguide doesn't have an opinion on this yet. Style/SymbolArray: Enabled: false + +# Disabled, there's a lot of `foo, _ = method_with_two_return_values` with a space in it. +Style/TrailingUnderscoreVariable: + Enabled: false From 51aa7fb6d6f21a44633119a8db92d7b5801c2885 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:22:28 +0000 Subject: [PATCH 11/21] Autocorrect RuboCop \`Style/SafeNavigation\` offenses --- lib/rubocop/cop/github/rails_controller_render_literal.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rubocop/cop/github/rails_controller_render_literal.rb b/lib/rubocop/cop/github/rails_controller_render_literal.rb index 8847e53d..83056be2 100644 --- a/lib/rubocop/cop/github/rails_controller_render_literal.rb +++ b/lib/rubocop/cop/github/rails_controller_render_literal.rb @@ -89,7 +89,7 @@ def on_send(node) add_offense(node) return end - option_pairs = option_hash && option_hash.pairs + option_pairs = option_hash&.pairs else option_pairs = node.arguments[0].pairs end From 401c927df8c01abfff32069a89075c5a993acc03 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:27:29 +0000 Subject: [PATCH 12/21] Autocorrect RuboCop `Layout/SpaceAroundOperators` offenses --- lib/rubocop/cop/github/accessibility/image_has_alt.rb | 2 +- lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/cop/github/accessibility/image_has_alt.rb b/lib/rubocop/cop/github/accessibility/image_has_alt.rb index 698d9224..dbfd5e00 100644 --- a/lib/rubocop/cop/github/accessibility/image_has_alt.rb +++ b/lib/rubocop/cop/github/accessibility/image_has_alt.rb @@ -12,7 +12,7 @@ class ImageHasAlt < Base def_node_search :has_alt_attribute?, "(sym :alt)" def on_send(node) - receiver, method_name, _= *node + receiver, method_name, _ = *node if receiver.nil? && method_name == :image_tag alt = has_alt_attribute?(node) diff --git a/lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb b/lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb index 80bd44d0..6ec5921e 100644 --- a/lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb +++ b/lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb @@ -13,7 +13,7 @@ class NoRedundantImageAlt < Base def_node_search :redundant_alt?, "(pair (sym :alt) (str #contains_redundant_alt_text?))" def on_send(node) - receiver, method_name, _= *node + receiver, method_name, _ = *node if receiver.nil? && method_name == :image_tag add_offense(node.loc.selector) if redundant_alt?(node) From 5ba9ba87cd6b779c155d531a572ca8ef2536c52b Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:27:53 +0000 Subject: [PATCH 13/21] config/default: Disable more rules that the styleguide doesn't opine on --- config/default.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config/default.yml b/config/default.yml index 8a45a3b4..c1b88392 100644 --- a/config/default.yml +++ b/config/default.yml @@ -50,12 +50,18 @@ Layout/SpaceInsideArrayLiteralBrackets: Lint/ConstantResolution: Enabled: false +Lint/DuplicateBranch: + Enabled: false + Lint/EmptyConditionalBody: Enabled: false Lint/NumberConversion: Enabled: false +Lint/UnusedBlockArgument: + Enabled: false + Metrics/AbcSize: Enabled: false From 127c051e3b88a598ab21ef9c321487afceb4f1ce Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:29:28 +0000 Subject: [PATCH 14/21] Autocorrect RuboCop `Layout/EmptyLineAfterGuardClause` offenses --- lib/rubocop/cop/github/accessibility/no_positive_tabindex.rb | 1 + lib/rubocop/cop/github/insecure_hash_algorithm.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/rubocop/cop/github/accessibility/no_positive_tabindex.rb b/lib/rubocop/cop/github/accessibility/no_positive_tabindex.rb index 748abcf1..31510db7 100644 --- a/lib/rubocop/cop/github/accessibility/no_positive_tabindex.rb +++ b/lib/rubocop/cop/github/accessibility/no_positive_tabindex.rb @@ -18,6 +18,7 @@ def on_send(node) hash.each_pair do |key, value| next if key.type == :dsym next unless key.respond_to?(:value) + add_offense(hash) if key.value == :tabindex && value.source.to_i > 0 end end diff --git a/lib/rubocop/cop/github/insecure_hash_algorithm.rb b/lib/rubocop/cop/github/insecure_hash_algorithm.rb index 6fcf0d74..dff93c67 100644 --- a/lib/rubocop/cop/github/insecure_hash_algorithm.rb +++ b/lib/rubocop/cop/github/insecure_hash_algorithm.rb @@ -64,6 +64,7 @@ class InsecureHashAlgorithm < Base def insecure_algorithm?(val) return false if val == :Digest # Don't match "Digest::Digest". + case alg_name(val) when *allowed_hash_functions false @@ -99,6 +100,7 @@ def allowed_hash_functions def alg_name(val) return :nil if val.nil? return val.to_s.downcase unless val.is_a?(RuboCop::AST::Node) + case val.type when :sym, :str val.children.first.to_s.downcase From 30fe0a5f46aaf2309f67ad7c4fea1b420288a694 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:31:24 +0000 Subject: [PATCH 15/21] Autocorrect RuboCop `Layout/RedundantLineBreak` offenses --- lib/rubocop/cop/github/insecure_hash_algorithm.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/rubocop/cop/github/insecure_hash_algorithm.rb b/lib/rubocop/cop/github/insecure_hash_algorithm.rb index dff93c67..2f2bfb65 100644 --- a/lib/rubocop/cop/github/insecure_hash_algorithm.rb +++ b/lib/rubocop/cop/github/insecure_hash_algorithm.rb @@ -87,11 +87,7 @@ def just_encoding?(val) # Built-in hash functions are listed in these docs: # https://ruby-doc.org/stdlib-2.7.0/libdoc/digest/rdoc/Digest.html # https://ruby-doc.org/stdlib-2.7.0/libdoc/openssl/rdoc/OpenSSL/Digest.html - DEFAULT_ALLOWED = %w( - SHA256 - SHA384 - SHA512 - ).freeze + DEFAULT_ALLOWED = %w(SHA256 SHA384 SHA512).freeze def allowed_hash_functions @allowed_algorithms ||= cop_config.fetch("Allowed", DEFAULT_ALLOWED).map(&:downcase) From be167bbb495b93be73326865db87d77a0598e66c Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:32:45 +0000 Subject: [PATCH 16/21] Fix RuboCop `Naming/MemoizedInstanceVariableName` offenses --- lib/rubocop/cop/github/insecure_hash_algorithm.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rubocop/cop/github/insecure_hash_algorithm.rb b/lib/rubocop/cop/github/insecure_hash_algorithm.rb index 2f2bfb65..175fe6cb 100644 --- a/lib/rubocop/cop/github/insecure_hash_algorithm.rb +++ b/lib/rubocop/cop/github/insecure_hash_algorithm.rb @@ -90,7 +90,7 @@ def just_encoding?(val) DEFAULT_ALLOWED = %w(SHA256 SHA384 SHA512).freeze def allowed_hash_functions - @allowed_algorithms ||= cop_config.fetch("Allowed", DEFAULT_ALLOWED).map(&:downcase) + @allowed_hash_functions ||= cop_config.fetch("Allowed", DEFAULT_ALLOWED).map(&:downcase) end def alg_name(val) From 9b62169a77a5e6917b8392d687df9e98b81f9718 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:34:24 +0000 Subject: [PATCH 17/21] Autocorrect RuboCop `Style/MultipleComparison` offenses --- config/default.yml | 3 +++ lib/rubocop/cop/github/insecure_hash_algorithm.rb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/config/default.yml b/config/default.yml index c1b88392..a2d379fa 100644 --- a/config/default.yml +++ b/config/default.yml @@ -114,6 +114,9 @@ Style/Documentation: Style/DocumentationMethod: Enabled: false +Style/EmptyCaseCondition: + Enabled: false + Style/GuardClause: Enabled: false diff --git a/lib/rubocop/cop/github/insecure_hash_algorithm.rb b/lib/rubocop/cop/github/insecure_hash_algorithm.rb index 175fe6cb..7fc7a212 100644 --- a/lib/rubocop/cop/github/insecure_hash_algorithm.rb +++ b/lib/rubocop/cop/github/insecure_hash_algorithm.rb @@ -81,7 +81,7 @@ def not_just_encoding?(val) end def just_encoding?(val) - val == :hexencode || val == :bubblebabble + [:hexencode, :bubblebabble].include?(val) end # Built-in hash functions are listed in these docs: From 26726103d292e858c77e36939e6fc46f161bb675 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 10 Oct 2022 13:37:51 +0000 Subject: [PATCH 18/21] Autocorrect RuboCop `Style/{RedundantFreeze,MutableConstants}` offenses --- lib/rubocop/cop/github/accessibility/link_has_href.rb | 2 +- lib/rubocop/cop/github/accessibility/no_redundant_image_alt.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/cop/github/accessibility/link_has_href.rb b/lib/rubocop/cop/github/accessibility/link_has_href.rb index 0d8ecc52..dc687801 100644 --- a/lib/rubocop/cop/github/accessibility/link_has_href.rb +++ b/lib/rubocop/cop/github/accessibility/link_has_href.rb @@ -7,7 +7,7 @@ module Cop module GitHub module Accessibility class LinkHasHref < Base - MSG = "Links should go somewhere, you probably want to use a `