-
Notifications
You must be signed in to change notification settings - Fork 657
fix(rome_js_analyze): keep comments when simplifying logical expression #3532
Conversation
✅ Deploy Preview for rometools ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
0bf0155
to
ec795b5
Compare
Parser conformance results on ubuntu-latestjs/262
jsx/babel
symbols/microsoft
ts/babel
ts/microsoft
|
crates/rome_js_analyze/src/analyzers/correctness/use_simplified_logic_expression.rs
Outdated
Show resolved
Hide resolved
crates/rome_js_analyze/src/analyzers/correctness/use_simplified_logic_expression.rs
Outdated
Show resolved
Hide resolved
crates/rome_cli/tests/snapshots/main_commands_ci/ci_runs_linter_not_formatter_issue_3495.snap
Outdated
Show resolved
Hide resolved
40aad9f
to
74ba6be
Compare
crates/rome_js_analyze/src/analyzers/correctness/use_simplified_logic_expression.rs
Outdated
Show resolved
Hide resolved
crates/rome_js_analyze/src/analyzers/correctness/use_simplified_logic_expression.rs
Outdated
Show resolved
Hide resolved
74ba6be
to
1fc9dbc
Compare
let mut new_token = | ||
new_token.with_trailing_trivia(vec![(TriviaPieceKind::Whitespace, " ")]); |
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.
Nit
let mut new_token = | |
new_token.with_trailing_trivia(vec![(TriviaPieceKind::Whitespace, " ")]); | |
let mut new_token = | |
new_token.with_trailing_trivia([(TriviaPieceKind::Whitespace, " ")]); |
let right_argument_trivia = right | ||
.syntax() |
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.
This won't return the right argument trivia, but instead, the trivia of the unary operator. If it returns the trivia of the right argument, it gets duplicated.
Overall, I think it is more explicit and safer to use the AST traversal methods instead because it clearly communicates the intent and ensures the transformation works correctly even if the unary expression misses an operator.
let right_unary_operator_trivia = right.operator_token().ok()?.leading_trivia().pieces();
This should also be faster because it doesn't require a full traversal to find the first trivia.
.map(|trivia| trivia.pieces()); | ||
if let Some(mut right_argument_trivia) = right_argument_trivia { | ||
if right_argument_trivia.any(|piece| piece.is_comments()) { | ||
new_token = new_token.with_trailing_trivia_pieces(right_argument_trivia); |
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.
Is it intentional that this overrides the trivia from line 230?
new_token.with_trailing_trivia(vec![(TriviaPieceKind::Whitespace, " ")]); | ||
let right_argument_trivia = right | ||
.syntax() | ||
.first_leading_trivia() |
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.
What about the trailing trivia of the operator_token?
! /* comment */ b
This PR is stale because it has been open 14 days with no activity. |
Summary
Closes #3527
I created a new mutation API called
replace_token_transfer_trivia
, which transfer the trivias from the old token, while keeping the trivia of the new token.Test Plan
Added two new test cases