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

fix(rome_js_analyze): keep comments when simplifying logical expression #3532

Closed
wants to merge 3 commits into from

Conversation

ematipico
Copy link
Contributor

@ematipico ematipico commented Oct 28, 2022

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

@ematipico ematipico requested a review from a team October 28, 2022 11:04
@netlify
Copy link

netlify bot commented Oct 28, 2022

Deploy Preview for rometools ready!

Name Link
🔨 Latest commit 1fc9dbc
🔍 Latest deploy log https://app.netlify.com/sites/rometools/deploys/636145e609f3cb0008ec1e9c
😎 Deploy Preview https://deploy-preview-3532--rometools.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@ematipico ematipico temporarily deployed to netlify-playground October 28, 2022 11:04 Inactive
@ematipico ematipico force-pushed the fix/simplified-logic-expression branch from 0bf0155 to ec795b5 Compare October 28, 2022 11:05
@ematipico ematipico temporarily deployed to netlify-playground October 28, 2022 11:05 Inactive
@github-actions
Copy link

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45879 45879 0
Passed 44939 44939 0
Failed 940 940 0
Panics 0 0 0
Coverage 97.95% 97.95% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 39 39 0
Passed 36 36 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.31% 92.31% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 5946 5946 0
Passed 1621 1621 0
Failed 4325 4325 0
Panics 0 0 0
Coverage 27.26% 27.26% 0.00%

ts/babel

Test result main count This PR count Difference
Total 588 588 0
Passed 519 519 0
Failed 69 69 0
Panics 0 0 0
Coverage 88.27% 88.27% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 16257 16257 0
Passed 12395 12395 0
Failed 3862 3862 0
Panics 0 0 0
Coverage 76.24% 76.24% 0.00%

@github-actions
Copy link

github-actions bot commented Oct 28, 2022

@ematipico ematipico temporarily deployed to netlify-playground October 28, 2022 12:45 Inactive
@ematipico ematipico requested a review from MichaReiser October 28, 2022 12:46
@ematipico ematipico force-pushed the fix/simplified-logic-expression branch from 40aad9f to 74ba6be Compare October 28, 2022 13:53
@ematipico ematipico temporarily deployed to netlify-playground October 28, 2022 13:53 Inactive
@ematipico ematipico changed the title fix(rome_js_analyze): keep tokens when simplifying logical expression fix(rome_js_analyze): keep comments when simplifying logical expression Oct 28, 2022
@ematipico ematipico requested a review from xunilrj October 28, 2022 14:12
@ematipico ematipico force-pushed the fix/simplified-logic-expression branch from 74ba6be to 1fc9dbc Compare November 1, 2022 16:14
@ematipico ematipico temporarily deployed to netlify-playground November 1, 2022 16:14 Inactive
@ematipico ematipico requested a review from MichaReiser November 1, 2022 16:16
Comment on lines +229 to +230
let mut new_token =
new_token.with_trailing_trivia(vec![(TriviaPieceKind::Whitespace, " ")]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit

Suggested change
let mut new_token =
new_token.with_trailing_trivia(vec![(TriviaPieceKind::Whitespace, " ")]);
let mut new_token =
new_token.with_trailing_trivia([(TriviaPieceKind::Whitespace, " ")]);

Comment on lines +231 to +232
let right_argument_trivia = right
.syntax()
Copy link
Contributor

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);
Copy link
Contributor

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()
Copy link
Contributor

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

@MichaReiser MichaReiser added this to the 11.0.0 milestone Nov 10, 2022
@github-actions
Copy link

This PR is stale because it has been open 14 days with no activity.

@ematipico ematipico removed this from the 11.0.0 milestone Dec 12, 2022
@ematipico ematipico added this to the Next milestone Dec 12, 2022
@github-actions github-actions bot removed the S-Stale label Dec 13, 2022
@Conaclos Conaclos modified the milestones: v12.0.0, v13.0.0 Mar 28, 2023
@ematipico ematipico closed this May 2, 2023
@ematipico ematipico removed this from the v12.1.0 milestone May 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 useSimplifiedLogicExpression removes comments in its code action
4 participants