Sha256: a634fbb00961fd670b0be6c223481623971181fab528fc88246a0b13b61ea6be

Contents?: true

Size: 1.08 KB

Versions: 9

Compression:

Stored size: 1.08 KB

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
    # {Sexp} representing a newline.
    NEWLINE_SEXP = SlimLint::Sexp.new([:newline])

    # Annotates the given {SlimLint::Sexp} with line number information.
    #
    # @param sexp [SlimLint::Sexp]
    # @return [SlimLint::Sexp]
    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

9 entries across 9 versions & 1 rubygems

Version Path
slim_lint-0.8.0 lib/slim_lint/filters/inject_line_numbers.rb
slim_lint-0.7.2 lib/slim_lint/filters/inject_line_numbers.rb
slim_lint-0.7.1 lib/slim_lint/filters/inject_line_numbers.rb
slim_lint-0.7.0 lib/slim_lint/filters/inject_line_numbers.rb
slim_lint-0.6.1 lib/slim_lint/filters/inject_line_numbers.rb
slim_lint-0.6.0 lib/slim_lint/filters/inject_line_numbers.rb
slim_lint-0.5.0 lib/slim_lint/filters/inject_line_numbers.rb
slim_lint-0.4.0 lib/slim_lint/filters/inject_line_numbers.rb
slim_lint-0.3.0 lib/slim_lint/filters/inject_line_numbers.rb