Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
Enforce curly braces for JS switch-cases with ESLint or Prettier

I'd like to enforce curly braces for switch-cases in JS, possibly with ESLint or Prettier. Do you know of any related config, or if not that, then any other linting or formatting tool?

I tried the ESLint rule curly set to "all", but it didn't compain about my curly-less switch-cases.

There is a switch-case ESLint plugin, but I haven't found such a rule in its doc, neither in its source code.

Example

Wrong:

switch (foo) {
  case "bar":
    return 1;
  case "baz":
    return 2;
  default:
    return 0;
}

Correct:

switch (foo) {
  case "bar": {
    return 1;
  }
  case "baz": {
    return 2;
  }
  default: {
    return 0;
  }
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The switch-braces rule in the following eslint custom rules repo seems to be the one you are looking for: https://github.com/justinanastos/eslint-plugin-justinanastos/blob/master/docs/rules/switch-braces.md

You can give it a try.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try 'no-restricted-syntax' rule with such selector.

With verbose output (each non-block child of a case is reported as error):

    'no-restricted-syntax': ['error',
      {
        'selector': 'SwitchCase > *.consequent[type!="BlockStatement"]',
        'message': 'Switch cases without blocks are disallowed.'
      },
    ],

If :has is supported by your parser, you can try this one, but it can have false-positive reports: since child (relative) selectors currently seem to be disallowed in :has, descendant non-block .consequent nodes may throw.

    'no-restricted-syntax': ['error',
      {
        'selector': 'SwitchCase:has(*.consequent[type!="BlockStatement"])',
        'message': 'Switch cases without blocks are disallowed.'
      },
    ],
about 4 years ago · Juan Pablo Isaza Relatório

0

*This applies to eslint without plugins.

The curly rule does not apply to blocks in switch cases. Unfortunately there is no good rule that does what you need. You can suggest the rule (or edit to curly rule) here https://github.com/eslint/eslint/issues.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda