Linter: Implement erb-no-trailing-whitespace rule#1168
Linter: Implement erb-no-trailing-whitespace rule#1168marcoroth merged 4 commits intomarcoroth:mainfrom
erb-no-trailing-whitespace rule#1168Conversation
There was a problem hiding this comment.
Hey @markokajzer, thanks for taking a stab at this!
This looks great already and is a really accurate port of the linter rule as it was at play in erb_lint.
However, I have a slight concern with autofix for this rule. I feel like we need to be a bit more careful and cannot just strip whitespace at the end of each line, as some of the whitespace might be relevant.
So I feel like we might have to change this linter rule to be a ParserRule (opposed to the SourceRule we have now) and skip contexts like <pre> and <textarea> . Maybe even <script> and <style> too.
But I also realize that the implementation of this would become a lot trickier when we have to operate on AST nodes for a rule like this.
Do you have any thoughts on this? Thank you!
javascript/packages/linter/docs/rules/erb-trailing-whitespace.md
Outdated
Show resolved
Hide resolved
erb-trailing-whitespace ruleerb-trailing-whitespace rule
|
I actually fear that might exceed my skills lol How would be go about this? Traverse all nodes, and if we're at the end of the line, and not within one of the mentioned nodes, delete the whitespace? (Haven't looked at On the other hand, does |
erb-trailing-whitespace ruleerb-no-trailing-whitespace rule
Yeah, sadly that's the super tricky part here. When we are operating on AST Nodes we don't really think in terms of lines anymore. But, the AST Nodes would have all required information on them to be able to figure that out again and map it back. It probably would just mean a lot of new rule helpers to make it easier to work with lines in a That said, you are right that this is already a problem in What I would recommend is to tell the rule that it's not auto-correctable set |
lower severity to warning in the same vein? |
|
Hey @markokajzer, sorry for pushing up directly to your branch. I addressed the concerns by converting the rule from a The detection now works in multiple steps: First, we split the source into lines and scan each line for trailing whitespace using a regex, this is pretty much what we had before, but this gives us a list of trailing whitespace candidates with their line and column positions. Next, we traverse the AST using a visitor once to collect the "skip zones". They are regions where trailing whitespace should be preserved. This includes the Finally, we filter the candidates against the skip zones. Only candidates that fall outside all skip zones become offenses. This approach gives us the simplicity of the source-based detection we had before, but with the accurate context-awareness of AST-based filtering. Since we now skip sensitive contexts, autofix is safe to enable again. I added some more tests to make sure we cover all of the tricky edge cases. Thanks for starting this and opening a pull request! 🙏🏼 |
|
Glad we’ll be able to merge something, even if you had to do all the work after all :^) I see you have a high standard for this project haha Might try again on another rule 👍 |
closes #547
Implements the
TrailingWhitespacerule from ERBLint.This rule disallows whitespace (spaces, tabs, carriage return) at the end of a line.
Unfamiliar with the project, I took inspiration from #557, which implemented a similar rule.