Sha256: 069ce348bf63e10aadc89d6fd6f862c53164c89aed280bdae372e4baf7c302bb

Contents?: true

Size: 901 Bytes

Versions: 2

Compression:

Stored size: 901 Bytes

Contents

module SlimLint::Filters
  # Traverses a Temple S-expression (that has already been converted to
  # {SlimLint::Sexp} instances) and annotates them with line numbers.
  #
  # This is a hack that allows us to access line information directly from the
  # S-expressions, which makes a lot of other tasks easier.
  class InjectLineNumbers < Temple::Filter
    NEWLINE_SEXP = [:newline]

    def call(sexp)
      @line_number = 1
      traverse(sexp)
      sexp
    end

    private

    # Traverses an {Sexp}, annotating it with line numbers by searching for
    # :newline abstractions within it.
    #
    # @param sexp [SlimLint::Sexp]
    def traverse(sexp)
      sexp.line = @line_number

      if sexp == NEWLINE_SEXP
        @line_number += 1
        return
      end

      sexp.each do |nested_sexp|
        traverse(nested_sexp) if nested_sexp.is_a?(SlimLint::Sexp)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slim_lint-0.2.0 lib/slim_lint/filters/inject_line_numbers.rb
slim_lint-0.1.0 lib/slim_lint/filters/inject_line_numbers.rb