From 19ab6666e8e4142a183bdee2be96e5bafbac0e21 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sun, 18 Aug 2019 07:52:59 +0200 Subject: [PATCH] Fix: yoda exceptRange false positives on empty string property names (#12071) --- lib/rules/yoda.js | 2 +- tests/lib/rules/yoda.js | 68 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/rules/yoda.js b/lib/rules/yoda.js index 43783c193bd..89c4a8afd11 100644 --- a/lib/rules/yoda.js +++ b/lib/rules/yoda.js @@ -119,7 +119,7 @@ function same(a, b) { const nameA = astUtils.getStaticPropertyName(a); // x.y = x["y"] - if (nameA) { + if (nameA !== null) { return ( same(a.object, b.object) && nameA === astUtils.getStaticPropertyName(b) diff --git a/tests/lib/rules/yoda.js b/tests/lib/rules/yoda.js index 89979ff58b0..d2c18347740 100644 --- a/tests/lib/rules/yoda.js +++ b/tests/lib/rules/yoda.js @@ -50,6 +50,14 @@ ruleTester.run("yoda", rule, { }, { code: "if ('blue' < x.y && x.y < 'green') {}", options: ["never", { exceptRange: true }] + }, { + code: "if (0 < x[``] && x[``] < 100) {}", + options: ["never", { exceptRange: true }], + parserOptions: { ecmaVersion: 2015 } + }, { + code: "if (0 < x[''] && x[``] < 100) {}", + options: ["never", { exceptRange: true }], + parserOptions: { ecmaVersion: 2015 } }, { code: "if (0 <= x['y'] && x['y'] <= 100) {}", options: ["never", { exceptRange: true }] @@ -332,6 +340,66 @@ ruleTester.run("yoda", rule, { } ] }, + { + code: "if (0 <= a[''] && a.b < 1) {}", + output: "if (a[''] >= 0 && a.b < 1) {}", + options: ["never", { exceptRange: true }], + errors: [ + { + messageId: "expected", + data: { expectedSide: "right", operator: "<=" }, + type: "BinaryExpression" + } + ] + }, + { + code: "if (0 <= a[''] && a[' '] < 1) {}", + output: "if (a[''] >= 0 && a[' '] < 1) {}", + options: ["never", { exceptRange: true }], + errors: [ + { + messageId: "expected", + data: { expectedSide: "right", operator: "<=" }, + type: "BinaryExpression" + } + ] + }, + { + code: "if (0 <= a[''] && a[null] < 1) {}", + output: "if (a[''] >= 0 && a[null] < 1) {}", + options: ["never", { exceptRange: true }], + errors: [ + { + messageId: "expected", + data: { expectedSide: "right", operator: "<=" }, + type: "BinaryExpression" + } + ] + }, + { + code: "if (0 <= a[''] && a[b] < 1) {}", + output: "if (a[''] >= 0 && a[b] < 1) {}", + options: ["never", { exceptRange: true }], + errors: [ + { + messageId: "expected", + data: { expectedSide: "right", operator: "<=" }, + type: "BinaryExpression" + } + ] + }, + { + code: "if (0 <= a[''] && a[b()] < 1) {}", + output: "if (a[''] >= 0 && a[b()] < 1) {}", + options: ["never", { exceptRange: true }], + errors: [ + { + messageId: "expected", + data: { expectedSide: "right", operator: "<=" }, + type: "BinaryExpression" + } + ] + }, { code: "if (0 <= a[b()] && a[b()] < 1) {}", output: "if (a[b()] >= 0 && a[b()] < 1) {}",