Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #8185] Fix a false positive for Style/YodaCondition #8186

Merged
merged 1 commit into from Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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