prettier icon indicating copy to clipboard operation
prettier copied to clipboard

Support prettier-ignore-start/end for other languages (not only Markdown)

Open brunolemos opened this issue 6 years ago • 66 comments

It seems this feature was added only for markdown (#4202 cc @ikatyang), but it's actually useful for other languages as well. It is not working on typescript files on my tests.

Related: https://github.com/facebook/create-react-app/issues/5543

Environments:

  • Prettier Version: 1.14.3
  • Running Prettier via: CLI
  • Runtime: Node.js v8.11.4
  • Operating System: macOS

Steps to reproduce:

Create a index.d.ts file with the following content:

// prettier-ignore
type A1 = string;

// prettier-ignore
type A2 = string

/* prettier-ignore-start */
type B1 = string;
type B2 = string
/* prettier-ignore-end */

type C1 = string;
type C2 = string

Expected behavior:

// prettier-ignore
type A1 = string;

// prettier-ignore
type A2 = string

/* prettier-ignore-start */
type B1 = string;
type B2 = string
/* prettier-ignore-end */

-type C1 = string;
+type C1 = string
type C2 = string

Actual behavior:

// prettier-ignore
type A1 = string;

// prettier-ignore
type A2 = string

/* prettier-ignore-start */
-type B1 = string;
+type B1 = string
type B2 = string
/* prettier-ignore-end */

-type C1 = string;
+type C1 = string
type C2 = string

brunolemos avatar Oct 23 '18 14:10 brunolemos

I have scripts that autogenerate chunks of class property type declarations for Sequelize classes like the following, really need this feature to make things more convenient:

  /* prettier-ignore-start */
  static Users: Association.BelongsToMany<OrganizationAttributes, OrganizationInitAttributes, Organization, UserAttributes, UserInitAttributes, User, OrganizationMemberAttributes, OrganizationMember> = (null: any);
  getUsers: BelongsToManyGetMany<User>;
  setUsers: BelongsToManySetMany<User, number, OrganizationMemberThroughInitAttributes>;
  addUsers: BelongsToManyAddMany<User, number, OrganizationMemberThroughInitAttributes>;
  addUser: BelongsToManyAddOne<User, number, OrganizationMemberThroughInitAttributes>;
  createUser: BelongsToManyCreateOne<UserInitAttributes, User, OrganizationMemberThroughInitAttributes>;
  removeUser: BelongsToManyRemoveOne<User, number>;
  removeUsers: BelongsToManyRemoveMany<User, number>;
  hasUser: BelongsToManyHasOne<User, number>;
  hasUsers: BelongsToManyHasMany<User, number>;
  countUsers: BelongsToManyCount;
  /* prettier-ignore-end */

jedwards1211 avatar Nov 16 '18 00:11 jedwards1211

Would be very useful for prettier --fix when you have a structure you don't want to be fixed or linted.

 /* prettier-ignore-start */
      topLeft: "╔", topMid: "╦", top: "═", topRight: "╗",
      midLeft: "╠", midMid: "╬", mid: "═", midRight: "╣",
      botLeft: "╚", botMid: "╩", bot: "═", botRight: "╝"
  /* prettier-ignore-end */

/* prettier-ignore-[end|start] */ would be very good

I would also add

  • /* prettier-ignore-next-line */: to ignore the line after
  • // prettier-ignore-line: to ignore the line the commend is in

muuvmuuv avatar Nov 27 '18 11:11 muuvmuuv

@muuvmuuv There’s already // prettier-ignore – check it out! https://prettier.io/docs/en/ignore.html#javascript

lydell avatar Nov 27 '18 15:11 lydell

@lydell That helps @muuvmuuv but sometimes we have multiple AST nodes to ignore, it's absurd to add // prettier-ignore to each line:

/* prettier-ignore-start */
route('my-designs', '/my-designs/', require('./pages/my-designs/my-designs'));
route('order', '/:project/:thank/order/', require('./pages/order/order'));
route('order-success', '/:project/order-success/', require('./pages/order-success/order-success'));
route('page-not-found', '/page-not-found/', require('./pages/page-not-found/page-not-found'));
route('password-forgot', '/password-forgot/', require('./pages/password-forgot/password-forgot'));
route('restore-password', '/restore-password/:token', require('./pages/restore-password/restore-password'));
/* prettier-ignore-end */

The only valid option for now is to ignore the whole file.

matias-quezada avatar Jan 29 '19 11:01 matias-quezada

@matias-quezada Correct, my reply was to @muuvmuuv.

lydell avatar Jan 29 '19 15:01 lydell

The // prettier-ignore-start and // prettier-ignore-end are not working in JS, right @lydell?

adelriosantiago avatar Feb 06 '19 06:02 adelriosantiago

@adelriosantiago That's correct, that's what this issue is about.

lydell avatar Feb 06 '19 06:02 lydell

+1

sturmenta avatar May 08 '19 22:05 sturmenta

6 months later, any ideas ?

notiv-nt avatar May 11 '19 18:05 notiv-nt

+1 for this block ignore

lucksp avatar Jul 12 '19 20:07 lucksp

+1

[edit]... I post a +1 and get confused emojis? a month after previous users post +1 comments and get upvotes... wth?

mdeeter avatar Aug 08 '19 20:08 mdeeter

I would like a // prettier-disable-line. I currently need an eslint comment: // eslint-disable-line @typescript-eslint/no-unused-vars but prettier keeps on shifting this to the next line.

basickarl avatar Aug 14 '19 14:08 basickarl

@basickarl It's recommended to use // eslint-disable-next-line rather than // eslint-disable-line because of that issue.

lydell avatar Aug 14 '19 15:08 lydell

@lydell I see! Thanks, I'll use this instead.

basickarl avatar Aug 16 '19 11:08 basickarl

In javascript prettier-ignore ignores the next block, so what I did was just make the next few lines a block.

Before:

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

After

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

Please mind some block scoped statements like const and let within the block you're creating.

christiaanwesterbeek avatar Oct 23 '19 09:10 christiaanwesterbeek

I'm interested in this as well. When trying to use descriptive names with React hooks this can quickly muddy up variable declarations I have at the top of a component. Being able to ignore a block would allow these variable names to remain description, but also keep them single-line which helps keep the code readable.

jaredmeakin avatar Oct 23 '19 19:10 jaredmeakin

Any update? It's pretty annoying having to disable formatting on every single line when multiple lines need to be ignored (not an actual code block), and the code gets difficult to read that way.

renaldas-kerpe-arria avatar Oct 29 '19 11:10 renaldas-kerpe-arria

this would also be very useful for people working with express.js, something like this:

router.post('/validate', async (req, res, next) => {
    //route code here
}

get's formatted like this:

router.post(
	'/validate',
	async (req, res, next) => {
   //route code here
}

However, it's better for readability to keep the first line intact, while formatting the actual route code. If I add a // prettier-ignore before the first line, prettier ignores everything (including the actual route code)

One should be able to do this in order to remedy this type of situation.

// prettier-ignore-next-line
router.post('/validate', async (req, res, next) => {
    //formatted route code here
}

skourismanolis avatar Dec 02 '19 17:12 skourismanolis

Please get this on the roadmap. There are many reasons why you may want to format things differently, particularly if it makes your code more legible. Easily opting out for short sections is a no brainer IMO, and matches what we expect from eslint and other similar tools.

theoephraim avatar Jan 22 '20 18:01 theoephraim

So, it's been years, obviously this would be an amazing feature - why won't it get implemented? :(

fkoeppling avatar Jun 04 '20 19:06 fkoeppling

@MIHAUKI See that green "help wanted" label? Hover your mouse cursor over it and read.

thorn0 avatar Jun 04 '20 19:06 thorn0

I've found myself wanting to make use of this feature quite a few times myself. Noticed that with 100+ :+1:s, I am not alone here. There are definitely a lot of devs wanting this feature. So I took some time out and decided to give this a go myself. I have created a PR for the same: https://github.com/prettier/prettier/pull/8926

chaitan94 avatar Aug 06 '20 18:08 chaitan94

I want it to ignore multiple lines in CSS also. Don't limit it to Js-files. Right now I make-up hacky CSS-blocks to make it work with just "prettier-ignore".

This should be basic functionality.

mikael1000 avatar Sep 25 '20 21:09 mikael1000

I just stumbled upon this issue and was also surprised why it's only implemented for Markdown ):

alfaproject avatar Nov 24 '20 12:11 alfaproject

@brunolemos opened this issue on 23 Oct 2018 & today is 25 Nov 2020.

Is I am the only one who is still waiting for this feature to be implemented?

neilveil avatar Nov 24 '20 20:11 neilveil

@neilveil Go ahead, implement it, and everyone will love you!

thorn0 avatar Nov 24 '20 20:11 thorn0

Okay, I will start working on this feature if there are at least 100 thumbs up on this comment. Let's see if people still need this feature except myself.

neilveil avatar Dec 01 '20 23:12 neilveil

@neilveil definitely needed, setting up test data and trying to avoid hitting the printWidth rule with args forcing line breaks.

Ne-Ne avatar Dec 02 '20 12:12 Ne-Ne

@Ne-Ne Doesn't this trick work for you? In Markdown, it's not possible to do this, that's why this feature is more relevant for that language than for the others.

thorn0 avatar Dec 03 '20 12:12 thorn0

I took a first stab at how this sort of feature might be implemented here: https://github.com/prettier/prettier/pull/9818. You can play around with it here. There are a lot of cases that don't yet work, and whitespace seems to be ignored occasionally. I'm relatively new to Prettier's codebase, so any feedback would be much appreciated.

salemhilal avatar Dec 03 '20 14:12 salemhilal