Skip to content

Commit

Permalink
fix: don't remove comments between key and value in object-shorthand (#…
Browse files Browse the repository at this point in the history
…18442)

* fix: don't collapse comments in object-shorthand

Fixes #18441

* add missing test

* refactor

* fix: recognize comments inside value node
  • Loading branch information
KubaJastrz committed May 16, 2024
1 parent 62e686c commit 5c28d9a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/rules/object-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,13 @@ module.exports = {
node,
messageId: "expectedPropertyShorthand",
fix(fixer) {

// x: /* */ x
// x: (/* */ x)
if (sourceCode.getCommentsInside(node).length > 0) {
return null;
}

return fixer.replaceText(node, node.value.name);
}
});
Expand All @@ -497,6 +504,13 @@ module.exports = {
node,
messageId: "expectedPropertyShorthand",
fix(fixer) {

// "x": /* */ x
// "x": (/* */ x)
if (sourceCode.getCommentsInside(node).length > 0) {
return null;
}

return fixer.replaceText(node, node.value.name);
}
});
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/object-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,36 @@ ruleTester.run("object-shorthand", rule, {
output: null,
errors: [METHOD_ERROR]
},
{
code: "var x = {a: /* comment */ a}",
output: null,
errors: [PROPERTY_ERROR]
},
{
code: "var x = {a /* comment */: a}",
output: null,
errors: [PROPERTY_ERROR]
},
{
code: "var x = {a: (a /* comment */)}",
output: null,
errors: [PROPERTY_ERROR]
},
{
code: "var x = {'a': /* comment */ a}",
output: null,
errors: [PROPERTY_ERROR]
},
{
code: "var x = {'a': (a /* comment */)}",
output: null,
errors: [PROPERTY_ERROR]
},
{
code: "var x = {'a' /* comment */: a}",
output: null,
errors: [PROPERTY_ERROR]
},
{
code: "var x = {y: function() {}}",
output: "var x = {y() {}}",
Expand Down

0 comments on commit 5c28d9a

Please sign in to comment.