deadcode: support references in Slim templates
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
Research direction
Start by reading the issue’s description of Deadcode::Index#index_file and its existing ERB handling, then trace how allowed extensions select files for analysis. The proposed path is to compile Slim templates to Ruby and reuse the reference indexing pipeline; the issue’s experiment gives examples to investigate and flags line mapping, dependency handling, and wrapper suppression. Done means references in Slim keep used methods or constants alive while unused definitions remain candidates, with tests covering supported cases and errors.
Written by the indexing model from the issue text.
Description
Problem
References in Slim templates are currently outside the supported template analysis path. This can cause definitions used only from those templates to be reported as dead code candidates.
I reproduced this with Ruby 3.4.3, Spoom 1.8.13, and Slim 5.2.2:
- The default extensions are
.rb,.erb, and.gemspec. Deadcode::Index#index_filehandles.erbspecially and treats other files as Ruby.- Adding
.slimthrough--allowed-extensionstherefore does not provide Slim parsing support.
Minimal reproduction
In an otherwise empty directory, use this Gemfile:
source "https://rubygems.org"
gem "spoom", "1.8.13"
gem "slim", "5.2.2"
Create presenter.rb:
class ReportPresenter
def display_label
"Report"
end
def unused_label
"Unused"
end
end
Create view.html.slim:
= ReportPresenter.new.display_label
Run:
bundle install
bundle exec spoom deadcode --no-color --show-files
bundle exec spoom deadcode --no-color --show-files --allowed-extensions .rb .erb .gemspec .slim
The default invocation collects only presenter.rb and reports both methods as dead candidates. With .slim explicitly enabled, both files are collected, but the CLI reports:
Error: Error parsing view.html.slim: Error while parsing view.html.slim:
- unexpected '=', ignoring it (at 1:0)
It continues analysis and still reports both methods:
Candidates:
ReportPresenter::display_label presenter.rb:2:2-4:5
ReportPresenter::unused_label presenter.rb:6:2-8:5
Found 2 dead candidates
Expected: display_label should be kept alive by the Slim reference, while unused_label should remain a candidate.
As controls, I ran the same method call from a Ruby file and from an ERB template in separate runs. Each reported only unused_label. All four runs exited with status 1, because the deliberately unused method remains in every case.
Rails verification
I also reproduced this in a small Rails 8.1.4.1 application with slim-rails 4.0.1, using the same Spoom, Slim, and Ruby versions.
The helper was:
module ReportsHelper
def display_label
"Slim reference works"
end
def unused_label
"Unused"
end
end
The Slim view was:
h1
= display_label
The ERB control was <h1><%= display_label %></h1>. Both routes returned HTTP 200 and rendered <h1>Slim reference works</h1> through the Rails application using Rack::MockRequest.
I then analyzed app/helpers and one view at a time so that the ERB reference could not mask the missing Slim reference:
bundle exec spoom deadcode app/helpers app/views/reports/erb.html.erb --no-color --show-files --show-plugins
bundle exec spoom deadcode app/helpers app/views/reports/slim.html.slim --no-color --show-files --show-plugins
bundle exec spoom deadcode app/helpers app/views/reports/slim.html.slim --no-color --show-files --show-plugins --allowed-extensions .rb .erb .gemspec .slim
| View analyzed | Result |
|---|---|
| ERB | Only unused_label reported |
| Slim, default extensions | View not collected; both methods reported |
| Slim, explicit extension | View collected but parse error at 2:2; both methods reported |
Spoom::Deadcode::Plugins::Rails was loaded in all three runs. This application was created for reproduction; these are not results from an existing production project.
Proposed direction
Would you be open to Slim support that follows the existing ERB approach: compile the template to Ruby, wrap it as needed for parsing, and reuse the existing reference indexing pipeline?
I would start with Slim, focusing on references to constants and methods. Haml could be considered separately afterward.
Compilation experiment
In a separate experiment, without modifying Spoom, I generated Ruby using Slim::Engine.new.call(template), wrapped it in a synthetic method, and passed it to Index#index_ruby.
With Slim 5.2.2 and its default options, the existing indexing pipeline found the expected constant/method references in six examples covering output, a silent-code loop, interpolation, a dynamic attribute, a splat attribute, and a nested conditional. A seventh example confirmed that a Slim comment did not introduce those references. The used method was kept alive in the six positive cases and the unused control remained a candidate in all seven.
Reference line numbers matched the template lines in these examples after subtracting the single wrapper line. This is limited evidence for the approach, not a guarantee for all Slim constructs or compiler versions. Column mapping, embedded engines, custom Slim options, and broader version compatibility still need investigation. The experiment also does not implement file dispatch, dependency/error handling, or suppression of the synthetic wrapper definition.
Questions
- Is compiling Slim to Ruby and reusing the ERB-style indexing path a direction you would accept?
- Would a Slim-only first contribution, enabled explicitly through
--allowed-extensions, be an appropriate scope? - Would you prefer Slim to be an optional dependency loaded only when needed? If a Slim file is selected but the gem is unavailable, should analysis fail or report the skipped file and continue? Skipping it could affect the dead code candidates because its references would be missing.
I would be happy to investigate and contribute tests for output expressions, control flow, attributes, interpolation, and invalid templates once the direction is agreed upon.
- Dominant language
- Ruby
- Stars
- 274
- Forks
- 30
- Avg merge
- 3h 4m
- Merged PRs (30d)
- 8
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Shopify/spoom
-
`spoom srb bump DIRECTORY` bumps files outside DIRECTORYPossibly taken @RaphaelFakhri claimed this 34 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
srb sigs translate: type parameter shadows a type name used in the same signaturePossibly taken @Hashim1999164 claimed this 97 days ago. Openbug
Difficulty 4/5 3-5 days Newbie friendliness 56/100
Maintainers usually reply within 1 day
-
`srb sigs translate` removes comments during translationPossibly taken @KaanOzkan claimed this 223 days ago. Open
Shopify/spoom#900 · 1 assignee ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
Shopify/spoom#488 · 3 comments · 3 reactions ·
Maintainers usually reply within 1 day
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
TarteelAI/quranic-universal-library#1111 ·
Maintainers usually reply within 1 day
-
Generated cask uses deprecated `postflight` stanza; Homebrew 7 warns on every commandPossibly taken @k4kratik claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
sachincool/cruft#4 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
riscv/riscv-unified-db#2859 ·
Maintainers usually reply within 2 days
-
agent-driven deployment documentation future releases nice to have
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
changesets
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
openstreetmap/openstreetmap-website#7629 · 1 comment ·
Maintainers usually reply within 1 day