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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RulePatcher with module that fix incorrect interfaces #142

Closed
wants to merge 3 commits into from

Conversation

Julien-R44
Copy link
Collaborator

Closes #141

Yo

Here is my attempt at what we were discussing in this issue

So we have the RulePatcher class, which is just a kind of container on which we will register modules. These said modules will be applied at the end of the rule file generation. Each of these modules will have access to the AST of the generated file, and can modify it. Once all the modules have been applied, the source code is generated from this modified ast.

I have implemented a first module that simply converts the mapped types and interface into a union with the different members.
And it works great for the node/file-extension-in-import rule

The only thing that is problematic is the following:
I use @babel/generator to regenerate the source code, and :

Note: The symbols like white spaces or new line characters are not preserved in the AST. When Babel generator prints code from the AST, the output format is not guaranteed.
https://babeljs.io/docs/en/babel-generator

I used the retainLines options which allows having something formatted more or less like before.

But we still end up with ~50 diff files compared to the original rules which have extra spaces in some places.

Example :
image

But it's not a big deal in my opinion.

The other thing I miss is to display the number of patches applied in the terminal report. Everything is ready thanks to the hasPatched property, it's just missing basically the console.log. But now I'm going to bed 馃構

Let me know if anything is wrong

@Julien-R44 Julien-R44 self-assigned this Oct 4, 2022
@Shinigami92
Copy link
Collaborator

Uh I hope I will not hurt you >.<

Your current approach looks interesting and very engineered, but might overshoot a bit 馃槄

I would like to propose a much simpler solution not using AST transform but store diff/patch files in a directory and apply them for generated files.
Generated file content doesn't change very often, so we could e.g. store a file like scripts/generate-rule-files/patches/rules/node/file-extension-in-import.d.ts.patch and apply it when the rule gets (re-)generated.
That way we hopefully can get rid of these babel dependencies.

What do you think? Do I miss anything that your approach does better?

@Julien-R44
Copy link
Collaborator Author

Julien-R44 commented Oct 5, 2022

No problem I'm open to discussion dude !

But I think I totally misunderstood you yesterday. I thought you were talking about an AST-based patching system like me ahah 馃槄

However I still largely favor my solution over a manual patching system. Because, well, the solution you propose involves manual work. And that's what we're trying to avoid I think 馃構 On plugins that are more than 100 rules long, going over each of them to modify this kind of small stuff would be really long and very very boring to do. And even if there are like only 4 rules out of 100 on which you have to apply this, well, you have to go through them all to search

For the moment we haven't detected a lot of patterns that we need to modify manually, but with time, there might be more. Keeping all this in mind to apply them manually on all the needed rules would be really boring to do.

And especially as soon as the metadata of a rule would be updated, we'd have to go over it again.

So yeah, it would be a real pain in the ass to do. And now that we have the code that allows this automation, we might as well use it, right?

The other thing is that this AST system could also be used for #139 and #138. We could remove the empty comments, and also look for common types to optimize them by just making an interface.

@Julien-R44
Copy link
Collaborator Author

And honestly, I find the code quite simple. I don't think it's a problem to maintain it.
This first module that fixes the bad mapped types is about fifteen lines of code, roughly.

What can be overkill is the RulePatcher container. We certainly don't need to register and delete modules at runtime

@Shinigami92
Copy link
Collaborator

Mh, not sure if we are currently thinking in two different directions.
I wont add hundreds of patch files but just as you said 1-4 or so.

#138 should definitely solved by just patching

https://github.com/Shinigami92/eslint-define-config/blob/746fcae51c7cd9ee7a14093956f43db074fd2a13/scripts/generate-rule-files/src/js-doc-builder.ts#L18-L25

So I don't want to patch just blank lines but cases like #141
And if you ever worked with https://www.npmjs.com/package/patch-package you might know that it is not work doing over and over again but more like a one-time write and forget until something got changed by the eslint-plugin itself for that specific rule


If you like to we could discuss in person via voice chat in Discord when we both find time about that 馃檪
Then it could be a bit simpler to explain and talk about drafts

@Shinigami92
Copy link
Collaborator

@Julien-R44 Please rebase and update this PR
Feature is still really welcome and needed

@Shinigami92 Shinigami92 closed this Apr 2, 2023
@Julien-R44 Julien-R44 deleted the feat/rule-patcher branch April 2, 2023 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generating node/file-extension-in-import results in a type error
2 participants