# TODO: Try getting more rules in sync.

require: ./Homebrew/rubocops.rb

inherit_mode:
  merge:
    - Include
    - Exclude

AllCops:
  TargetRubyVersion: 2.6
  DisplayCopNames: false
  # enable all pending rubocops
  NewCops: enable
  Include:
    - '**/*.rbi'
  Exclude:
    - 'Homebrew/sorbet/rbi/gems/**/*.rbi'
    - 'Homebrew/sorbet/rbi/hidden-definitions/**/*.rbi'
    - 'Homebrew/sorbet/rbi/todo.rbi'
    - 'Homebrew/bin/*'
    - 'Homebrew/vendor/**/*'

Cask/Desc:
  Description: 'Ensure that the desc stanza conforms to various content and style checks.'
  Enabled: true

Cask/HomepageMatchesUrl:
  Description: 'Ensure that the homepage and url match, otherwise add a comment. More info at https://github.com/Homebrew/homebrew-cask/blob/HEAD/doc/cask_language_reference/stanzas/url.md#when-url-and-homepage-hostnames-differ-add-a-comment'
  Enabled: true

Cask/HomepageUrlTrailingSlash:
  Description: 'Ensure that the homepage url has a slash after the domain name.'
  Enabled: true

Cask/NoDslVersion:
  Description: 'Do not use the deprecated DSL version syntax in your cask header.'
  Enabled: true

Cask/StanzaGrouping:
  Description: 'Ensure that cask stanzas are grouped correctly. More info at https://github.com/Homebrew/homebrew-cask/blob/HEAD/doc/cask_language_reference/readme.md#stanza-order'
  Enabled: true

Cask/StanzaOrder:
  Description: 'Ensure that cask stanzas are sorted correctly. More info at https://github.com/Homebrew/homebrew-cask/blob/HEAD/doc/cask_language_reference/readme.md#stanza-order'
  Enabled: true

# enable all formulae audits
FormulaAudit:
  Enabled: true

# enable all formulae strict audits
FormulaAuditStrict:
  Enabled: true

# Use `<<~` for heredocs.
Layout/HeredocIndentation:
  Enabled: true

# Keyword arguments don't have the same readability
# problems as normal parameters.
Metrics/ParameterLists:
  CountKeywordArgs: false

# Implicitly allow EOS as we use it everywhere.
Naming/HeredocDelimiterNaming:
  ForbiddenDelimiters:
    - END, EOD, EOF

Naming/MethodName:
  IgnoredPatterns:
    - '\AHEAD\?\Z'

# Allow dashes in filenames.
Naming/FileName:
  Regex: !ruby/regexp /^[\w\@\-\+\.]+(\.rb)?$/

# Both styles are used depending on context,
# e.g. `sha256` and `something_countable_1`.
Naming/VariableNumber:
  Enabled: false

# Avoid leaking resources.
Style/AutoResourceCleanup:
  Enabled: true

# This makes these a little more obvious.
Style/BarePercentLiterals:
  EnforcedStyle: percent_q

# Use consistent style for better readability.
Style/CollectionMethods:
  Enabled: true

# Prefer tokens with type annotations for consistency
# between formatting numbers and strings.
Style/FormatStringToken:
  EnforcedStyle: annotated

# autocorrectable and more readable
Style/HashEachMethods:
  Enabled: true
Style/HashTransformKeys:
  Enabled: true
Style/HashTransformValues:
  Enabled: true

# Allow for license expressions
Style/HashAsLastArrayItem:
  Exclude:
    - 'Taps/*/*/{Formula/,}*.rb'

# Enabled now LineLength is lowish.
Style/IfUnlessModifier:
  Enabled: true

# Only use this for numbers >= `1_000_000`.
Style/NumericLiterals:
  MinDigits: 7
  Strict: true

# Zero-prefixed octal literals are widely used and understood.
Style/NumericLiteralPrefix:
  EnforcedOctalStyle: zero_only

# Rescuing `StandardError` is an understood default.
Style/RescueStandardError:
  EnforcedStyle: implicit

# Returning `nil` is unnecessary.
Style/ReturnNil:
  Enabled: true

# We have no use for using `warn` because we
# are calling Ruby with warnings disabled.
Style/StderrPuts:
  Enabled: false

# Use consistent method names.
Style/StringMethods:
  Enabled: true

# An array of symbols is more readable than a symbol array
# and also allows for easier grepping.
Style/SymbolArray:
  EnforcedStyle: brackets

# Trailing commas make diffs nicer.
Style/TrailingCommaInArguments:
  EnforcedStyleForMultiline: comma
Style/TrailingCommaInArrayLiteral:
  EnforcedStyleForMultiline: comma
Style/TrailingCommaInHashLiteral:
  EnforcedStyleForMultiline: comma

# Does not hinder readability, so might as well enable it.
Performance/CaseWhenSplat:
  Enabled: true

# Makes code less readable for minor performance increases.
Performance/Caller:
  Enabled: false

# Don't allow cops to be disabled in casks and formulae.
Style/DisableCopsWithinSourceCodeDirective:
  Enabled: true
  Include:
    - 'Taps/*/*/{Formula/,Casks/,}*.rb'

# make our hashes consistent
Layout/HashAlignment:
  EnforcedHashRocketStyle: table
  EnforcedColonStyle: table

# `system` is a special case and aligns on second argument, so allow this for formulae.
Layout/ArgumentAlignment:
  Exclude:
    - 'Taps/*/*/{Formula/,}*.rb'

# this is a bit less "floaty"
Layout/CaseIndentation:
  EnforcedStyle: end

# this is a bit less "floaty"
Layout/EndAlignment:
  EnforcedStyleAlignWith: start_of_line

