Skip to content

Comment in default block of switch is moving unexpectedly #12481

@stefcameron

Description

@stefcameron

Discovered this after attempting to upgrade from 2.5.1 to 2.6.0.

Prettier 2.6.0
Playground link

{
  "arrowParens": "always",
  "bracketSameLine": false,
  "bracketSpacing": true,
  "embeddedLanguageFormatting": "auto",
  "htmlWhitespaceSensitivity": "strict",
  "insertPragma": false,
  "jsxSingleQuote": false,
  "printWidth": 80,
  "proseWrap": "never",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false
}

Input:

function test() {
  switch (state) {
    case fieldStates.READONLY:
      result = fieldStates.ENABLED;
      break;
    case fieldStates.ACTIVE_READONLY:
      result = fieldStates.ACTIVE;
      break;
    case fieldStates.ERROR_READONLY:
      result = fieldStates.ERROR;
      break;
    case fieldStates.ERROR_ACTIVE_READONLY:
      result = fieldStates.ERROR_ACTIVE;
      break;
    default:
      result = state; // no change
      break;
  }
}

Output:

function test() {
  switch (state) {
    case fieldStates.READONLY:
      result = fieldStates.ENABLED;
      break;
    case fieldStates.ACTIVE_READONLY:
      result = fieldStates.ACTIVE;
      break;
    case fieldStates.ERROR_READONLY:
      result = fieldStates.ERROR;
      break;
    case fieldStates.ERROR_ACTIVE_READONLY:
      result = fieldStates.ERROR_ACTIVE;
      break;
    default: // no change
      result = state;
      break;
  }
}

Expected behavior:

The // no change comment shouldn't move. It should stay next to the line where I put it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:commentsIssues with how Prettier prints commentslang:javascriptIssues affecting JSlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions