From bfff77ad4eaa02e2e62481c986634df38d5db6e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E6=98=8A=E5=AE=87?= Date: Sat, 5 Jan 2019 04:32:49 +0800 Subject: [PATCH] Fix: no-param-reassign parameter in ternary operator (fixes #11236) (#11239) --- lib/rules/no-param-reassign.js | 8 ++++++++ tests/lib/rules/no-param-reassign.js | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/lib/rules/no-param-reassign.js b/lib/rules/no-param-reassign.js index 243bb6412cf..349345c148a 100644 --- a/lib/rules/no-param-reassign.js +++ b/lib/rules/no-param-reassign.js @@ -107,6 +107,14 @@ module.exports = { break; + // EXCLUDES: e.g. (foo ? a : b).c = bar; + case "ConditionalExpression": + if (parent.test === node) { + return false; + } + + break; + // no default } diff --git a/tests/lib/rules/no-param-reassign.js b/tests/lib/rules/no-param-reassign.js index 5d009c338f3..40156f481ec 100644 --- a/tests/lib/rules/no-param-reassign.js +++ b/tests/lib/rules/no-param-reassign.js @@ -32,6 +32,8 @@ ruleTester.run("no-param-reassign", rule, { { code: "function foo(a) { bar(a.b).c = 0; }", options: [{ props: true }] }, { code: "function foo(a) { data[a.b] = 0; }", options: [{ props: true }] }, { code: "function foo(a) { +a.b; }", options: [{ props: true }] }, + { code: "function foo(a) { (a ? [] : [])[0] = 1; }", options: [{ props: true }] }, + { code: "function foo(a) { (a.b ? [] : [])[0] = 1; }", options: [{ props: true }] }, { code: "function foo(a) { a.b = 0; }", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] }, { code: "function foo(a) { ++a.b; }", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] }, { code: "function foo(a) { delete a.b; }", options: [{ props: true, ignorePropertyModificationsFor: ["a"] }] }, @@ -87,6 +89,11 @@ ruleTester.run("no-param-reassign", rule, { options: [{ props: true }], errors: [{ message: "Assignment to property of function parameter 'bar'." }] }, + { + code: "function foo(bar) { (bar ? bar : [])[0] = 1; }", + options: [{ props: true }], + errors: [{ message: "Assignment to property of function parameter 'bar'." }] + }, { code: "function foo(bar) { [bar.a] = []; }", options: [{ props: true }],