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(json_formatter): handle comments in empty arrays/objects #1323

Merged
merged 1 commit into from
Dec 24, 2023

Conversation

faultyserver
Copy link
Contributor

Summary

Fixes #1322. Comments within empty object and array values are now placed as dangling comments, which those nodes then handle formatting for directly to place them within the braces for the value. For example:

{
    "object-block": { /* here's a block comment */},
    "object-line": { // here's a line comment
    },
    "object-ownline": {
        /* here's a block comment */
        // and a line comment
    }
}

// Now becomes

{
	"object-block": {
		/* here's a block comment */
	},
	"object-line": {
		// here's a line comment
	},
	"object-ownline": {
		/* here's a block comment */
		// and a line comment
	}
}

Test Plan

Added a snapshot test to cover various permutations of comments in empties.

@github-actions github-actions bot added A-Formatter Area: formatter L-JSON Language: JSON and super languages labels Dec 24, 2023
Copy link

netlify bot commented Dec 24, 2023

Deploy Preview for biomejs canceled.

Name Link
🔨 Latest commit 2b7e7b2
🔍 Latest deploy log https://app.netlify.com/sites/biomejs/deploys/6587c929e8746d0008b56b37

elements.format(),
format_dangling_comments(node.syntax())
]))
.should_expand(should_expand),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should_expand wasn't here before, but since the only way Dangling comments get added is explicitly through the handler that checks for empty blocks, I believe it's safe to add and won't change any other existing formatting.

@ematipico ematipico merged commit 5f9610b into main Dec 24, 2023
@ematipico ematipico deleted the faulty/json-empty-comments branch December 24, 2023 07:08
@Conaclos
Copy link
Member

It could be worth adding a changelog entry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Formatter Area: formatter L-JSON Language: JSON and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📝 Comments are displaced when formatting JSON with comments
3 participants