Class: Herb::AST::ERBUnlessNode

Inherits:
Node
  • Object
show all
Includes:
Colors
Defined in:
lib/herb/ast/nodes.rb,
ext/herb/nodes.c

Overview

: type serialized_erb_unless_node = { | tag_opening: Herb::Token?, | content: Herb::Token?, | tag_closing: Herb::Token?, | then_keyword: Herb::Location?, | prism_node: String?, | statements: Array, | else_clause: Herb::AST::ERBElseNode?, | end_node: Herb::AST::ERBEndNode?, | }

Constant Summary

Constants included from Colors

Colors::CLEAR_SCREEN, Colors::HIDE_CURSOR, Colors::SHOW_CURSOR

Instance Attribute Summary collapse

Attributes inherited from Node

#errors, #location, #source, #type

Instance Method Summary collapse

Methods included from Colors

bold, bright_magenta, cyan, dimmed, enabled?, fg, fg_bg, green, magenta, red, white, yellow

Methods inherited from Node

#class_name, #inspect_array, #inspect_errors, #node_name, #recursive_errors, #to_json

Constructor Details

#initialize(type, location, errors, tag_opening, content, tag_closing, then_keyword, prism_node, statements, else_clause, end_node) ⇒ ERBUnlessNode

: (String, Location, Array, Herb::Token, Herb::Token, Herb::Token, Herb::Location, String, Array, Herb::AST::ERBElseNode, Herb::AST::ERBEndNode) -> void



3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
# File 'lib/herb/ast/nodes.rb', line 3390

def initialize(type, location, errors, tag_opening, content, tag_closing, then_keyword, prism_node, statements, else_clause, end_node)
  super(type, location, errors)
  @tag_opening = tag_opening
  @content = content
  @tag_closing = tag_closing
  @then_keyword = then_keyword
  @prism_node = prism_node
  @statements = statements
  @else_clause = else_clause
  @end_node = end_node
end

Instance Attribute Details

#contentObject (readonly)

: Herb::Token?



3381
3382
3383
# File 'lib/herb/ast/nodes.rb', line 3381

def content
  @content
end

#else_clauseObject (readonly)

: Herb::AST::ERBElseNode?



3386
3387
3388
# File 'lib/herb/ast/nodes.rb', line 3386

def else_clause
  @else_clause
end

#end_nodeObject (readonly)

: Herb::AST::ERBEndNode?



3387
3388
3389
# File 'lib/herb/ast/nodes.rb', line 3387

def end_node
  @end_node
end

#prism_nodeObject (readonly)

: String?



3384
3385
3386
# File 'lib/herb/ast/nodes.rb', line 3384

def prism_node
  @prism_node
end

#statementsObject (readonly)

: Array



3385
3386
3387
# File 'lib/herb/ast/nodes.rb', line 3385

def statements
  @statements
end

#tag_closingObject (readonly)

: Herb::Token?



3382
3383
3384
# File 'lib/herb/ast/nodes.rb', line 3382

def tag_closing
  @tag_closing
end

#tag_openingObject (readonly)

: Herb::Token?



3380
3381
3382
# File 'lib/herb/ast/nodes.rb', line 3380

def tag_opening
  @tag_opening
end

#then_keywordObject (readonly)

: Herb::Location?



3383
3384
3385
# File 'lib/herb/ast/nodes.rb', line 3383

def then_keyword
  @then_keyword
end

Instance Method Details

#accept(visitor) ⇒ Object

: (Visitor) -> void



3433
3434
3435
# File 'lib/herb/ast/nodes.rb', line 3433

def accept(visitor)
  visitor.visit_erb_unless_node(self)
end

#child_nodesObject

: () -> Array



3438
3439
3440
# File 'lib/herb/ast/nodes.rb', line 3438

def child_nodes
  [*(statements || []), else_clause, end_node]
end

#compact_child_nodesObject

: () -> Array



3443
3444
3445
# File 'lib/herb/ast/nodes.rb', line 3443

def compact_child_nodes
  child_nodes.compact
end

#deserialized_prism_nodeObject

: () -> Prism::node?



3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
# File 'lib/herb/ast/nodes.rb', line 3403

def deserialized_prism_node
  prism_node = @prism_node
  return nil unless prism_node
  return nil unless source

  begin
    require "prism"
  rescue LoadError
    warn "The 'prism' gem is required to deserialize Prism nodes. Add it to your Gemfile or install it with: gem install prism"
    return nil
  end

  Prism.load(source, prism_node).value
end

#inspectObject

: () -> String



3448
3449
3450
# File 'lib/herb/ast/nodes.rb', line 3448

def inspect
  tree_inspect.rstrip.gsub(/\s+$/, "")
end

#to_hashObject

: () -> serialized_erb_unless_node



3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
# File 'lib/herb/ast/nodes.rb', line 3419

def to_hash
  super.merge({
    tag_opening: tag_opening,
    content: content,
    tag_closing: tag_closing,
    then_keyword: then_keyword,
    prism_node: prism_node,
    statements: statements,
    else_clause: else_clause,
    end_node: end_node,
  }) #: Herb::serialized_erb_unless_node
end

#tree_inspect(indent: 0, depth: 0, depth_limit: 10) ⇒ Object

: (?indent: Integer, ?depth: Integer, ?depth_limit: Integer) -> String



3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
# File 'lib/herb/ast/nodes.rb', line 3453

def tree_inspect(indent: 0, depth: 0, depth_limit: 10)
  output = +""

  output += white("@ #{bold(yellow(node_name.to_s))} #{dimmed("(location: #{location.tree_inspect})")}")
  output += "\n"

  if depth >= depth_limit
    output += dimmed("└── [depth limit reached ...]\n\n")

    return output.gsub(/^/, "    " * indent)
  end

  output += inspect_errors(prefix: "")

  output += white("├── tag_opening: ")
  output += tag_opening ? tag_opening.tree_inspect : magenta("")
  output += "\n"
  output += white("├── content: ")
  output += content ? content.tree_inspect : magenta("")
  output += "\n"
  output += white("├── tag_closing: ")
  output += tag_closing ? tag_closing.tree_inspect : magenta("")
  output += "\n"
  output += white("├── then_keyword: ")
  output += then_keyword ? dimmed("(location: #{then_keyword.tree_inspect})") : magenta("")
  output += "\n"
  if prism_node && source
    output += white("├── prism_node: ")
    output += Herb::PrismInspect.inspect_prism_serialized(prism_node, source, "")
    output += "\n"
  end
  output += white("├── statements: ")
  output += inspect_array(statements, prefix: "", indent: indent, depth: depth + 1, depth_limit: depth_limit)
  output += white("├── else_clause: ")
  if else_clause
    output += "\n"
    output += "│   └── "
    output += else_clause.tree_inspect(indent: indent, depth: depth + 1, depth_limit: depth_limit).gsub(/^/, "    " * (indent + 1)).lstrip.gsub(/^/, "").delete_prefix("")
  else
    output += magenta("∅\n")
  end
  output += white("└── end_node: ")
  if end_node
    output += "\n"
    output += "    └── "
    output += end_node.tree_inspect(indent: indent, depth: depth + 1, depth_limit: depth_limit).gsub(/^/, "    " * (indent + 1)).lstrip.gsub(/^/, "    ").delete_prefix("    ")
  else
    output += magenta("∅\n")
  end
  output += "\n"

  output.gsub(/^/, "    " * indent)
end