简体   繁体   中英

disable Rubocop complaint about method

I have a method that goes like this return if amount == 0 and rubocop is complaining that it should be like return if amount.zero? . How can I have it skip over that method? Here is my .rubocop.yml:

rubocop

StringLiterals:
  EnforcedStyle: double_quotes
  Enabled: true

Style/FrozenStringLiteralComment:
  Enabled: false

Style/TrailingCommaInLiteral:
  Enabled: false

Style/TrailingCommaInArguments:
  Enabled: false

Style/CaseEquality:
  Enabled: false

Documentation:
  Description: "Document classes and non-namespace modules."
  Enabled: false

Metrics/MethodLength:
  CountComments: false
  Max: 15

Rails/Delegate:
  Enabled: false

AllCops:
  Exclude:
    - db/**/*
    - config/**/*
    - script/**/*
    - vendor/**/*
    - bin/**/*
    - Rakefile
    - spec/spec_helper.rb
    - spec/rails_helper.rb
    - spec/teaspoon_env.rb

  TargetRubyVersion: 2.3

Rails:
  Enabled: true

submit_ticket_payment.rb

def call
  return if amount == 0
  Stripe::Charge.create(
    amount: amount,
    currency: "usd",
    description: event_name,
    receipt_email: context.rsvp.email,
    source: context.token,
    statement_descriptor: event_name
  )
rescue Stripe::StripeError
  context.fail!
end

So basically how can I have it not care about that particular instance?

The check in question is implemented by the Style/NumericPredicate cop.

If you generally want it to be enabled but not complain about an individual occurence, you can temporarily disable it with a special comment:

# rubocop:disable Style/NumericPredicate
return if amount == 0
# rubocop:enable Style/NumericPredicate

Note that it needs to be enabled again; otherwise it will skip the check for the whole remainder of the file.

If you wish to skip rule only for this particular method then:

# rubocop:disable Style/NumericPredicate
def call
  return if amount == 0
  # ...
end
# rubocop:enable Style/NumericPredicate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM