Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: prefer-destructuring removes comments (refs #13678) #13682

Merged
merged 1 commit into from Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}]
}
]
});