bryanrsmith / eslint-plugin-sort-class-members

An ESLint rule for enforcing consistent ES6 class member order
119 stars 21 forks source link

feat: support @babel/eslint-parser 7.16.0 (espree8) and above #79

Closed AkatQuas closed 2 years ago

AkatQuas commented 2 years ago

@babel/eslint-parser has some breaking change in private property between 7.16.0+ and 7.15.8 .

So the private condition need some modifications.

I think this happens when espree@7 upgrade to espree@8

bryanrsmith commented 2 years ago

Thanks @AkatQuas! I'm away on vacation right now, but will try to get to this next week. Sorry for the delay!

bryanrsmith commented 2 years ago

Thank you for working on this @AkatQuas! I believe I've resolved the problem in a backwards-compatible fashion on main. I've also added tests for both current and legacy babel parsers, as well as the typescript parser. The fix is included in eslint-plugin-sort-class-members@1.15.1. Please let me know how it works for you!

AkatQuas commented 2 years ago

Thanks for the great work!

I found when parsed by '@typescript-eslint/parser', a private property such as private _c = 1; is parsed to be like the following:

 <ref *1> {
    type: 'PropertyDefinition',
    key: {
      type: 'Identifier',
      name: '_c',
      range: [Array],
      loc: [Object],
      parent: [Circular *1]
    },
    value: {
      type: 'Literal',
      value: 8,
      raw: '8',
      range: [Array],
      loc: [Object],
      parent: [Circular *1]
    },
    computed: false,
    static: false,
    readonly: undefined,
    declare: false,
    override: false,
    range: [ 195, 210 ],
    loc: { start: [Object], end: [Object] },
    accessibility: 'private',
    parent: {
      type: 'ClassBody',
      body: [Array],
      range: [Array],
      loc: [Object],
      parent: [Object]
    }
  }

It doesn't have a key PrivateIdentifier node, but it has a property accessibility: 'private'.

So this node is not parsed as a private property. Sadly.

I'm a little confused.

bryanrsmith commented 2 years ago

Yes, typescript's private accessibility modifier is a different feature than ES private fields. There's some discussion in this PR, which was closed due to inactivity: https://github.com/bryanrsmith/eslint-plugin-sort-class-members/pull/68

AkatQuas commented 2 years ago

That makes senes.

The private name, which is prefixed with #, would be different from the original one if we really test the accessiblity modifier in Typescript.

I would follow a custom convention to name private property or method prefixed with _ to avoid it.