Sha256: fed0892bc4d19ee737b5a9d223d3d5312157eb297d10fa1b6477e49d0fff9b48

Contents?: true

Size: 869 Bytes

Versions: 6

Compression:

Stored size: 869 Bytes

Contents

require 'rubocop'
require 'rubocop/ast/builder'
require 'parser/current'

module SlimLint
  # Parser for the Ruby language.
  #
  # This provides a convenient wrapper around the `parser` gem and the
  # `astrolabe` integration to go with it. It is intended to be used for linter
  # checks that require deep inspection of Ruby code.
  class RubyParser
    # Creates a reusable parser.
    def initialize
      @builder = ::RuboCop::AST::Builder.new
      @parser = ::Parser::CurrentRuby.new(@builder)
    end

    # 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
    def parse(source)
      buffer = ::Parser::Source::Buffer.new('(string)')
      buffer.source = source

      @parser.reset
      @parser.parse(buffer)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
slim_lint-0.15.1 lib/slim_lint/ruby_parser.rb
slim_lint-0.15.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.14.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.13.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.12.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.11.0 lib/slim_lint/ruby_parser.rb