Class: HamlLint::Tree::Node Abstract
- Inherits:
-
Object
- Object
- HamlLint::Tree::Node
- Includes:
- Enumerable
- Defined in:
- lib/haml_lint/tree/node.rb
Overview
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.
Direct Known Subclasses
CommentNode, DoctypeNode, FilterNode, HamlCommentNode, NullNode, PlainNode, RootNode, ScriptNode, SilentScriptNode, TagNode
Defined Under Namespace
Classes: Siblings
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#comment_configuration ⇒ HamlLint::CommentConfiguration
Holds any configuration that is created from Haml comments.
-
#directives ⇒ Array<HamlLint::Directive>
The comment directives to apply to the node.
-
#disabled?(visitor) ⇒ true, false
Checks whether a visitor is disabled due to comment configuration.
-
#each ⇒ Enumerator, HamlLint::Tree::Node
Implements the Enumerable interface to walk through an entire tree.
-
#initialize(document, parse_node) ⇒ Node
constructor
Creates a node wrapping the given Haml::Parser::ParseNode struct.
- #inspect ⇒ Object
- #keyword ⇒ Object
-
#line_numbers ⇒ Range
The line numbers that are contained within the node.
-
#lines ⇒ Array<String>
The lines of text, if any, that are contained in the node.
-
#next_node ⇒ HamlLint::Tree::Node?
Returns the next node that appears after this node in the document.
-
#predecessor ⇒ HamlLint::Tree::Node?
The previous node to be traversed in the tree.
-
#source_code ⇒ String
Source code of all lines this node spans (excluding children).
-
#subsequents ⇒ Array<HamlLint::Tree::Node>
The sibling nodes that come after this node in the tree.
-
#successor ⇒ HamlLint::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.
-
#text ⇒ String
Returns the text content of this node.
Constructor Details
#initialize(document, parse_node) ⇒ Node
Creates a node wrapping the given Haml::Parser::ParseNode struct.
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
#children ⇒ Object
Returns the value of attribute children.
18 19 20 |
# File 'lib/haml_lint/tree/node.rb', line 18 def children @children end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
19 20 21 |
# File 'lib/haml_lint/tree/node.rb', line 19 def line @line end |
#parent ⇒ Object
Returns the value of attribute parent.
18 19 20 |
# File 'lib/haml_lint/tree/node.rb', line 18 def parent @parent end |
#type ⇒ Object (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_configuration ⇒ HamlLint::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 |
#directives ⇒ Array<HamlLint::Directive>
The comment directives to apply to the node.
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.
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 |
#each ⇒ Enumerator, HamlLint::Tree::Node
Implements the Enumerable interface to walk through an entire tree.
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 |
#inspect ⇒ Object
86 87 88 |
# File 'lib/haml_lint/tree/node.rb', line 86 def inspect "#<#{self.class.name}>" end |
#keyword ⇒ Object
163 164 165 |
# File 'lib/haml_lint/tree/node.rb', line 163 def keyword @value[:keyword] end |
#line_numbers ⇒ Range
The line numbers that are contained within the node.
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/haml_lint/tree/node.rb', line 104 def line_numbers return (line..line) unless @value && text end_line = if !lines.empty? line + lines.count - 1 elsif children.empty? nontrivial_end_line else line end (line..end_line) end |
#lines ⇒ Array<String>
The lines of text, if any, that are contained in the node.
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_node ⇒ HamlLint::Tree::Node?
Returns the next node that appears after this node in the document.
Returns nil if there is no next node.
145 146 147 |
# File 'lib/haml_lint/tree/node.rb', line 145 def next_node children.first || successor end |
#predecessor ⇒ HamlLint::Tree::Node?
The previous node to be traversed in the tree.
121 122 123 |
# File 'lib/haml_lint/tree/node.rb', line 121 def predecessor siblings.previous(self) || parent end |
#source_code ⇒ String
Source code of all lines this node spans (excluding children).
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 |
#subsequents ⇒ Array<HamlLint::Tree::Node>
The sibling nodes that come after this node in the tree.
152 153 154 |
# File 'lib/haml_lint/tree/node.rb', line 152 def subsequents siblings.subsequents(self) end |
#successor ⇒ HamlLint::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.
133 134 135 136 137 138 |
# File 'lib/haml_lint/tree/node.rb', line 133 def successor next_sibling = siblings.next(self) return next_sibling if next_sibling parent&.successor end |
#text ⇒ String
Returns the text content of this node.
159 160 161 |
# File 'lib/haml_lint/tree/node.rb', line 159 def text @value[:text].to_s end |