Skip to content

Commit

Permalink
[Fix rubocop#8185] Fix a false positive for Style/YodaCondition
Browse files Browse the repository at this point in the history
Fixes rubocop#8185.

This PR fixes a false positive for `Style/YodaCondition` when
interpolation is used on the left side.

String interpolation (and Regexp interpolation) contains variables,
I think it's acceptable to use it on the left hand side.
  • Loading branch information
koic committed Jun 22, 2020
1 parent 3ddca25 commit 1e59fe1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -23,6 +23,7 @@
* [#8098](https://github.com/rubocop-hq/rubocop/issues/8098): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using interpolations. ([@owst][])
* [#8150](https://github.com/rubocop-hq/rubocop/pull/8150): Fix a false positive for `Layout/EmptyLinesAroundAttributeAccessor` when using attribute accessors in `if` ... `else` branches. ([@koic][])
* [#8179](https://github.com/rubocop-hq/rubocop/issues/8179): Fix an infinite correction loop error for `Layout/MultilineBlockLayout` when missing newline before opening parenthesis `(` for block body. ([@koic][])
* [#8185](https://github.com/rubocop-hq/rubocop/issues/8185): Fix a false positive for `Style/YodaCondition` when interpolation is used on the left hand side. ([@koic][])

### Changes

Expand Down
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -10018,6 +10018,8 @@ foo == 99
foo == "bar"
foo <= 42
bar > 10
"#{interpolation}" == foo
/#{interpolation}/ == foo
----

==== EnforcedStyle: forbid_for_equality_operators_only
Expand Down
19 changes: 18 additions & 1 deletion lib/rubocop/cop/style/yoda_condition.rb
Expand Up @@ -19,6 +19,8 @@ module Style
# foo == "bar"
# foo <= 42
# bar > 10
# "#{interpolation}" == foo
# /#{interpolation}/ == foo
#
# @example EnforcedStyle: forbid_for_equality_operators_only
# # bad
Expand Down Expand Up @@ -109,7 +111,8 @@ def valid_yoda?(node)
rhs = node.first_argument

return true if lhs.literal? && rhs.literal? ||
!lhs.literal? && !rhs.literal?
!lhs.literal? && !rhs.literal? ||
interpolation?(lhs)

enforce_yoda? ? lhs.literal? : rhs.literal?
end
Expand Down Expand Up @@ -150,6 +153,20 @@ def source_file_path_constant?(node)
def program_name?(name)
PROGRAM_NAMES.include?(name)
end

def interpolation?(node)
return true if node.dstr_type?

# TODO: Use `RegexpNode#interpolation?` when the following is released.
# https://github.com/rubocop-hq/rubocop-ast/pull/18
if node.regexp_type?
return true if node.children.any? do |child|
child.respond_to?(:begin_type?) && child.begin_type?
end
end

false
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/rubocop/cop/style/yoda_condition_spec.rb
Expand Up @@ -44,6 +44,8 @@
it_behaves_like 'accepts', 'b = 1; b == 2'
it_behaves_like 'accepts', '$var == 5'
it_behaves_like 'accepts', 'foo == "bar"'
it_behaves_like 'accepts', '"#{interpolation}" == foo'
it_behaves_like 'accepts', '/#{interpolation}/ == foo'
it_behaves_like 'accepts', 'foo[0] > "bar" || baz != "baz"'
it_behaves_like 'accepts', 'node = last_node.parent'
it_behaves_like 'accepts', '(first_line - second_line) > 0'
Expand Down

0 comments on commit 1e59fe1

Please sign in to comment.