From 3175316db26aebef4b19e269aca90c8ce3955363 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sat, 24 Oct 2020 01:52:59 +0200 Subject: [PATCH] Fix: prefer-destructuring invalid autofix with comma operator (#13761) --- lib/rules/prefer-destructuring.js | 20 ++++++++++- tests/lib/rules/prefer-destructuring.js | 45 +++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lib/rules/prefer-destructuring.js b/lib/rules/prefer-destructuring.js index 66e412fd3e3..b2d3c8a0b01 100644 --- a/lib/rules/prefer-destructuring.js +++ b/lib/rules/prefer-destructuring.js @@ -4,6 +4,18 @@ */ "use strict"; +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const astUtils = require("./utils/ast-utils"); + +//------------------------------------------------------------------------------ +// Helpers +//------------------------------------------------------------------------------ + +const PRECEDENCE_OF_ASSIGNMENT_EXPR = astUtils.getPrecedence({ type: "AssignmentExpression" }); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -185,9 +197,15 @@ module.exports = { return null; } + let objectText = sourceCode.getText(rightNode.object); + + if (astUtils.getPrecedence(rightNode.object) < PRECEDENCE_OF_ASSIGNMENT_EXPR) { + objectText = `(${objectText})`; + } + return fixer.replaceText( node, - `{${rightNode.property.name}} = ${sourceCode.getText(rightNode.object)}` + `{${rightNode.property.name}} = ${objectText}` ); } diff --git a/tests/lib/rules/prefer-destructuring.js b/tests/lib/rules/prefer-destructuring.js index 3b7c0183db8..8f111a1d8c2 100644 --- a/tests/lib/rules/prefer-destructuring.js +++ b/tests/lib/rules/prefer-destructuring.js @@ -188,6 +188,51 @@ ruleTester.run("prefer-destructuring", rule, { type: "VariableDeclarator" }] }, + { + code: "var foo = (a, b).foo;", + output: "var {foo} = (a, b);", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var length = (() => {}).length;", + output: "var {length} = () => {};", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = (a = b).foo;", + output: "var {foo} = a = b;", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = (a || b).foo;", + output: "var {foo} = a || b;", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = (f()).foo;", + output: "var {foo} = f();", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, { code: "var foo = object.bar.foo;", output: "var {foo} = object.bar;",