Class: SlimLint::RubyExtractor

Inherits:
Object
  • Object
show all
Extended by:
SexpVisitor::DSL
Includes:
SexpVisitor
Defined in:
lib/slim_lint/ruby_extractor.rb

Overview

Utility class for extracting Ruby script from a Slim template that can then be linted with a Ruby linter (i.e. is “legal” Ruby).

The goal is to turn this:

- if items.any?
  table#items
  - for item in items
    tr
      td.name = item.name
      td.price = item.price
- else
   p No items found.

into (something like) this:

if items.any?
  for item in items
    puts item.name
    puts item.price
else
  puts 'No items found'
end

The translation won’t be perfect, and won’t make any real sense, but the relationship between variable declarations/uses and the flow control graph will remain intact.

Defined Under Namespace

Classes: RubySource

Instance Attribute Summary

Attributes included from SexpVisitor::DSL

#captures, #patterns

Instance Method Summary collapse

Methods included from SexpVisitor::DSL

anything, capture, on, on_start

Methods included from SexpVisitor

#captures, #on_start, #patterns, #traverse, #traverse_children, #trigger_pattern_callbacks

Instance Method Details

#extract(sexp) ⇒ SlimLint::RubyExtractor::RubySource

Extracts Ruby code from Sexp representing a Slim document.

Parameters:

Returns:



47
48
49
50
# File 'lib/slim_lint/ruby_extractor.rb', line 47

def extract(sexp)
  trigger_pattern_callbacks(sexp)
  RubySource.new(@source_lines.join("\n"), @source_map)
end