Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): sort members alphabetically #263

Merged

Conversation

timkraut
Copy link
Contributor

@timkraut timkraut commented Feb 12, 2019

Hi,

this PR adds a new rule to sort members in interfaces alphabetically. A use case for such a rule would be the following:

Use case

Sorting things can have certain benefits, e.g. fewer merge conflicts or easier manual comparison of implementations.

Currently, in ESLint, multiple things can be sorted alphabetically:

Additionally, it is possible to sort members using member-ordering. Except if I didn't understand how to use the member-ordering rule correctly, currently, there is no option to sort members of interfaces alphabetically.

Let's say we have an interface which looks as follows:

interface Foo {
  B: string; // should come after `A`
  A: string;
  C: string;
}

The intended definition of this interface is:

interface Foo {
  A: string;
  B: string;
  C: string;
}

This PR adds a rule which detects these issues. This is probably something where we could also provide a fix. I just wanted to make sure this goes into the right direction before. Looks like there is an open issue to create a rule for this in TSLint: Feature Request: alphabetize interface members/fields. Let me know what you think!

@timkraut

This comment has been minimized.

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if you have this rule plus member ordering turned on?
They'll conflict, won't they?
I don't think we can put the plugin into a state where you can have two rules which can directly conflict. (I don't think we have that yet do we?)

It may be better to get this as an option in member-ordering.
That rule might need a revisit because it's pretty messy...

worth a discussion @typescript-eslint/core-team

packages/eslint-plugin/src/rules/sort-interface-members.ts Outdated Show resolved Hide resolved
@timkraut

This comment has been minimized.

@j-f1

This comment has been minimized.

@bradzacher bradzacher added the enhancement: new plugin rule New rule request for eslint-plugin label Feb 14, 2019
@timkraut

This comment has been minimized.

@bradzacher

This comment has been minimized.

@timkraut
Copy link
Contributor Author

timkraut commented Mar 5, 2019

Thanks a lot for your ideas about how this might look like and your feedback for the code changes. This helped a lot!

I'll start with updating the documentation of the rule to be more clear how this all works and then I'll try to add the options to sort all members regardless of their type alphabetically or to sort them alphabetically inside of their groups.

@timkraut

This comment has been minimized.

@timkraut timkraut force-pushed the sort-interface-properties branch 2 times, most recently from bd124fb to f35ce10 Compare March 13, 2019 10:41
@timkraut

This comment has been minimized.

@timkraut

This comment has been minimized.

@JamesHenry JamesHenry added the awaiting response Issues waiting for a reply from the OP or another party label Apr 4, 2019
@bradzacher

This comment has been minimized.

@polomani

This comment has been minimized.

@timkraut

This comment has been minimized.

@drewlustro

This comment has been minimized.

@WaldoJeffers

This comment has been minimized.

kaicataldo pushed a commit to kaicataldo/typescript-eslint that referenced this pull request Aug 27, 2019
@timkraut timkraut force-pushed the sort-interface-properties branch 3 times, most recently from 75788eb to d6d972c Compare September 17, 2019 13:57
@timkraut timkraut force-pushed the sort-interface-properties branch 2 times, most recently from 55f583a to bef3d83 Compare September 18, 2019 10:43
@bradzacher bradzacher removed the help wanted Extra attention is needed label Feb 24, 2020
@Fuzzyma
Copy link

Fuzzyma commented Feb 24, 2020

@timkraut does a default value keeps me from writing a long list of types? Then yes! Often people dont have any particular order. They just want a consistent order. A default value can do that without any configuration overhead. Alternatively you can accept a string default instead of the array so the user has a choice

@timkraut
Copy link
Contributor Author

@Fuzzyma Yes. The default config is the following:

export const defaultOrder = [
  // Index signature
  'signature',

  // Fields
  'public-static-field',
  'protected-static-field',
  'private-static-field',

  'public-instance-field',
  'protected-instance-field',
  'private-instance-field',

  'public-abstract-field',
  'protected-abstract-field',
  'private-abstract-field',

  'public-field',
  'protected-field',
  'private-field',

  'static-field',
  'instance-field',
  'abstract-field',

  'field',

  // Constructors
  'public-constructor',
  'protected-constructor',
  'private-constructor',

  'constructor',

  // Methods
  'public-static-method',
  'protected-static-method',
  'private-static-method',

  'public-instance-method',
  'protected-instance-method',
  'private-instance-method',

  'public-abstract-method',
  'protected-abstract-method',
  'private-abstract-method',

  'public-method',
  'protected-method',
  'private-method',

  'static-method',
  'instance-method',
  'abstract-method',

  'method',
];

@Fuzzyma
Copy link

Fuzzyma commented Feb 25, 2020

I am all for it! Did I already say thank you that you continued working on this? :)

@codecov
Copy link

codecov bot commented Mar 20, 2020

Codecov Report

Merging #263 into master will increase coverage by 0.09%.
The diff coverage is 99.00%.

@@            Coverage Diff             @@
##           master     #263      +/-   ##
==========================================
+ Coverage   94.64%   94.73%   +0.09%     
==========================================
  Files         160      160              
  Lines        7265     7300      +35     
  Branches     2082     2092      +10     
==========================================
+ Hits         6876     6916      +40     
+ Misses        167      164       -3     
+ Partials      222      220       -2     
Flag Coverage Δ
#unittest 94.73% <99.00%> (+0.09%) ⬆️
Impacted Files Coverage Δ
...ackages/eslint-plugin/src/rules/member-ordering.ts 99.14% <99.00%> (+6.46%) ⬆️

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nit picks, but otherwise LGTM.
Thanks for seeing this through to the end.

packages/eslint-plugin/docs/rules/member-ordering.md Outdated Show resolved Hide resolved
packages/eslint-plugin/docs/rules/member-ordering.md Outdated Show resolved Hide resolved
@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Mar 23, 2020
@timkraut
Copy link
Contributor Author

timkraut commented Mar 31, 2020

@bradzacher Thanks for going through all the code again! I've fixed the docs issues and rebased this branch against the current master

@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Mar 31, 2020
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks!

@timkraut
Copy link
Contributor Author

timkraut commented Apr 3, 2020

Thanks for the helpful reviews!

@escape0707
Copy link

Hi! I would I like to know if there is a way to apply the rule like this:

"rules": {
  "@typescript-eslint/member-ordering": [
    "warn",
    {
      "default": { "memberTypes": "defaultOrder", "order": "alphabetically" }
    }
  ]
}

@bradzacher
Copy link
Member

@escape0707
Copy link

escape0707 commented Apr 20, 2020

@bradzacher Thanks for your reply, but I mean I checked and followed the doc but I still don't understand what's the <Default Order> part means. I think it's not a valid json notation, right?

Do I have to repeat the default orders again here?

@timkraut
Copy link
Contributor Author

@escape0707 Yes, exactly. There is no magic string "use-default-order" or something like this. If you want to have the default order as written in the docs (this is copied directly from the source code btw.), you have to specify this manually (see https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/member-ordering.md#default-configuration for the default order)

@escape0707
Copy link

escape0707 commented Apr 20, 2020 via email

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants