Skip to content

Prism: Do not scan code inside ERB comments - #759

Closed
anyingiit wants to merge 1 commit into
glebm:mainfrom
anyingiit:fix-erb-comment-prism
Closed

anyingiit wants to merge 1 commit into
glebm:mainfrom
anyingiit:fix-erb-comment-prism

Conversation

@anyingiit

Copy link
Copy Markdown
Contributor

Description

The Prism-based ERB scanner (ErbAstScanner#process_comments) parsed the body of an ERB comment (<%# ... %>) as Ruby code, only rewriting i18n-tasks-use into #i18n-tasks-use . As a result, any commented-out code inside an ERB comment was treated as live code, e.g.

<%# ModelClass.human_attribute_name(:unknown) %>
<%# t("some.key") %>

was reported as using activerecord.attributes.model_class.unknown / some.key (and therefore as missing keys). The Parser-based scanner already treats ERB comments as comments and only reads magic comments from them.

This change turns every line of an ERB comment into a Ruby comment (stripping leading whitespace/#), so Prism only sees comments and the existing magic-comment handling picks up i18n-tasks-use lines (single-line, multi-line and <%#- forms keep working), while commented-out code is ignored.

The comments.html.erb fixture gets a few commented-out calls (t(...), human_attribute_name, multi-line); the Prism spec asserts they are not reported. The same fixture is used by the Parser ERB spec, which already passes with it.

Motivation / disclosure: I had some spare AI-assistant quota (Claude Code) and am using it to try to
help projects with open good-first-issues. The change was prepared with Claude Code and verified as listed
below. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it — please feel free to
close it, no hard feelings at all 🙂

Related issue

Closes #746

Checklist

  • Tests pass locally (bundle exec rake with RUBYOPT="--enable-frozen-string-literal --debug-frozen-string-literal" on Ruby 3.4.9: 286 examples, 0 failures, 5 pending — Google Translate specs, no API key). New assertions fail without the fix (11 used keys instead of 8). bundle exec rubocop: no new offenses (the 5 Lint/InterpolationCheck offenses in spec/used_keys_erb_spec.rb also exist on main).
  • CHANGES.md is updated (if applicable) — entry under "Unreleased"
  • Documentation is updated (if applicable) — n/a, behaviour now matches the documented magic-comment usage

The Prism ERB scanner parsed the body of `<%# ... %>` comments as Ruby,
only turning `i18n-tasks-use` into a comment. Any commented-out code such
as `<%# User.human_attribute_name(:foo) %>` or `<%# t("key") %>` was thus
reported as a used key. Turn every line of an ERB comment into a Ruby
comment so that only the `i18n-tasks-use` magic comments are picked up,
matching the Parser-based scanner.

Fixes glebm#746

@davidwessman davidwessman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It looks good, please fix the conflicts and the rubocop errors. I will try to fix the Simplecov warnings on main.

@davidwessman

Copy link
Copy Markdown
Collaborator

@copilot resolve the merge conflicts on this branch.

@davidwessman

Copy link
Copy Markdown
Collaborator

Merged in #763

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.

Prism reports offenses in comments

2 participants