Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

809
Vistas
How to make Prettier to ignore a block of code?

Say we have a line of code:

const a = 'a'; const b = 'b';

and we wouldn't want it to be formatter by Prettier.

What I've tried so far:

1)

// prettier-ignore
const a = 'a'; const b = 'b';
// prettier-ignore-start
const a = 'a'; const b = 'b';
// prettier-ignore-end

In both cases it gets transformed into:

const a = 'a';
const b = 'b';

So how to ignore a block of code?

over 4 years ago · Santiago Trujillo
5 Respuestas
Responde la pregunta

0

The reason why // prettier-ignore does not work here is that // prettier-ignore will exclude the next node in the abstract syntax tree from formatting (see https://prettier.io/docs/en/ignore.html).

In your case the next node would only be const a = 'a';. For instance,

// prettier-ignore
const a = 
  'a';

would be preserved after formatting.

If you want to keep both assignments in one line without changing the prettier configuration for your whole file, you could use destructuring assignment if you are using es6 or node:

const [a, b] = ["a", "b"];
over 4 years ago · Santiago Trujillo Denunciar

0

Sometimes multiple statements can be wrapped in a block with // prettier-ignore in front of it:

// prettier-ignore
{
abcRouter('/api/abc', server);
xRouter  ('/api/x', server);
}

Of course, that doesn't make sense for block-level const declarations, but you wrote that was not your actual code and just an example. So that's a solution that works in some but not in all cases. Overall, the strategy is to wrap multiple things in one thing that can be prettier-ignored.

Another option is to move all the code you don't want to format (e.g., because it's generated) to a separate file excluded by .prettierignore.

prettier-ignore-start and prettier-ignore-end are supported only in Markdown.

over 4 years ago · Santiago Trujillo Denunciar

0

You can ignore the whole method

// prettier-ignore
function myFunc() {
    const a = 'a'; const b = 'b';
    return a + b;
}

Actually I tried only on typescript. But I think should works on JS as well.

over 4 years ago · Santiago Trujillo Denunciar

0

You can ignore a code block is not supported at the moment, your option is is to only use // prettier-ignore

example below:

// prettier-ignore
const a = 'a';
const b = 'b';

// prettier-ignore
function xyz() {
 console.log({a, b})
}

known issue -> https://github.com/prettier/prettier/issues/5287

Docs -> https://prettier.io/docs/en/ignore.html#javascript

over 4 years ago · Santiago Trujillo Denunciar

0

It sucks, but this isnt currently supported.

While this doesnt work for OP's example, one option is to add the prettier-ignore comment to the start of each line to be ignored, using the /* */ comment style, eg.

/* prettier-ignore */ import { SomeTypeA, SomeTypeB, SomeTypeC } from './some-long-folder-name/super-long-import';
/* prettier-ignore */ import { someFnA, someFnB, someFnC, } from './another-long-folder-name/super-long-import';

In my opinion, this is easier on the eyes than the alternatives;

Letting prettier reformat the imports:

import {
    SomeTypeA,
    SomeTypeB,
    SomeTypeC,
} from './some-long-folder-name/super-long-import';
import {
    someFnA,
    someFnB,
    someFnC,
} from './another-long-folder-name/super-long-import';

Specifying the // prettier-ignore comment above each import:

// prettier-ignore
import { SomeTypeA, SomeTypeB, SomeTypeC } from './some-long-folder-name/super-long-import';
// prettier-ignore
import { someFnA, someFnB, someFnC, } from './another-long-folder-name/super-long-import';
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda