Class: HamlLint::Tree::Node Abstract

Inherits:
Object
  • Object
show all
Includes:
Enumerable
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.

Defined Under Namespace

Classes: Siblings

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



25
26
27
28
29
30
# File 'lib/haml_lint/tree/node.rb', line 25

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.



18
19
20
# File 'lib/haml_lint/tree/node.rb', line 18

def children
  @children
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

#parentObject

Returns the value of attribute parent.



18
19
20
# File 'lib/haml_lint/tree/node.rb', line 18

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#comment_configurationHamlLint::CommentConfiguration

Holds any configuration that is created from Haml comments.



35
36
37
# File 'lib/haml_lint/tree/node.rb', line 35

def comment_configuration
  @comment_configuration ||= HamlLint::CommentConfiguration.new(self)
end

#directivesArray<HamlLint::Directive>

The comment directives to apply to the node.

Returns:



64
65
66
67
68
# File 'lib/haml_lint/tree/node.rb', line 64

def directives
  directives = []
  directives << predecessor.directives if predecessor
  directives.flatten
end

#disabled?(visitor) ⇒ true, false

Checks whether a visitor is disabled due to comment configuration.

Parameters:

Returns:

  • (true, false)


43
44
45
46
# File 'lib/haml_lint/tree/node.rb', line 43

def disabled?(visitor)
  visitor.is_a?(HamlLint::Linter) &&
    comment_configuration.disabled?(visitor.name)
end

#eachEnumerator, HamlLint::Tree::Node

Implements the Enumerable interface to walk through an entire tree.

Returns:



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

def each
  return to_enum(__callee__) unless block_given?

  node = self
  loop do
    yield node
    break unless (node = node.next_node)
  end
end

#inspectObject



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

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

#line_numbersRange

The line numbers that are contained within the node.

Returns:

  • (Range)


104
105
106
107
108
109
110
111
# File 'lib/haml_lint/tree/node.rb', line 104

def line_numbers
  return (line..line) unless @value && text

  end_line = line + lines.count
  end_line = nontrivial_end_line if line == end_line && children.empty?

  (line..end_line)
end

#linesArray<String>

The lines of text, if any, that are contained in the node.

Returns:

  • (Array<String>)


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

def lines
  return [] unless @value && text

  text.split(/\r\n|\r|\n/)
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:



140
141
142
# File 'lib/haml_lint/tree/node.rb', line 140

def next_node
  children.first || successor
end

#predecessorHamlLint::Tree::Node?

The previous node to be traversed in the tree.

Returns:



116
117
118
# File 'lib/haml_lint/tree/node.rb', line 116

def predecessor
  siblings.previous(self) || parent
end

#source_codeString

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

Returns:

  • (String)


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

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

#subsequentsArray<HamlLint::Tree::Node>

The sibling nodes that come after this node in the tree.

Returns:



147
148
149
# File 'lib/haml_lint/tree/node.rb', line 147

def subsequents
  siblings.subsequents(self)
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:



128
129
130
131
132
133
# File 'lib/haml_lint/tree/node.rb', line 128

def successor
  next_sibling = siblings.next(self)
  return next_sibling if next_sibling

  parent&.successor
end

#textString

Returns the text content of this node.

Returns:

  • (String)


154
155
156
# File 'lib/haml_lint/tree/node.rb', line 154

def text
  @value[:text].to_s
end