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: don't remove comments between key and value in object-shorthand #18442

Merged
merged 4 commits into from May 16, 2024

Conversation

KubaJastrz
Copy link
Contributor

Fixes #18441

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Check for comments between the last key token and the value in the property node.

Is there anything you'd like reviewers to focus on?

@KubaJastrz KubaJastrz requested a review from a team as a code owner May 11, 2024 19:33
@eslint-github-bot eslint-github-bot bot added the bug ESLint is working incorrectly label May 11, 2024
@github-actions github-actions bot added the rule Relates to ESLint's core rules label May 11, 2024
Copy link

netlify bot commented May 11, 2024

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit 58fe097
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/66462f0d22ffbe000822cf3b

Comment on lines 500 to 506
const lastKeyToken = sourceCode.getLastToken(node.key);

// x: /* */ x
if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) {
return null;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about if we give a fix here through appending the comment before or after the property something like:

var x = {
a /* comment */: a,
b
}

can be fixed to

var x = {
/* comment */ a,
b
}

or

var x = {
a, /* comment */ 
b
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in that case it becomes a feature request, not a bug fix. You can create an issue for this idea.

Whatever the decision is, I'd recommend that both identifiers and function expressions were handled identically (which they aren't right now, hence this PR).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine not to provide the fix when there's a comment between the two identifiers and let the author fix this manually as it isn't clear where the new position for the comment should be.

@Tanujkanti4441 Tanujkanti4441 added the evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion label May 12, 2024
@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion autofix This change is related to ESLint's autofixing capabilities and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels May 12, 2024
@mdjermanovic mdjermanovic changed the title fix: don't collapse comments in object-shorthand fix: don't remove comments between key and value in object-shorthand May 12, 2024
tests/lib/rules/object-shorthand.js Show resolved Hide resolved
Comment on lines 500 to 505
const lastKeyToken = sourceCode.getLastToken(node.key);

// x: /* */ x
if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) {
return null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const lastKeyToken = sourceCode.getLastToken(node.key);
// x: /* */ x
if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) {
return null;
}
// x: /* */ x
if (sourceCode.commentsExistBetween(node.key, node.value)) {
return null;
}

We can use node.key directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 520 to 525
const lastKeyToken = sourceCode.getLastToken(node.key);

// "x": /* */ x
if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) {
return null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const lastKeyToken = sourceCode.getLastToken(node.key);
// "x": /* */ x
if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) {
return null;
}
// "x": /* */ x
if (sourceCode.commentsExistBetween(node.key, node.value)) {
return null;
}

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aladdin-add
aladdin-add previously approved these changes May 13, 2024
Copy link
Member

@aladdin-add aladdin-add left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed an edge case that wasn't covered (see the new test cases).

tests/lib/rules/object-shorthand.js Show resolved Hide resolved
tests/lib/rules/object-shorthand.js Show resolved Hide resolved
@@ -497,6 +497,12 @@ module.exports = {
node,
messageId: "expectedPropertyShorthand",
fix(fixer) {

// x: /* */ x
if (sourceCode.commentsExistBetween(node.key, node.value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (sourceCode.commentsExistBetween(node.key, node.value)) {
if (sourceCode.getCommentsInside(node).length > 0) {

Since we're replacing the entire node with just an identifier name, seems best to check if there are comments anywhere inside the node.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -510,6 +516,12 @@ module.exports = {
node,
messageId: "expectedPropertyShorthand",
fix(fixer) {

// "x": /* */ x
if (sourceCode.commentsExistBetween(node.key, node.value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (sourceCode.commentsExistBetween(node.key, node.value)) {
if (sourceCode.getCommentsInside(node).length > 0) {

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@mdjermanovic mdjermanovic merged commit 5c28d9a into eslint:main May 16, 2024
19 checks passed
@KubaJastrz KubaJastrz deleted the issue18441 branch May 16, 2024 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion autofix This change is related to ESLint's autofixing capabilities bug ESLint is working incorrectly contributor pool rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: object-shorthand auto-fix removes comments
4 participants