deadcode: support references in Slim templates
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 42/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Ruby
- Stelle
- 274
- Fork
- 30
- Merge medio
- 3h 4m
- PR unite (30g)
- 8
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Shopify/spoom
-
`spoom srb bump DIRECTORY` bumps files outside DIRECTORYForse già presa @RaphaelFakhri l’ha presa 34 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 65/100
I maintainer di solito rispondono entro 1 giorno
-
srb sigs translate: type parameter shadows a type name used in the same signatureForse già presa @Hashim1999164 l’ha presa 97 giorni fa. Apertabug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 56/100
I maintainer di solito rispondono entro 1 giorno
-
`srb sigs translate` removes comments during translationForse già presa @KaanOzkan l’ha presa 223 giorni fa. Aperta
Shopify/spoom#900 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 45/100
Shopify/spoom#488 · 3 commenti · 3 reazioni ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di Shopify/spoom
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
TarteelAI/quranic-universal-library#1111 ·
I maintainer di solito rispondono entro 1 giorno
-
Generated cask uses deprecated `postflight` stanza; Homebrew 7 warns on every commandForse già presa @k4kratik l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
sachincool/cruft#4 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
riscv/riscv-unified-db#2859 ·
I maintainer di solito rispondono entro 2 giorni
-
agent-driven deployment documentation future releases nice to have
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
changesets
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
openstreetmap/openstreetmap-website#7629 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno