deadcode: support references in Slim templates

Abierto
#1,048 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
42/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
ruby
Área
devtools

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Ruby
Estrellas
274
Forks
30
Merge medio
3 h 4 min
PR fusionados (30 d)
8

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Shopify/spoom

Todos los issues de Shopify/spoom

Issues similares

Más issues de Ruby

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.