Sha256: 98f420adf29dac35ca21a9b7eb70c3a17d3dd939a48779b1493918a7c1199baa

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module ActionviewPrecompiler
  module ASTParser
    class Node
      def self.wrap(node)
        if RubyVM::AbstractSyntaxTree::Node === node
          new(node)
        else
          node
        end
      end

      def initialize(node)
        @node = node
      end

      def children
        @children ||= @node.children.map do |child|
          self.class.wrap(child)
        end
      end

      def inspect
        "#<#{self.class} #{@node.inspect}>"
      end

      def argument_nodes
        children[1].children[0...-1]
      end

      def array?
        type == :ARRAY
      end

      def fcall?
        type == :FCALL
      end

      def hash?
        type == :HASH
      end

      def string?
        type == :STR && String === children[0]
      end

      def symbol?
        type == :LIT && Symbol === children[0]
      end

      def to_hash
        children[0].children[0..-2].each_slice(2).to_h
      end

      def to_string
        children[0]
      end

      def to_symbol
        children[0]
      end

      def fcall_named?(name)
        fcall? &&
          children[0] == name &&
          children[1] &&
          children[1].array?
      end

      private

      def type
        @node.type
      end
    end

    def parse(code)
      Node.wrap(RubyVM::AbstractSyntaxTree.parse(code))
    end

    def node?(node)
      Node === node
    end

    def fcall?(node, name)
      node.fcall_named?(name)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
actionview_precompiler-0.2.1 lib/actionview_precompiler/ast_parser/ruby26.rb
actionview_precompiler-0.2.0 lib/actionview_precompiler/ast_parser/ruby26.rb