mthadley / eslint-plugin-sort-destructure-keys

ESLint plugin to check if keys should be sorted in an object pattern.
https://www.npmjs.com/package/eslint-plugin-sort-destructure-keys
ISC License
95 stars 9 forks source link

Sort destructure keys by TypeScript type #299

Closed nirtamir2 closed 3 months ago

nirtamir2 commented 3 months ago

Hi I would like the option to use the TypeScript type properties order to sort destructure keys. It should be using @typesccript-eslint

interface Props {
  name: string;
  email: string;
}

export funciton Example(props: Props) {
   const { email, name } = props;
   // ⚠️ - 'name' property should be destructured before 'email'
   // ✅ fix to:  const { name, email } = props;

The option can be called {matchTypeScriptTypePropertiesOrder: true} and we can make it non strict - so for A&B stuff that are not simple we won't force it. It may be a big change so maybe it's better to create a new plugin eslint-plugin-sort-destructure-keys-typescript

nirtamir2 commented 3 months ago

I created a new ESLint plugin for it eslint-plugin-sort-destructure-keys-typescript

gabriel-calin-lazar commented 3 months ago

@nirtamir2 related to eslint-plugin-sort-destructure-keys-typescript - does it supported nested destructuring ? will order of destructured keys be enforced when destructuring multiple levels ? const {a: {b: {d, c}}} = e; should be identified as an issue if the type for nested b property of type a - has properties defined as c and d - in this order

nirtamir2 commented 3 months ago

Hi @gabriel-calin-lazar, Thank you for your comment. At this time, only two days after creating the plugin, this feature is not yet supported. However, it would be a valuable addition, so I opened an issue at https://github.com/nirtamir2/eslint-plugin-sort-destructure-keys-typescript/issues/3.

gabriel-calin-lazar commented 3 months ago

@nirtamir2 is there something similar for the opposite - to enforce ordering of properties when one defines an object literal with a type in place ? to enforce that property ordering in literal definition matches the ordering of the properties from the type or interface. basically a mirror of this eslint plugin - not for destructuring but for object construction

nirtamir2 commented 3 months ago

@gabriel-calin-lazar why would you want to do it? Also, assume that the type can include more properties than the destructuring / you won't have control changing it (for example - type that comes from 3rd party lib).