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(parser, parse_node) ⇒ Node

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

Parameters:

  • parser (HamlLint::Parser)

    parser that created this node

  • parse_node (Haml::Parser::ParseNode)

    parse node created by HAML's parser


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

def initialize(parser, parse_node)
  # TODO: Change signature to take source code object, not parser
  @line = parse_node.line
  @parser = parser
  @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(&block) ⇒ 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.

Returns:


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

def find(&block)
  return self if block.call(self)

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

  nil # Otherwise no matching node was found
end

#inspectObject


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

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:


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

def next_node
  children.first || successor
end

#source_codeString

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

Returns:

  • (String)

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

def source_code
  next_node_line =
    if next_node
      next_node.line - 1
    else
      @parser.lines.count + 1
    end

  @parser.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:


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

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)

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

def text
  @value[:text].to_s
end