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

feat(rome_js_formatter): Parenthesize TypeScript types #3108

Merged
merged 4 commits into from
Aug 30, 2022

Commits on Aug 29, 2022

  1. feat(rome_js_formatter): Parenthesize Types

    This PR implements the rules for when parentheses around TypeScript types are needed or can be removed without changing the semantics of the program.
    
    The majority of the PR is to implement `NeedsParentheses` for every `TsType`.
    
    This PR further fixes an instability issue with the syntax rewriter. The rewriter used to remove all whitespace between the `(` paren and the token (or first comment and skipped token trivia). This is necessary or the formatter otherwise inserts an extra blank line before nodes that are parenthesized:
    
    ```
    a
    (
      Long &&
      Longer &&
    )
    ```
    
    becomes
    
    ```
    a
    
    (
      Long &&
      Longer &&
    )
    ```
    
    Notice, the added new line. What this logic didn't account for is that it should not remove a leading new line before a comment because we want to keep the comment on its own line and this requires that there's a leading new line trivia.
    
    The last fix is in the source map that so far assumed that all ranges are added in increasing `end` order
    
    ```
    correct: 2..3, 2..4, 2..5 (notice how the ranges are sorted by end)
    incorrect: 2..4, 2..3, 2..5
    ```
    
    This PR updates the sorting of the text ranges to also account the end ranges.
    
    I added unit tests for all rules where parentheses are required and reviewed the updated snapshots.
    
    There are a few new changes but these unrelated to parentheses but instead problems with the formatting of the specific syntax.
    
    Average compatibility: 83.70 -> 84.11
    Compatible lines: 80.79 -> 81.42
    MichaReiser committed Aug 29, 2022
    Configuration menu
    Copy the full SHA
    4d77bcd View commit details
    Browse the repository at this point in the history
  2. My beloved friend clippy

    MichaReiser committed Aug 29, 2022
    Configuration menu
    Copy the full SHA
    8ae1f8e View commit details
    Browse the repository at this point in the history
  3. Format files

    MichaReiser committed Aug 29, 2022
    Configuration menu
    Copy the full SHA
    f2fdd85 View commit details
    Browse the repository at this point in the history
  4. Update Node tests

    MichaReiser committed Aug 29, 2022
    Configuration menu
    Copy the full SHA
    52386d2 View commit details
    Browse the repository at this point in the history