Skip to content
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

fix(js_formatter): don't hug blocks in case clauses with multiple statements #1035

Merged
merged 2 commits into from
Dec 4, 2023

Conversation

faultyserver
Copy link
Contributor

@faultyserver faultyserver commented Dec 4, 2023

Summary

I noticed this while checking diagnostics from trying to migrate our monorepo. Switch case clauses with a block body let the opening brace of the clause hug the same line as the case itself, like:

switch(true) {
  case true: {
    const a = 1;
  }
}

But when the same case clause contains additional statements after the block, Prettier doesn't allow them to hug:

switch(true) {
  case true: {
    const a = 1;
  }
  break;
}

// Prettier
switch (true) {
  case true:
    {
      const a = 1;
    }
    break;
}


// Biome 
switch (true) {
  case true: {
    const a = 1;
  }
  break;
}

This PR makes Biome match Prettier's behavior here: if there's more than a single non-empty block statement in the case clause, the braces won't be allowed to hug.

I think this is actually good behavior, even if it's somewhat ugly, because it shows more clearly that statements after the block (i.e., the break; above), are still part of the case clause, even though the braces might suggest the clause has ended.

I also noticed that trailing comments weren't staying on the same line, so I fixed that as part of this as well.

Test Plan

Added a new test case to cover this.

Copy link

netlify bot commented Dec 4, 2023

Deploy Preview for rad-torte-839a59 canceled.

Name Link
🔨 Latest commit 33a8d84
🔍 Latest deploy log https://app.netlify.com/sites/rad-torte-839a59/deploys/656d6f8501fd4a00082c8b2d

@github-actions github-actions bot added A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages labels Dec 4, 2023
@Conaclos Conaclos merged commit 3317660 into main Dec 4, 2023
18 checks passed
@Conaclos Conaclos deleted the faulty/switch-block branch December 4, 2023 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants