eslint-plugin-perfectionist icon indicating copy to clipboard operation
eslint-plugin-perfectionist copied to clipboard

If something should be sorted, move all above comments with that before a blank line or a non-comment entity is found

Open datismoa opened this issue 1 year ago • 1 comments

What rule do you want to change?

sort-object-types, sort-objects, sort-classes, ...?

Describe the problem

When some entity is being sorted, move along with it not only the first comment, but all of them before a blank line or a non-comment entity is found

Code example

(natural sorting is used)

Example 1:

const test = {
  // yes 1
  // yes 2
  name: 'Immanuel',

  // first blablabla
  // second blablabla
  age: 79,

  // what a nice year
  bornYear: 1724,
}

to

const test = {
  // first blablabla
  // second blablabla
  age: 79,

  // what a nice year
  bornYear: 1724,

  // yes 1
  // yes 2
  name: 'Immanuel',
}

Example 2:

const test = {
  // :)
  /** 
   * yeah
   * works
   * too
   */
  status: 'who_knows',
  key: 'value', // sticked!
  // something more
  c: 'good!',
}

to

const test = {
  // something more
  c: 'good!',
  key: 'value', // sticked!
  // :)
  /** 
   * yeah
   * works
   * too
   */
  status: 'who_knows',
}

Example 3:

class Test {
  name: string

  // first blablabla
  // second blablabla
  age: number
}

to

class Test {
  // first blablabla
  // second blablabla
  age: number

  name: string
}

Example 4

type TTest = {
  b: string

  // wanna
  // be
  a: 123
}

to

type TTest = {
  // wanna
  // be
  a: 123

  b: string
}

Additional comments

Really needed this functionality, so I have hastily created a workaround. Repository: https://github.com/datismoa/eslint-plugin-perfectionist. Does not break any of tests, tho

Would be great if we had an option that regulates such behaviour & the ability to choose in what entities and how (for example, count a multiline comment as an inhibitor) it works

Validations

  • [X] Read the docs.
  • [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.

datismoa avatar Dec 20 '23 06:12 datismoa

This would also be helpful for keeping eslint-disable-next-line comments associated with the thing they are trying to disable:

class C {
  z = new Date();
  // eslint-disable-next-line perfectionist/sort-classes
  a = this.z;
}

(Context: https://github.com/azat-io/eslint-plugin-perfectionist/issues/102)

danvk avatar Jan 20 '24 15:01 danvk

Fixed in bd8ba3f. Released in v3.6.0.

azat-io avatar Sep 13 '24 16:09 azat-io