Class: Packwerk::Parsers::Erb

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

Instance Method Summary collapse

Constructor Details

#initialize(parser_class: BetterHtml::Parser, ruby_parser: Ruby.new) ⇒ Erb

Returns a new instance of Erb.



17
18
19
20
# File 'lib/packwerk/parsers/erb.rb', line 17

def initialize(parser_class: BetterHtml::Parser, ruby_parser: Ruby.new)
  @parser_class = T.let(parser_class, T.class_of(BetterHtml::Parser))
  @ruby_parser = ruby_parser
end

Instance Method Details

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



23
24
25
26
27
# File 'lib/packwerk/parsers/erb.rb', line 23

def call(io:, file_path: "<unknown>")
  buffer = Parser::Source::Buffer.new(file_path)
  buffer.source = io.read
  parse_buffer(buffer, file_path: file_path)
end

#parse_buffer(buffer, file_path:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/packwerk/parsers/erb.rb', line 30

def parse_buffer(buffer, file_path:)
  parser = @parser_class.new(buffer, template_language: :html)
  to_ruby_ast(parser.ast, file_path)
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