Repository navigation
Ruby: Add RuboCop support for Ruby in ERB templates - #2260
joelhawksley wants to merge 10 commits into
Conversation
There was a problem hiding this comment.
Thanks for getting this started!
I reserved the rubocop-herb gem name to run the Herb Linter as part of a regular RuboCop run, but I could also see that gem to have tighter RuboCop integration in the future for what you are proposing here.
I think if we'd want to land something like this feature, it probably belongs into rubocop-herb.
I am suggesting the opposite (I think?): we should run RuboCop as part of Herb, not the other way around. I don't know if the semantics matter in this case though.
@marcoroth my mental model of the Herb linter has been that it is meant to replace ERB Lint out of the box, and ERB Lint supports RuboCop as one of its linters: https://github.com/Shopify/erb_lint#linters. I think dropping ERB Lint (which doesn't get much attention these days) is a good value proposition for adopting Herb. I'd like to drop our use of ERB Lint at GitHub, but we use the ERB Lint RuboCop linter, so this is a blocker. Which means I'm motivated to get |
|
Yeah, I think that
This PR is providing the work for 2). So, if you'd want to migrate the mechanics of this pull request into |
|
Alright @marcoroth, I've gotten this working to the point of catching RuboCop errors in |
As part of testing marcoroth/herb#2260, I ran the PR against this repository. `rubocop-herb` found and autofixed RuboCop rule violations in six .html.erb files, using the existing RuboCop rule configuration for this project.
d1d11f6 to
5705225
Compare
Replace the duplicate Ruby source builder with Herb.extract_ruby while retaining range collection for safe autocorrection. Normalize masked multibyte characters to RuboCop's character offsets. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Inline the minimal RuboCop extraction hook around Herb.extract_ruby and remove the standalone extractor, range collector, and autocorrection restriction patches. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Refresh the path gem lock entry for Herb 0.11.0 and align extraction tests with the current Herb binding behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
4472bc2 to
75e2ebe
Compare
Signed-off-by: Joel Hawksley <joel@hawksley.org>
Register every .erb and .herb file for RuboCop extraction, apply template-specific exclusions consistently, and cover non-HTML ERB and Herb files in integration tests. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Register the Herb extractor directly through RuboCop::Runner.ruby_extractors, matching RuboCop's established template plugins. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Marco Roth <marco.roth@intergga.ch> Signed-off-by: Joel Hawksley <joel@hawksley.org>
Remove the single-use ProcessedSourceBuilder class and construct extracted RuboCop sources directly in the plugin while preserving parser and configuration metadata. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Use Herb's version directly, replace Pathname with File.expand_path, keep the extractor as a Method, and streamline source construction naming and calls. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep invalid extracted sources in RuboCop's pipeline so Lint/Syntax can report malformed Ruby, while continuing to skip valid template fragments with no AST. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Moving back to draft while I look at byte vs. character position logic gaps between RuboCop and Herb 鉂わ笍 |
This adds
rubocop-herb, a RuboCop plugin that runs a project鈥檚 configured RuboCop cops against Ruby embedded in.erband.herbtemplates.The plugin uses Herb's
extract_rubybinding to produce position-preserving Ruby for the entire template and passes it through RuboCop鈥檚 nativeruby_extractorsAPI. This preserves control flow and variable usage across ERB tags while mapping offenses and autocorrections back to their original template locations. Existing configuration continues to work, including custom cops, inherited configuration, exclusions, target Ruby versions,--only, and safe or unsafe autocorrection.Cops that depend on physical Ruby file contents or obscured template layout are excluded for template files by default. The integration lives under
gems/rubocop-herband is enabled through the standard RuboCop plugin configuration:This PR implements responsibility (2) from the review discussion. Running Herb鈥檚 own linter rules as part of RuboCop remains separate future work for the
herb-linterintegration.