Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: prefer-destructuring removes comments (refs #13678) (#13682)
  • Loading branch information
mdjermanovic committed Sep 12, 2020
1 parent b4da0a7 commit 29d1cdc
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rules/prefer-destructuring.js
Expand Up @@ -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)}`
Expand Down
218 changes: 218 additions & 0 deletions tests/lib/rules/prefer-destructuring.js
Expand Up @@ -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"
}]
}
]
});

0 comments on commit 29d1cdc

Please sign in to comment.