-
-
Notifications
You must be signed in to change notification settings - Fork 575
challenge(formatter): add bracketSpacing
option matching Prettier's setting
#627
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
Conversation
pub fn conditional_space(should_insert: bool) -> Option<Space> { | ||
if should_insert { | ||
Some(Space) | ||
} else { | ||
None | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this conditional_space
function just for clarity at the callsites. Not sure if it has too much utility outside of this option, but I like the "branchless" flow from the caller side while still making it clear that the space may or may not exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using conditional_space(cond)
at call site, did you try cond.then_some(space())
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using
conditional_space(cond)
at call site, did you trycond.then_some(space())
?
That wouldn't be very ergonomic when writing the IR, and it saves us a bunch of branching.
I can totally see the usage of this builder because our infra can understand Option
. I would call this function maybe_space
(sorry for the bike-shedding) because it's a prefix we already used through the code base,.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can definitely agree with maybe_*
. Honestly idk why I didn't go with that in the first place since it's normally what I end up using lol.
I didn't actually know .then_some
existed when I wrote this code (still very new to Rust), and had I known I might've used that instead. But I do like the declarative nature of saying maybe_
for things, along with the advantage of working with any boolean value, not just an Option
.
PR should be updated with the name change now.
crates/biome_js_formatter/src/ts/auxiliary/property_signature_type_member.rs
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
@faultyserver you can be interested by #720 |
bracketSpacing
option matching Prettier's settingbracketSpacing
option matching Prettier's setting
Happy to turn this into a challenge feature! I hadn't seen anything about whether there was a goal to support all of Prettier's options as part of the challenge or not. |
@faultyserver FYI we just got confirmation that the challenge does aim for all prettier js options, after we asked for clarification |
727af4d
to
9375775
Compare
9375775
to
01d6202
Compare
I ended up basically re-implementing the feature after trying to rebase off main and somehow ended up with 20 fewer files changed lol. But I think this is now ready to go again pending CI. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation looks good, and fortunately, this option doesn't add much friction in terms of maintenance.
I added some comments we should address before merging, although I am very pleased to see this contribution, thank you @faultyserver !
@@ -36,6 +36,10 @@ pub struct JavascriptFormatter { | |||
#[bpaf(long("arrow-parentheses"), argument("always|as-needed"), optional)] | |||
#[serde(skip_serializing_if = "Option::is_none")] | |||
pub arrow_parentheses: Option<ArrowParentheses>, | |||
/// Whether to insert spaces around brackets in object literals. Defaults to true. | |||
#[bpaf(long("bracket-spacing"), argument("BOOLEAN"), optional)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For arguments that are true/false, at the moment we use different implementation, e.g.
#[bpaf(long("vcs-use-ignore-file"), argument("true|false"), optional)]
pub use_ignore_file: Option<bool>,
We could change all of them to use BOOLEAN
, but for now we should be more consistent. .
pub fn conditional_space(should_insert: bool) -> Option<Space> { | ||
if should_insert { | ||
Some(Space) | ||
} else { | ||
None | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using
conditional_space(cond)
at call site, did you trycond.then_some(space())
?
That wouldn't be very ergonomic when writing the IR, and it saves us a bunch of branching.
I can totally see the usage of this builder because our infra can understand Option
. I would call this function maybe_space
(sorry for the bike-shedding) because it's a prefix we already used through the code base,.
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub fn soft_block_indent_with_conditional_space<Context>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as the previous suggestion: soft_block_indent_with_maybe_space
ffa37b8
to
13f74f3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you forgot one minor detail, but other than that, looks good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! We just need to fix the conflicts with the new options we just added and we can merge the PR! Again, great work here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can't really properly review a PR like this without my browser tab lagging but i'm approving to remove the previous requested changes review. great work! thanks a lot for contributing ❤
should hopefully be updated with no conflitcs now 🤞 |
3471369
to
008d490
Compare
i should stop assuming before CI passes, but now finally it's all set. |
Summary
This PR adds a new
bracketSpacing
option for the js formatter which matches Prettier's equivalent option. This option controls whether spaces are inserted around "object-like literals", meaning object expressions, named import/export specifier lists, import assertions, and type expressions.The following is a case that should cover pretty much every variation that this option affects:
With
bracketSpacing: false
, this code should appear unchanged after formatting.By default,
bracketSpacing
is left astrue
, just like in Prettier, and users will have to explicitly disable it to see any changes from their current formatting.The goal of adding this option is to ease adoption from existing Prettier users. While Biome does not (and I'm assuming won't) aim to have perfect parity with Prettier's features, bracket spacing is a commonly-set option, and one that causes a massive diff for large codebases.
Not much other discussion has happened on this option, other than this post from a little over a month ago: #392.
Implementation Notes
I'm both relatively new to Rust and completely new to this project, so I looked for inspiration on how to add an option by finding a previous addition,
arrowParentheses
, and copying over the plumbing, then implementing the feature, and finally adding the relevant tests.I decided to use a plain
bool
value for the option rather than a string/enum only because that's what Prettier does. If a string value would be preferred, I'm happy to change it to match those as well.Test Plan
cargo test
with a bunch of new snapshots. A local playground link with the sample code above also demonstrates the behavior.