Sha256: 69ecec93c0e9ffcf8fd040b78a80253a0025aa5362350927bd42674bbd869d43

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module SlimLint::Filters
  # A dumbed-down version of {Slim::Controls} which doesn't introduce temporary
  # variables and other cruft (which in the context of extracting Ruby code,
  # results in a lot of weird cops reported by RuboCop).
  class ControlProcessor < Slim::Filter
    BLOCK_RE = /\A(if|unless)\b|\bdo\s*(\|[^|]*\|)?\s*$/

    # Handle control expression `[:slim, :control, code, content]`
    #
    # @param code [String]
    # @param content [Array]
    def on_slim_control(code, content)
      [:multi,
        [:code, code],
        compile(content)]
    end

    # Handle output expression `[:slim, :output, escape, code, content]`
    #
    # @param _escape [Boolean]
    # @param code [String]
    # @param content [Array]
    # @return [Array
    def on_slim_output(_escape, code, content)
      if code[BLOCK_RE]
        [:multi,
          [:code, code, compile(content)],
          [:code, 'end']]
      else
        [:multi, [:dynamic, code], compile(content)]
      end
    end

    # Handle text expression `[:slim, :text, type, content]`
    #
    # @param _type [Symbol]
    # @param content [Array]
    # @return [Array]
    def on_slim_text(_type, content)
      # Ensures :newline expressions from static output are still represented in
      # the final expression
      compile(content)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
slim_lint-0.31.1 lib/slim_lint/filters/control_processor.rb
slim_lint-0.31.0 lib/slim_lint/filters/control_processor.rb
slim_lint-0.30.0 lib/slim_lint/filters/control_processor.rb
slim_lint-0.29.0 lib/slim_lint/filters/control_processor.rb
slim_lint-0.28.0 lib/slim_lint/filters/control_processor.rb
slim_lint-0.27.0 lib/slim_lint/filters/control_processor.rb
slim_lint-0.26.0 lib/slim_lint/filters/control_processor.rb
slim_lint-0.25.0 lib/slim_lint/filters/control_processor.rb
slim_lint-0.24.0 lib/slim_lint/filters/control_processor.rb