Class: Packwerk::Parsers::Ruby

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
ParserInterface
Defined in:
lib/packwerk/parsers/ruby.rb

Defined Under Namespace

Classes: RaiseExceptionsParser, TolerateInvalidUtf8Builder

Instance Method Summary collapse

Constructor Details

#initialize(parser_class: RaiseExceptionsParser) ⇒ Ruby

Returns a new instance of Ruby.



41
42
43
44
# File 'lib/packwerk/parsers/ruby.rb', line 41

def initialize(parser_class: RaiseExceptionsParser)
  @builder = T.let(TolerateInvalidUtf8Builder.new, Object)
  @parser_class = T.let(parser_class, T.class_of(RaiseExceptionsParser))
end

Instance Method Details

#call(io:, file_path: "<unknown>") ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/packwerk/parsers/ruby.rb', line 47

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