Sha256: 706f3d3ea595a15428557457574b5ffa5587b2375a3d1e09fbe64d70f7b8b286

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# typed: strict
# frozen_string_literal: true

require "parser"
require "parser/current"

module Packwerk
  module Parsers
    class Ruby
      extend T::Sig

      include ParserInterface

      class RaiseExceptionsParser < Parser::CurrentRuby
        extend T::Sig

        sig { params(builder: T.untyped).void }
        def initialize(builder)
          super(builder)
          super.diagnostics.all_errors_are_fatal = true
        end
      end

      class TolerateInvalidUtf8Builder < Parser::Builders::Default
        extend T::Sig

        sig { params(token: T.untyped).returns(T.untyped) }
        def string_value(token)
          value(token)
        end
      end

      sig { params(parser_class: T.untyped).void }
      def initialize(parser_class: RaiseExceptionsParser)
        @builder = T.let(TolerateInvalidUtf8Builder.new, Object)
        @parser_class = T.let(parser_class, T.class_of(RaiseExceptionsParser))
      end

      sig { override.params(io: T.any(IO, StringIO), file_path: String).returns(T.nilable(Parser::AST::Node)) }
      def call(io:, file_path: "<unknown>")
        buffer = Parser::Source::Buffer.new(file_path)
        buffer.source = io.read
        parser = @parser_class.new(@builder)
        parser.parse(buffer)
      rescue EncodingError => e
        result = ParseResult.new(file: file_path, message: e.message)
        raise Parsers::ParseError, result
      rescue Parser::SyntaxError => e
        result = ParseResult.new(file: file_path, message: "Syntax error: #{e}")
        raise Parsers::ParseError, result
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
packwerk-3.1.0 lib/packwerk/parsers/ruby.rb
packwerk-3.0.1 lib/packwerk/parsers/ruby.rb
packwerk-3.0.0 lib/packwerk/parsers/ruby.rb