diff --git a/lib/rules/prefer-destructuring.js b/lib/rules/prefer-destructuring.js index 1a51956dde5..d3314dc7e0d 100644 --- a/lib/rules/prefer-destructuring.js +++ b/lib/rules/prefer-destructuring.js @@ -178,6 +178,11 @@ module.exports = { const rightNode = node.init; const sourceCode = context.getSourceCode(); + // Don't fix if that would remove any comments. Only comments inside `rightNode.object` can be preserved. + if (sourceCode.getCommentsInside(node).length > sourceCode.getCommentsInside(rightNode.object).length) { + return null; + } + return fixer.replaceText( node, `{${rightNode.property.name}} = ${sourceCode.getText(rightNode.object)}` diff --git a/tests/lib/rules/prefer-destructuring.js b/tests/lib/rules/prefer-destructuring.js index 517158efb88..7795ed85b75 100644 --- a/tests/lib/rules/prefer-destructuring.js +++ b/tests/lib/rules/prefer-destructuring.js @@ -353,6 +353,224 @@ ruleTester.run("prefer-destructuring", rule, { data: { type: "object" }, type: "VariableDeclarator" }] + }, + + // comments + { + code: "var /* comment */ foo = object.foo;", + output: "var /* comment */ {foo} = object;", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var a, /* comment */foo = object.foo;", + output: "var a, /* comment */{foo} = object;", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo /* comment */ = object.foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var a, foo /* comment */ = object.foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo /* comment */ = object.foo, a;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo // comment\n = object.foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = /* comment */ object.foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = // comment\n object.foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = (/* comment */ object).foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = (object /* comment */).foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = bar(/* comment */).foo;", + output: "var {foo} = bar(/* comment */);", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = bar/* comment */.baz.foo;", + output: "var {foo} = bar/* comment */.baz;", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = bar[// comment\nbaz].foo;", + output: "var {foo} = bar[// comment\nbaz];", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo // comment\n = bar(/* comment */).foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = bar/* comment */.baz/* comment */.foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = object// comment\n.foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = object./* comment */foo;", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = (/* comment */ object.foo);", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = (object.foo /* comment */);", + output: null, + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = object.foo/* comment */;", + output: "var {foo} = object/* comment */;", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = object.foo// comment", + output: "var {foo} = object// comment", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = object.foo/* comment */, a;", + output: "var {foo} = object/* comment */, a;", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = object.foo// comment\n, a;", + output: "var {foo} = object// comment\n, a;", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] + }, + { + code: "var foo = object.foo, /* comment */ a;", + output: "var {foo} = object, /* comment */ a;", + errors: [{ + messageId: "preferDestructuring", + data: { type: "object" }, + type: "VariableDeclarator" + }] } ] });