refactor(js_formatter): move NeedsParentheses
trait to biome_js_syntax
#3541
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR moves the
NeedsParentheses
trait and its implementations frombiome_js_formatter
tobiome_js_syntax
.I had also to move some utility types (
AnyJsBinaryLikeExpression
,AnyJsBinaryLikeLeftExpression
,BinaryLikeOperator
andAnyJsExpressionLeftSide
).I plan to move
AnyJsExpressionLeftSide
in a dedicated file.Also, I want to evaluate converting some methods to functions in order to move them back in
biome_js_formatter
.I removed some implementations of
NeedsParentheses
that was not used (AnyTsType
) and implementations ofNeedsParentheses
forAnyTsType
variants that returnfalse
.I inlined some small utility functions and removed some utility types (
TsAsOrSatisfiesExpression
,TsAsOrSatisfiesAssignment
). This reduces node cloning and improves readability of the code.I took the opportunity of improving some implementations (replacing
map_or(false, X)
withis_some_and(X)
, use consistentlycast
/cast_ref
/try_cast
).Many implementations of
NeedsParentheses::needs_parentheses
used the attribute#[inline]
or#[inline(always)]
. I replaced all occurrence of#[inline(always)]
with#[inline]
and added#[inline]
where it was missing. I am not sure if it is actually useful to add this attribute. I added it for consistency,Remaining tasks:
AnyJsExpressionLeftSide
in a dedicated file?Test Plan
I first moved all tests from
biome_js_formatter
tobiome_js_syntax
; unfortunately the test assertions use the formatter.Thus, I commented the tests in
biome_js_syntax
and restored the ones inbiome_js_formatter
.I don't think we are able to properly test the implementations in
biome_js_syntax
because building nodes by hand is a lot of work and doesn't allow accessing parent nodes. ManyNeedsParentheses
implementations retrieve the parent node to decide if a node needs parentheses.