Class: HamlLint::Tree::Node Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/haml_lint/tree/node.rb

Overview

This class is abstract.

Decorator class that provides a convenient set of helpers for HAML’s Haml::Parser::ParseNode struct.

The goal is to abstract away the details of the underlying struct and provide a cleaner and more uniform interface for getting information about a node, as there are a number of weird/special cases in the struct returned by the HAML parser.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, parse_node) ⇒ Node

Creates a node wrapping the given Haml::Parser::ParseNode struct.

Parameters:

  • document (HamlLint::Document)

    Haml document that created this node

  • parse_node (Haml::Parser::ParseNode)

    parse node created by HAML’s parser



19
20
21
22
23
24
# File 'lib/haml_lint/tree/node.rb', line 19

def initialize(document, parse_node)
  @line = parse_node.line
  @document = document
  @value = parse_node.value
  @type = parse_node.type
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



12
13
14
# File 'lib/haml_lint/tree/node.rb', line 12

def children
  @children
end

#lineObject (readonly)

Returns the value of attribute line.



13
14
15
# File 'lib/haml_lint/tree/node.rb', line 13

def line
  @line
end

#parentObject

Returns the value of attribute parent.



12
13
14
# File 'lib/haml_lint/tree/node.rb', line 12

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/haml_lint/tree/node.rb', line 13

def type
  @type
end

Instance Method Details

#find {|node| ... } ⇒ HamlLint::Tree::Node?

Returns the first node found under the subtree which matches the given block.

Returns nil if no node matching the given block was found.

Yield Parameters:

Yield Returns:

  • (Boolean)

    whether the node matches

Returns:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/haml_lint/tree/node.rb', line 34

def find(&block)
  return self if yield(self)

  children.each do |child|
    if result = child.find(&block)
      return result
    end
  end

  nil # Otherwise no matching node was found
end

#inspectObject



62
63
64
# File 'lib/haml_lint/tree/node.rb', line 62

def inspect
  "#<#{self.class.name}>"
end

#next_nodeHamlLint::Tree::Node?

Returns the next node that appears after this node in the document.

Returns nil if there is no next node.

Returns:



88
89
90
# File 'lib/haml_lint/tree/node.rb', line 88

def next_node
  children.first || successor
end

#source_codeString

Source code of all lines this node spans (excluding children).

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/haml_lint/tree/node.rb', line 49

def source_code
  next_node_line =
    if next_node
      next_node.line - 1
    else
      @document.source_lines.count + 1
    end

  @document.source_lines[@line - 1...next_node_line]
    .join("\n")
    .gsub(/^\s*\z/m, '') # Remove blank lines at the end
end

#successorHamlLint::Tree::Node?

Returns the node that follows this node, whether it be a sibling or an ancestor’s child, but not a child of this node.

If you are also willing to return the child, call #next_node.

Returns nil if there is no successor.

Returns:



74
75
76
77
78
79
80
81
# File 'lib/haml_lint/tree/node.rb', line 74

def successor
  siblings = parent ? parent.children : [self]

  next_sibling = siblings[siblings.index(self) + 1] if siblings.count > 1
  return next_sibling if next_sibling

  parent.successor if parent
end

#textString

Returns the text content of this node.

Returns:

  • (String)


95
96
97
# File 'lib/haml_lint/tree/node.rb', line 95

def text
  @value[:text].to_s
end