Sha256: ac25452101ac4d14b1056858cbf5c2bda9998411fc6553797f2d22200e564a59

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require 'parser'

module ThemeCheck
  class ThemeFileRewriter
    def initialize(name, source)
      @buffer = Parser::Source::Buffer.new(name, source: source)
      @rewriter = Parser::Source::TreeRewriter.new(
        @buffer
      )
    end

    def insert_before(node, content)
      @rewriter.insert_before(
        range(node.start_index, node.end_index),
        content
      )
    end

    def insert_after(node, content)
      @rewriter.insert_after(
        range(node.start_index, node.end_index),
        content
      )
    end

    def replace(node, content)
      @rewriter.replace(
        range(node.start_index, node.end_index),
        content
      )
    end

    def wrap(node, insert_before, insert_after)
      @rewriter.wrap(
        range(node.start_index, node.end_index),
        insert_before,
        insert_after,
      )
    end

    def to_s
      @rewriter.process
    end

    private

    def range(start_index, end_index)
      Parser::Source::Range.new(
        @buffer,
        start_index,
        end_index,
      )
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
theme-check-1.7.2 lib/theme_check/theme_file_rewriter.rb
theme-check-1.7.1 lib/theme_check/theme_file_rewriter.rb
theme-check-1.7.0 lib/theme_check/theme_file_rewriter.rb
theme-check-1.6.2 lib/theme_check/theme_file_rewriter.rb