Sha256: b6a67f4dda3887d8176abd946681edd9d2247fca0015eaebddb694c03507869b

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'pathname'

module RVNC
  class SourceFile
    def initialize(path)
      @path = path
      @relative_path = Pathname.new(path).relative_path_from(Pathname.new(Dir.pwd))
      @source = File.readlines(path)
      @variables = []
    end

    def variables
      ast = RubyVM::AST.parse(@source.join)
      traverse(ast)
      @variables.flatten
    end

    private

    def traverse(parent)
      if RVNC::VARIABLE_NODES.include?(parent.type)
        extract_variables(parent)
      end

      parent.children.compact.each do |child|
        traverse(child)
      end
    end

    def extract_variables(node)
      first_lineno = node.first_lineno - 1
      first_column = node.first_column
      last_lineno = node.last_lineno - 1
      last_column = node.last_column

      if first_lineno == last_lineno
        src = @source[first_lineno][first_column..last_column]
      else
        src = @source[first_lineno][first_column..]
        ((first_lineno + 1)...(last_lineno)).each do |lineno|
          src << @source[lineno]
        end
        src << @source[last_lineno][0..last_column]
      end

      return unless src.include?('=')

      location = [@relative_path, node.first_lineno].join(':')
      vars = src
               .partition('=')
               .first
               .split(',')
               .map { |v| v.delete('*+-/%|&') }
               .map(&:strip)
               .map { |v| { name: v, location: location } }
      @variables << vars
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rvnc-0.0.1 lib/rvnc/source_file.rb