From 1e59fe1fd0d57314e9003ce3f7c75b05167fb594 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 22 Jun 2020 12:55:03 +0900 Subject: [PATCH] [Fix #8185] Fix a false positive for `Style/YodaCondition` Fixes #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. --- CHANGELOG.md | 1 + docs/modules/ROOT/pages/cops_style.adoc | 2 ++ lib/rubocop/cop/style/yoda_condition.rb | 19 ++++++++++++++++++- spec/rubocop/cop/style/yoda_condition_spec.rb | 2 ++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7d5e5beec6..e3f95a95703 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index b7b912312e9..19b2a2fc606 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -10018,6 +10018,8 @@ foo == 99 foo == "bar" foo <= 42 bar > 10 +"#{interpolation}" == foo +/#{interpolation}/ == foo ---- ==== EnforcedStyle: forbid_for_equality_operators_only diff --git a/lib/rubocop/cop/style/yoda_condition.rb b/lib/rubocop/cop/style/yoda_condition.rb index 00640b05a01..cbd4b0f0cb1 100644 --- a/lib/rubocop/cop/style/yoda_condition.rb +++ b/lib/rubocop/cop/style/yoda_condition.rb @@ -19,6 +19,8 @@ module Style # foo == "bar" # foo <= 42 # bar > 10 + # "#{interpolation}" == foo + # /#{interpolation}/ == foo # # @example EnforcedStyle: forbid_for_equality_operators_only # # bad @@ -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 @@ -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 diff --git a/spec/rubocop/cop/style/yoda_condition_spec.rb b/spec/rubocop/cop/style/yoda_condition_spec.rb index c41a9dbbe2f..60f276b0294 100644 --- a/spec/rubocop/cop/style/yoda_condition_spec.rb +++ b/spec/rubocop/cop/style/yoda_condition_spec.rb @@ -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'