Class: SlimLint::Filters::EndInserter Private

Inherits:
SlimLint::Filter show all
Defined in:
lib/slim_lint/filters/end_inserter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

In Slim you don’t need to close any blocks:

- if Slim.awesome?
  | But of course it is!

However, the parser is not smart enough (and that’s a good thing) to automatically insert end’s where they are needed. Luckily, this filter does exactly that (and it does it well!)

Constant Summary collapse

IF_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A(if|begin|unless|else|elsif|when|rescue|ensure)\b|\bdo\s*(\|[^|]*\|)?\s*$/
ELSE_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A(else|elsif|when|rescue|ensure)\b/
END_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\Aend\b/

Instance Method Summary collapse

Methods included from SlimLint::Filter::Overrides

#on_escape, #on_html_attr, #on_html_attrs, #on_html_comment, #on_html_condcomment, #on_html_js, #on_html_tag, #on_slim_control, #on_slim_embedded, #on_slim_output, #on_slim_text

Instance Method Details

#on_multi(*exps) ⇒ Sexp

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle multi expression ‘[:multi, *exps]`



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/slim_lint/filters/end_inserter.rb', line 21

def on_multi(*exps)
  @self.clear
  @self.concat(@key)

  # This variable is true if the previous line was
  # (1) a control code and (2) contained indented content.
  prev_indent = false

  exps.each do |exp|
    if control?(exp)
      code_frags = exp[2].last
      statement = code_frags.last.value
      raise(Temple::FilterError, "Explicit end statements are forbidden") if END_RE.match?(statement)

      # Two control code in a row. If this one is *not*
      # an else block, we should close the previous one.
      if prev_indent && statement !~ ELSE_RE
        @self << Sexp.new(:code, "end", start: prev_indent.start, finish: prev_indent.start)
      end

      # Indent if the control code starts a block.
      prev_indent = (statement =~ IF_RE) && exp
    elsif prev_indent
      # This is *not* a control code, so we should close the previous one.
      # Ignores newlines because they will be inserted after each line.
      @self << Sexp.new(:code, "end", start: prev_indent.start, finish: prev_indent.start)
      prev_indent = false
    end

    @self << compile(exp)
  end

  # The last line can be a control code too.
  if prev_indent
    @self << Sexp.new(:code, "end", start: prev_indent.start, finish: prev_indent.start)
  end

  @self
end