-
Notifications
You must be signed in to change notification settings - Fork 128
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
Added autofix function for imports-on-top rule + Tests for this feature #156
Conversation
Thanks @GabrielAlacchi, currently reviewing this PR. |
@GabrielAlacchi Don't worry about cursor issues. If you supply a fix and Solium ensures that this fix doesn't conflict with any other rule fixes, then the fix is guaranteed to be applied just the way you intended. (Correct me if I'm wrong below)
^ Given all this, I see the end result you created for the user - cleanly organised import statements. I'd like to suggest a shorter fix - just follow the comments I put, ie, to
But I can be wrong, so I'm still open to hearing you in case you feel your approach is better |
@duaraghav8 I agree with you on the fact that relying on other iterations of the rule is a bad idea in general. My original idea was to just insert the import below the last valid import node, or else below the I think your solution is fine for temporarily solving this problem. One issue I can see is that the fix may conflict with the blank-lines rule if it's not implemented carefully, consider this case:
The rule will see that when it reaches the node corresponding to
However this contradicts the rule that contracts should be preceded by 2 blank lines, and thus the autofix would create code that fails the linting process. This can be solved by including some extra cases when inserting the import statement before |
I fully agree with the modified approach you've described above (could you please make the necessary changes in PR?) I'll make the changes so Solium so it runs multiple iterations over a file instead of single. So in first iteration, import statements are fixed as intended by you, but they cause a conflict and solium reports it. But in the next iter. Solium will fix the conflict arising from the first iter. (once the Bottom line: you need not worry about conflicts your rule's fix might create with another rule's checks. That's Solium's problem On a side note, please keep in mind that there's also an experimental pragma statement (eg- |
I've implemented the insert after last valid node strategy that we've agreed on in my latest commit, and have modified the unit tests to reflect this change. I also simplified the loop over
|
Thanks @GabrielAlacchi 🙌 |
Cheers @duaraghav8 and Happy New Year to you too! I have pushed some commits for autofixing the blank-lines rule on the dev branch of my fork Should I open a PR or wait to complete some of the others first? |
great! Yes, please open a PR. Its easier if we only have 1 thing to focus on in 1 PR |
This is in regards to issue #94. There are still many other autofix functions left to write. I'm more than willing to help with them in the coming days after my exams are finished.
My implementation does the following:
When the linter encounters the first invalid import statement, it finds all proceeding import statements and combines them into one block. Then inserts that in one of three places:
pragma solidity ^0.4.0;
statement.Afterwards, any other import statement is just simply removed.
I had to combine everything and add it to the top of the file in one step so that there was no issue with two fixes overlapping. However one thing that concerns me is the case where another rule has a fix that sets the cursor beyond the top of the file. Then what will happen is the copying of import statements to the top of the file will not be run, but every import statement that follows will still be removed, thus completely defacing the user's code. Maybe I've misunderstood the fixing engine, but if you think that this is a valid concern then I can develop a use case that will execute this bug and open an issue surrounding it.