deadcode: support references in Slim templates

Aperta
#1,048 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
ruby
Ambito
devtools

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_file handles .erb specially and treats other files as Ruby.
  • Adding .slim through --allowed-extensions therefore 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

  1. Is compiling Slim to Ruby and reusing the ERB-style indexing path a direction you would accept?
  2. Would a Slim-only first contribution, enabled explicitly through --allowed-extensions, be an appropriate scope?
  3. 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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Shopify/spoom

Tutte le issue di Shopify/spoom

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.