Module: Herb::Embedded::ResultEnvelope

Defined in:
lib/herb/embedded/result_envelope.rb

Overview

Herb::ParseResult#to_json returns an inspect string, not the wire format @herb-tools/core's ParseResult.from() expects. This builds that envelope by hand from the pieces that do serialize correctly.

Constant Summary collapse

FORWARDABLE_OPTIONS =

Allowlist of Herb::ParserOptions keys safe to forward from caller-supplied options. Deliberately excludes prism_nodes, prism_nodes_deep, and prism_program: asking Herb.parse itself to embed prism_node populates it with a raw ASCII-8BIT String, and forwarding that through raises JSON::GeneratorError. Both are instead handled below by computing JSON-safe byte arrays ourselves. Also excludes timeout and max_errors (timing/error-cap options, not shape).

%i[
  strict
  track_whitespace
  analyze
  action_view_helpers
  transform_conditionals
  render_nodes
  strict_locals
].freeze

Class Method Summary collapse

Class Method Details

.lex(source) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/herb/embedded/result_envelope.rb', line 58

def lex(source)
  result = Herb.lex(source)

  {
    tokens: result.value,
    source: result.source,
    warnings: result.warnings,
    errors: result.errors,
  }.to_json
end

.parse(source, options_hash = {}) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/herb/embedded/result_envelope.rb', line 33

def parse(source, options_hash = {})
  options_hash = (options_hash || {}).transform_keys(&:to_sym)
  envelope = build_envelope(source, options_hash)

  return envelope.to_json unless options_hash[:prism_nodes] || options_hash[:prism_nodes_deep]

  with_injected_prism_nodes(envelope, source)
end