class HamlLint::RubyParser

Parser for the Ruby language.

This provides a convenient wrapper around the ‘parser` gem and the Astrolabe integration (now built-in to RuboCop, so no longer called Astrolabe) to go with it. It is intended to be used for linter checks that require deep inspection of Ruby code.

Public Class Methods

new() click to toggle source

Creates a reusable parser.

# File lib/haml_lint/ruby_parser.rb, line 15
def initialize
  require_parser
  @builder = ::RuboCop::AST::Builder.new
  @parser = ::Parser::CurrentRuby.new(@builder)
end

Public Instance Methods

parse(source) click to toggle source

Parse the given Ruby source into an abstract syntax tree.

@param source [String] Ruby source code @return [Array] syntax tree in the form returned by Parser gem

# File lib/haml_lint/ruby_parser.rb, line 35
def parse(source)
  buffer = ::Parser::Source::Buffer.new('(string)')
  buffer.source = source

  @parser.reset
  @parser.parse(buffer)
end
require_parser() click to toggle source

Require the current parser version while suppressing the compliancy warning for minor version differences.

# File lib/haml_lint/ruby_parser.rb, line 23
def require_parser
  prev = $VERBOSE
  $VERBOSE = nil
  require 'parser/current'
ensure
  $VERBOSE = prev
end