# conflicts with DSL-style path concatenation with `/`
Layout/SpaceAroundOperators:
  Enabled: false

# layout is not configurable (https://github.com/rubocop-hq/rubocop/issues/6254).
Layout/RescueEnsureAlignment:
  Enabled: false

# favour parens-less DSL-style arguments
Lint/AmbiguousBlockAssociation:
  Enabled: false

# so many of these in formulae and can't be autocorrected
# TODO: fix these as `ruby -w` complains about them.
Lint/AmbiguousRegexpLiteral:
  Exclude:
    - 'Taps/*/*/{Formula/,}*.rb'

# useful for metaprogramming in RSpec
Lint/ConstantDefinitionInBlock:
  Exclude:
    - '**/*_spec.rb'

# so many of these in formulae and can't be autocorrected
Lint/ParenthesesAsGroupedExpression:
  Exclude:
    - 'Taps/*/*/{Formula/,}*.rb'

# Most metrics don't make sense to apply for casks/formulae/taps.
Metrics/AbcSize:
  Exclude:
    - 'Taps/**/*'
Metrics/BlockLength:
  Exclude:
    - 'Taps/**/*'
Metrics/ClassLength:
  Exclude:
    - 'Taps/**/*'
Metrics/CyclomaticComplexity:
  Exclude:
    - 'Taps/**/*'
Metrics/MethodLength:
  Exclude:
    - 'Taps/**/*'
Metrics/ModuleLength:
  Exclude:
    - 'Taps/**/*'
Metrics/PerceivedComplexity:
  Exclude:
    - 'Taps/**/*'

# allow those that are standard
# TODO: try to remove some of these
Naming/MethodParameterName:
  AllowedNames:
    - '_'
    - 'a'
    - 'b'
    - 'cc'
    - 'c1'
    - 'c2'
    - 'd'
    - 'e'
    - 'f'
    - 'ff'
    - 'fn'
    - 'id'
    - 'io'
    - 'o'
    - 'p'
    - 'pr'
    - 'r'
    - 'rb'
    - 's'
    - 'to'
    - 'v'

# GitHub diff UI wraps beyond 118 characters
Layout/LineLength:
  Max: 118
  # ignore manpage comments and long single-line strings
  IgnoredPatterns: ['#: ', ' url "', ' mirror "', ' plist_options ',
                    ' appcast "', ' executable: "', ' font "', ' homepage "', ' name "',
                    ' pkg "', ' pkgutil: "', '#{language}', '#{version.',
                    ' "/Library/Application Support/', '"/Library/Caches/', '"/Library/PreferencePanes/',
                    ' "~/Library/Application Support/', '"~/Library/Caches/', '"~/Application Support',
                    ' was verified as official when first introduced to the cask']

Sorbet/FalseSigil:
  Exclude:
    - 'Taps/**/*.rb'
    - 'Homebrew/test/**/Casks/**/*.rb'

Sorbet/StrictSigil:
  Enabled: true
  Include:
    - '**/*.rbi'

# Try getting rid of these.
Sorbet/ConstantsFromStrings:
  Enabled: false

# Avoid false positives on modifiers used on symbols of methods
# See https://github.com/rubocop-hq/rubocop/issues/5953
Style/AccessModifierDeclarations:
  Enabled: false

# don't group nicely documented or private attr_readers
Style/AccessorGrouping:
  Exclude:
    - 'Homebrew/formula.rb'
    - 'Homebrew/formulary.rb'
    - 'Homebrew/migrator.rb'
    - 'Homebrew/resource.rb'
    - 'Homebrew/system_command.rb'
    - 'Homebrew/tap.rb'

# make rspec formatting more flexible
Style/BlockDelimiters:
  Exclude:
    - 'Homebrew/**/*_spec.rb'
    - 'Homebrew/**/shared_examples/**/*.rb'

# TODO: remove this when possible.
Style/ClassVars:
  Exclude:
  - '**/developer/bin/*'

# Don't enforce documentation in casks or formulae.
Style/Documentation:
  Exclude:
    - 'Taps/**/*'
    - '**/*.rbi'

Style/DocumentationMethod:
  Include:
    - 'Homebrew/formula.rb'

# Not used for casks and formulae.
Style/FrozenStringLiteralComment:
  EnforcedStyle: always
  Exclude:
    - 'Taps/*/*/{Formula,Casks,}/*.rb'
    - 'Homebrew/test/**/Casks/**/*.rb'
    - '**/*.rbi'

# TODO: remove this when possible.
Style/GlobalVars:
  Exclude:
  - '**/developer/bin/*'

# potential for errors in formulae too high with this
Style/GuardClause:
  Exclude:
    - 'Taps/*/*/{Formula/,Casks/,}*.rb'

# avoid hash rockets where possible
Style/HashSyntax:
  EnforcedStyle: ruby19

# so many of these in formulae and can't be autocorrected
Style/StringConcatenation:
  Exclude:
    - 'Taps/*/*/{Formula/,Casks/,}*.rb'

# ruby style guide favorite
Style/StringLiterals:
  EnforcedStyle: double_quotes

# consistency with above
Style/StringLiteralsInInterpolation:
  EnforcedStyle: double_quotes

# make things a bit easier to read
Style/TernaryParentheses:
  EnforcedStyle: require_parentheses_when_complex

# a bit confusing to non-Rubyists but useful for longer arrays
Style/WordArray:
  MinSize: 4

# would rather freeze too much than too little
Style/MutableConstant:
  EnforcedStyle: strict

# unused keyword arguments improve APIs
Lint/UnusedMethodArgument:
  AllowUnusedKeywordArguments: true
