Skip to content

Commit

Permalink
Skip offense when defining blank method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jônatas Davi Paganini authored and bbatsov committed Dec 23, 2018
1 parent 0d65c6d commit bf2bc4c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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][])
Expand Down
10 changes: 10 additions & 0 deletions lib/rubocop/cop/rails/blank.rb
Expand Up @@ -43,6 +43,11 @@ module Rails
# if foo.blank?
# something
# end
#
# # good
# def blank?
# !present?
# end
class Blank < Cop
MSG_NIL_OR_EMPTY = 'Use `%<prefer>s` instead of `%<current>s`.'.freeze
MSG_NOT_PRESENT = 'Use `%<prefer>s` instead of `%<current>s`.'.freeze
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions manual/cops_rails.md
Expand Up @@ -216,6 +216,11 @@ end
if foo.blank?
something
end

# good
def blank?
!present?
end
```

### Configurable attributes
Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/cop/rails/blank_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit bf2bc4c

Please sign in to comment.