Sha256: 6fd4b6ca9ec6642f42f68f1e523bcab501b21a18238a9ab51addeb9c63621f40

Contents?: true

Size: 1022 Bytes

Versions: 9

Compression:

Stored size: 1022 Bytes

Contents

# frozen_string_literal: true

require 'rubocop'
require 'rubocop/ast/builder'

def require_parser(path)
  prev = $VERBOSE
$VERBOSE = nil
  require "parser/#{path}"
ensure
  $VERBOSE = prev
end

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
      require_parser('current')
      @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

9 entries across 9 versions & 1 rubygems

Version Path
slim_lint-0.31.1 lib/slim_lint/ruby_parser.rb
slim_lint-0.31.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.30.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.29.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.28.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.27.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.26.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.25.0 lib/slim_lint/ruby_parser.rb
slim_lint-0.24.0 lib/slim_lint/ruby_parser.rb