Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

feat(select-style): add fixer #189

Merged

Conversation

rafaelss95
Copy link
Collaborator

Part of #179.

@rafaelss95 rafaelss95 changed the title feat: add fixer for select-style feat(select-style): add fixer Sep 4, 2021
Copy link
Owner

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

This is great, and I'm sure that it will help some codebases!
I'm going to take a look at the implementation later, but I already have a question.

tests/rules/select-style.test.ts Outdated Show resolved Hide resolved
@rafaelss95 rafaelss95 force-pushed the feat/select-style-fixer branch 4 times, most recently from e2b4fef to c5683f4 Compare September 7, 2021 01:06
@rafaelss95 rafaelss95 force-pushed the feat/select-style-fixer branch from c5683f4 to 564c428 Compare September 7, 2021 01:08
@@ -15,25 +15,25 @@ ruleTester().run(path.parse(__filename).name, rule, {
import { Store } from '@ngrx/store'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Something that came to my mind... since we have some rules relying on Store import, wouldn't it be a good idea to have a rule like use-consistent-global-import or something like this? Because if I do import { Store as ngrxStore } from '@ngrx/store' these rules won't work as expected...

Copy link
Owner

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

Looks good to me!
I'm so excited about this fix 🚀
About your question, I think it's good to keep in mind, but I also think we won't get a lot of benefit from it. Maybe we should make the store lookup mechanism smarter, so it looks for the renamed node, e.g. ngrxStore instead of Store?

@timdeschryver timdeschryver merged commit dc1ff4a into timdeschryver:main Sep 7, 2021
@github-actions
Copy link

github-actions bot commented Sep 7, 2021

🎉 This PR is included in version 1.30.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@rafaelss95 rafaelss95 deleted the feat/select-style-fixer branch September 7, 2021 14:36
@rafaelss95
Copy link
Collaborator Author

rafaelss95 commented Sep 7, 2021

About your question, I think it's good to keep in mind, but I also think we won't get a lot of benefit from it. Maybe we should make the store lookup mechanism smarter, so it looks for the renamed node, e.g. ngrxStore instead of Store?

Oh, I just noticed that we already handle these cases properly in rules that rely on findNgRxStoreName here:

importClause.imported.name === importedName

Using imported.name is sufficient to cover this.


The only ones that are missing are those that depend on the typeName:

export const injectedStore = `MethodDefinition[kind='constructor'] Identifier[typeAnnotation.typeAnnotation.typeName.name='Store']`
export const typedStore = `MethodDefinition[kind='constructor'] Identifier>TSTypeAnnotation>TSTypeReference[typeName.name='Store'][typeParameters.params]`

But yes, I don't think it's a valid requirement to the point of having a rule just for these few cases (three rules more specifically).

@timdeschryver
Copy link
Owner

@rafaelss95 this works like a charm!
One nit, would it be possible to remove the select import if we switch from the operator syntax to the method syntax?

For example:

// before
import { select, Store } from '@ngrx/store';
this.searchQuery$ = store.pipe(select(fromBooks.selectSearchQuery));

// becomes (notice that `select` is still imported).
import { select, Store } from '@ngrx/store';
this.searchQuery$ = store.select(fromBooks.selectSearchQuery);

I would've expected that the unused import also had a fixer, but it seems like it does not have one - thus after this fix we end up with a new warning 😅

@rafaelss95
Copy link
Collaborator Author

rafaelss95 commented Sep 7, 2021

Hmm, this is tricky as there is no way to actually know if there are other occurrences of select. Coincidentally, I talked about this with James not long ago here angular-eslint/angular-eslint#606 (comment). In this specific case, which had the intention to remove the imports from lifecycle interfaces, he suggested that we do a string check and then remove if there was only one occurrence (the import) and so we did... however, for this case here I don't know if this approach would be viable, since a search for 'select' would still return several occurrences, as it only changed the position/way it is called and we can't really trust on something like .select or (select) because of formatting 😞.

I just sent a PR hoping to accomplish this 🙏

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants