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

Improving/replacing sort-ngmodule-metadata-arrays #1232

Closed
JamesHenry opened this issue Nov 24, 2022 · 6 comments · Fixed by #1781
Closed

Improving/replacing sort-ngmodule-metadata-arrays #1232

JamesHenry opened this issue Nov 24, 2022 · 6 comments · Fixed by #1781
Labels
enhancement New feature or request package: eslint-plugin Angular-specific TypeScript rules PRs Welcome If a high-quality PR is created for this it will be accepted

Comments

@JamesHenry
Copy link
Member

JamesHenry commented Nov 24, 2022

In light of the number of issues reported with this rule I wanted to bring them together in one place in order to inform a greatly improved implementation of sorting imports metadata.

In light of this last point, the new rule instead needs to focus on extracting the relevant chunks of a file manually (not using granular AST selectors) and fixing the final form in one action.

This is the approach taken by this import sorting plugin for example: https://github.com/lydell/eslint-plugin-simple-import-sort/blob/main/src/imports.js


*The new rule will be called @angular-eslint/sort-imports-metadata

@JamesHenry JamesHenry added enhancement New feature or request package: eslint-plugin Angular-specific TypeScript rules labels Nov 24, 2022
@JamesHenry JamesHenry pinned this issue Nov 24, 2022
@JamesHenry JamesHenry changed the title Improving @angular-eslint/sort-ngmodule-metadata-arrays Improving/replacing @angular-eslint/sort-ngmodule-metadata-arrays Nov 24, 2022
@JamesHenry JamesHenry changed the title Improving/replacing @angular-eslint/sort-ngmodule-metadata-arrays Improving/replacing sort-ngmodule-metadata-arrays Nov 24, 2022
@JamesHenry JamesHenry added the PRs Welcome If a high-quality PR is created for this it will be accepted label Dec 3, 2022
@abaran30
Copy link
Contributor

FYI @JamesHenry I started looking into solutions for this issue. I have a draft PR up here, though it only contains boilerplate code for the new rule at the moment. Will continue to play around with it when I can.

@rubiesonthesky
Copy link
Contributor

I have also played with this and have some sort of working version. I'll try to check that PR later if I could have some suggestions etc. :)

I have opionated default groups for example :D

@Nosfistis
Copy link

Any news on this rule?

@abaran30
Copy link
Contributor

I closed my draft PR (can always refer back to it whenever necessary) that attempted to resolve this issue because we discovered that there are cases where the order of imports matters.

One case (as I commented in the draft PR) is when simulating a data server using the In-memory Web API. The docs explicitly state, "Always import the HttpClientInMemoryWebApiModule after the HttpClientModule to ensure that the in-memory backend provider supersedes the Angular version." Sorting the imports would break this.

Another case is importing routing modules. There's a snippet in the Angular docs for the Router tutorial here that states, "The order of route configuration is important because the router accepts the first route that matches a navigation request path." With the Router tutorial example, if AppRoutingModule does not get imported after HeroesModules, routing breaks; reproduced in the live example.

There could be other cases where the order of imports matters. It could be possible to use rule properties to work around these cases. However, even in the case where order does not matter, sorted imports is a formatting preference as described in this comment, so where to draw the line?

Happy to discuss.

@Nosfistis
Copy link

Could sorting be ignored if there is an inline ignore rule?

@abaran30
Copy link
Contributor

Could sorting be ignored if there is an inline ignore rule?

Yes and no... If I'm understanding correctly (anyone, please correct me if I'm wrong 🙂), ignore rule comments will prevent the reporting of warnings/errors, and will not prevent rule logic from processing code to evaluate. It could be possible to add logic to a rule to do something like, "if this import has an inline ignore comment, exclude it from sorting." However (personal opinion), I don't think rules should overload the purpose of eslint-disable comments. One could consider a new commenting system specific to a rule, but I would be curious to learn about the use case(s) that would warrant such complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request package: eslint-plugin Angular-specific TypeScript rules PRs Welcome If a high-quality PR is created for this it will be accepted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants