diff --git a/config/default.yml b/config/default.yml index 67af72a5..ac650352 100644 --- a/config/default.yml +++ b/config/default.yml @@ -2,183 +2,66 @@ require: - rubocop/cop/github - rubocop-performance -AllCops: - DisabledByDefault: true - -Bundler/DuplicatedGem: - Enabled: true - -Bundler/OrderedGems: - Enabled: true - -GitHub/InsecureHashAlgorithm: - Enabled: true +Gemspec/DependencyVersion: + Enabled: false -Layout/BlockAlignment: - Enabled: true +# TODO: This is too new to be written in the styleguide. +Gemspec/RequireMFA: + Enabled: false -Layout/BlockEndNewline: - Enabled: true +Gemspec/RequiredRubyVersion: + Enabled: false -Layout/ConditionPosition: - Enabled: true +# TOOD: This seems arbitrary, but we don't have a styleguide opinion on it? +Layout/ClassStructure: + Enabled: false -Layout/DefEndAlignment: - Enabled: true +Layout/EmptyLineAfterMagicComment: + 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 - +# 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 -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 +# TODO: The styleguide has no opinions on this yet. +Layout/MultilineMethodCallIndentation: + Enabled: false -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 +# TODO: Absolutely we should enable this one, but it's very noisy right now. +Lint/AssignmentInCondition: + Enabled: false -Lint/RedundantSplatExpansion: - Enabled: true +Lint/ConstantResolution: + Enabled: false -Lint/UnreachableCode: - Enabled: true +Lint/DuplicateBranch: + Enabled: false -Lint/BinaryOperatorWithIdenticalOperands: - Enabled: true +Lint/EmptyConditionalBody: + Enabled: false -Lint/UselessSetterCall: - Enabled: true +Lint/NumberConversion: + Enabled: false -Lint/Void: - Enabled: true +Lint/UnusedBlockArgument: + Enabled: false Metrics/AbcSize: Enabled: false @@ -207,128 +90,78 @@ 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 +# TODO: The styleguide doesn't have an opinion on this yet. +Style/Documentation: + Enabled: false -Style/EndBlock: - Enabled: true +Style/DocumentationMethod: + Enabled: false -Style/For: - Enabled: true +Style/EmptyCaseCondition: + Enabled: false -Style/FrozenStringLiteralComment: - Enabled: true +Style/GuardClause: + Enabled: false Style/HashSyntax: - Enabled: true EnforcedStyle: ruby19_no_mixed_keys -Style/LambdaCall: - Enabled: true - -Style/MethodCallWithoutArgsParentheses: - Enabled: true +Style/InlineComment: + Enabled: false -Style/MethodDefParentheses: - Enabled: true +Style/MethodCallWithArgsParentheses: + Enabled: false -Style/MultilineIfThen: - Enabled: true +Style/MultilineBlockChain: + Enabled: false -Style/NilComparison: - Enabled: true +Style/MissingElse: + Enabled: false -Style/Not: - Enabled: true +Style/NegatedIf: + Enabled: false -Style/OneLineConditional: - Enabled: true +Style/NumericPredicate: + Enabled: false -Style/RedundantSortBy: - Enabled: true +Style/Next: + Enabled: false -Style/Sample: - Enabled: true +Style/PercentLiteralDelimiters: + PreferredDelimiters: + '%w': '()' -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 + +# Disabled, there's a lot of `foo, _ = method_with_two_return_values` with a space in it. +Style/TrailingUnderscoreVariable: + Enabled: false diff --git a/lib/rubocop/cop/github/insecure_hash_algorithm.rb b/lib/rubocop/cop/github/insecure_hash_algorithm.rb index de1b321d..7fc7a212 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 @@ -80,25 +81,22 @@ 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: # 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) + @allowed_hash_functions ||= cop_config.fetch("Allowed", DEFAULT_ALLOWED).map(&:downcase) end 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 @@ -108,25 +106,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..ca45460d 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..83056be2 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) @@ -91,16 +89,14 @@ 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 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 126738a0..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 @@ -50,7 +44,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_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 49a40f2c..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 @@ -47,7 +43,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 diff --git a/test/test_insecure_hash_algorithm.rb b/test/test_insecure_hash_algorithm.rb index 597450b8..104137e0 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 @@ -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 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
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: