azat-io / eslint-plugin-perfectionist

🦄 ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
https://eslint-plugin-perfectionist.azat.io
MIT License
1.62k stars 28 forks source link

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

Open datismoa opened 5 months ago

datismoa commented 5 months ago

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

danvk commented 3 months ago

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)