From bf2bc4ccb43baf3f6e2f7af651708dd8f468286c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B4natas=20Davi=20Paganini?= Date: Thu, 13 Dec 2018 16:14:00 -0200 Subject: [PATCH] Skip offense when defining blank method --- CHANGELOG.md | 1 + lib/rubocop/cop/rails/blank.rb | 10 ++++++++++ manual/cops_rails.md | 5 +++++ spec/rubocop/cop/rails/blank_spec.rb | 4 ++++ 4 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 993809e4e34..1405c12cd15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [#4229](https://github.com/rubocop-hq/rubocop/issues/4229): Fix unexpected Style/HashSyntax consistency offence. ([@timon][]) * [#6500](https://github.com/rubocop-hq/rubocop/issues/6500): Add offense to use `in_time_zone` instead of deprecated `to_time_in_current_zone`. ([@nadiyaka][]) +* [#6577](https://github.com/rubocop-hq/rubocop/pull/6577): Prevent Rails/Blank cop from adding offense when define the blank method. ([@jonatas][]) * [#6554](https://github.com/rubocop-hq/rubocop/issues/6554): Prevent Layout/RescueEnsureAlignment cop from breaking on block assignment when assignment is on a separate line. ([@timmcanty][]) * [#6343](https://github.com/rubocop-hq/rubocop/pull/6343): Optimise `--auto-gen-config` when `Metrics/LineLength` cop is disabled. ([@tom-lord][]) * [#6389](https://github.com/rubocop-hq/rubocop/pull/6389): Fix false negative for `Style/TrailingCommaInHashLitera`/`Style/TrailingCommaInArrayLiteral` when there is a comment in the last line. ([@bayandin][]) diff --git a/lib/rubocop/cop/rails/blank.rb b/lib/rubocop/cop/rails/blank.rb index 7f1f2f6536a..28c6c4f6d3f 100644 --- a/lib/rubocop/cop/rails/blank.rb +++ b/lib/rubocop/cop/rails/blank.rb @@ -43,6 +43,11 @@ module Rails # if foo.blank? # something # end + # + # # good + # def blank? + # !present? + # end class Blank < Cop MSG_NIL_OR_EMPTY = 'Use `%s` instead of `%s`.'.freeze MSG_NOT_PRESENT = 'Use `%s` instead of `%s`.'.freeze @@ -70,6 +75,8 @@ class Blank < Cop def_node_matcher :not_present?, '(send (send $_ :present?) :!)' + def_node_matcher :defining_blank?, '(def :blank? (args) ...)' + def_node_matcher :unless_present?, <<-PATTERN (:if $(send $_ :present?) {nil? (...)} ...) PATTERN @@ -78,6 +85,9 @@ def on_send(node) return unless cop_config['NotPresent'] not_present?(node) do |receiver| + # accepts !present? if its in the body of a `blank?` method + next if defining_blank?(node.parent) + add_offense(node, message: format(MSG_NOT_PRESENT, prefer: replacement(receiver), diff --git a/manual/cops_rails.md b/manual/cops_rails.md index e6b9c642b8f..39bddfd390b 100644 --- a/manual/cops_rails.md +++ b/manual/cops_rails.md @@ -216,6 +216,11 @@ end if foo.blank? something end + +# good +def blank? + !present? +end ``` ### Configurable attributes diff --git a/spec/rubocop/cop/rails/blank_spec.rb b/spec/rubocop/cop/rails/blank_spec.rb index 16a1477fab3..ae98e7780d5 100644 --- a/spec/rubocop/cop/rails/blank_spec.rb +++ b/spec/rubocop/cop/rails/blank_spec.rb @@ -129,6 +129,10 @@ it_behaves_like 'offense', '!present?', 'blank?', 'Use `blank?` instead of `!present?`.' + + it 'accepts !present? if its in the body of a `blank?` method' do + expect_no_offenses('def blank?; !present? end') + end end context 'UnlessPresent set to true' do