+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_json_formatter): implement JSON array formatting #4064

Merged
merged 13 commits into from
Dec 21, 2022

Conversation

dhirajarun
Copy link
Contributor

Summary

JSON Array Formating #2570

@netlify
Copy link

netlify bot commented Dec 16, 2022

Deploy Preview for docs-rometools canceled.

Name Link
🔨 Latest commit 65acddc
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/63a31af94955190008d7e3c4

@dhirajarun dhirajarun requested a review from leops as a code owner December 17, 2022 05:37
@dhirajarun
Copy link
Contributor Author

Some of the prettier snapshot tests are failing, should I accept those?

MichaReiser added a commit that referenced this pull request Dec 17, 2022
… errors

Some prettier tests for JSON5 are failing after implementing array formatting in #4064

The tests fail because the formatting returns a `FormatError::SyntaxError` if a mandatory child node is missing but the error is never handled by the JSON formatting.

This PR fixes this by

* Gracefully handle missing values on the root value by calling into verbatim formatting if that's the case
* Use `format_or_verbatim` when formatting values

I had to pull out the `format_or_verbatim` from the `rome_js_formatter` and move it to `rome_formatter` so that it can be re-used between js and json formatting.
Copy link
Contributor

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Yes. You'll need to update the prettier snapshots. Ideally, most of the output will match prettier precisely after your changes (as all JSON formatting is implemented).

However, you have to wait for #4066 to land because some JSON 5 tests are failing because of a syntax error on the root level.

MichaReiser added a commit that referenced this pull request Dec 17, 2022
… errors

Some prettier tests for JSON5 are failing after implementing array formatting in #4064

The tests fail because the formatting returns a `FormatError::SyntaxError` if a mandatory child node is missing but the error is never handled by the JSON formatting.

This PR fixes this by

* Gracefully handle missing values on the root value by calling into verbatim formatting if that's the case
* Use `format_or_verbatim` when formatting values

I had to pull out the `format_or_verbatim` from the `rome_js_formatter` and move it to `rome_formatter` so that it can be re-used between js and json formatting.
Co-authored-by: Micha Reiser <micha@reiser.io>
@dhirajarun
Copy link
Contributor Author

Yes. You'll need to update the prettier snapshots. Ideally, most of the output will match prettier precisely after your changes (as all JSON formatting is implemented).

However, you have to wait for #4066 to land because some JSON 5 tests are failing because of a syntax error on the root level.

I'll wait then

ematipico pushed a commit that referenced this pull request Dec 19, 2022
… errors (#4066)

Some prettier tests for JSON5 are failing after implementing array formatting in #4064

The tests fail because the formatting returns a `FormatError::SyntaxError` if a mandatory child node is missing but the error is never handled by the JSON formatting.

This PR fixes this by

* Gracefully handle missing values on the root value by calling into verbatim formatting if that's the case
* Use `format_or_verbatim` when formatting values

I had to pull out the `format_or_verbatim` from the `rome_js_formatter` and move it to `rome_formatter` so that it can be re-used between js and json formatting.
@MichaReiser
Copy link
Contributor

The last thing missing after merging main is to use the fill layout if the array only consists of string, number, boolean, or null literals as you can see in this example

The logic should work similar to the JavaScript array formatting

let layout = if can_concisely_print_array_list(node, f.context().comments()) {
ArrayLayout::Fill
} else {
ArrayLayout::OnePerLine
};
match layout {
ArrayLayout::Fill => {
let trailing_separator = FormatTrailingComma::ES5.trailing_separator(f.options());
let mut filler = f.fill();
// Using format_separated is valid in this case as can_print_fill does not allow holes
for (element, formatted) in node.iter().zip(
node.format_separated(",")
.with_trailing_separator(trailing_separator)
.with_group_id(self.group_id),
) {
filler.entry(
&format_once(|f| {
if get_lines_before(element?.syntax()) > 1 {
write!(f, [empty_line()])
} else {
write!(f, [soft_line_break_or_space()])
}
}),
&formatted,
);
}
filler.finish()
}
ArrayLayout::OnePerLine => write_array_node(node, f),
}

@MichaReiser MichaReiser added A-Formatter Area: formatter L-JSON Language: JSON labels Dec 20, 2022
@MichaReiser MichaReiser added this to the Next milestone Dec 20, 2022
@MichaReiser MichaReiser mentioned this pull request Dec 20, 2022
6 tasks
@dhirajarun
Copy link
Contributor Author

The last thing missing after merging main is to use the fill layout if the array only consists of string, number, boolean, or null literals as you can see in this example

The logic should work similar to the JavaScript array formatting

let layout = if can_concisely_print_array_list(node, f.context().comments()) {
ArrayLayout::Fill
} else {
ArrayLayout::OnePerLine
};
match layout {
ArrayLayout::Fill => {
let trailing_separator = FormatTrailingComma::ES5.trailing_separator(f.options());
let mut filler = f.fill();
// Using format_separated is valid in this case as can_print_fill does not allow holes
for (element, formatted) in node.iter().zip(
node.format_separated(",")
.with_trailing_separator(trailing_separator)
.with_group_id(self.group_id),
) {
filler.entry(
&format_once(|f| {
if get_lines_before(element?.syntax()) > 1 {
write!(f, [empty_line()])
} else {
write!(f, [soft_line_break_or_space()])
}
}),
&formatted,
);
}
filler.finish()
}
ArrayLayout::OnePerLine => write_array_node(node, f),
}

done

@MichaReiser MichaReiser merged commit 8d5b2ea into rome:main Dec 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Formatter Area: formatter L-JSON Language: JSON
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载