Class: Less2Sass::Less::Tree::Node
- Inherits:
-
Object
- Object
- Less2Sass::Less::Tree::Node
- Includes:
- Enumerable
- Defined in:
- lib/less2sass/less/tree/node.rb
Overview
The base node class of the Less AST.
Direct Known Subclasses
AlphaNode, AnonymousNode, AssignmentNode, AttributeNode, CallNode, ColorNode, CombinatorNode, CommentNode, ConditionNode, DetachedRulesetNode, DimensionNode, DirectiveNode, ElementNode, ExpressionNode, ExtendNode, ImportNode, KeywordNode, MediaNode, MixinCallNode, MixinDefinitionNode, NegativeNode, OperationNode, ParenNode, QuotedNode, RuleNode, RulesetCallNode, RulesetNode, SelectorNode, UnicodeDescriptorNode, UnitNode, UrlNode, ValueNode, VariableNode
Constant Summary collapse
- @@lines =
The current line number the conversion process is at
0
Instance Attribute Summary collapse
-
#children ⇒ Array<Less2Sass::Less::Tree::Node>
The child nodes of this node.
-
#has_children ⇒ Boolean
readonly
Whether or not this node has child nodes.
-
#has_parent ⇒ Boolean
readonly
Whether or not this node has parent node.
-
#line(opt = :current) ⇒ Fixnum
Returns the line number and sets the class level current line number based on the passed option.
-
#parent ⇒ Less2Sass::Less::Tree::Node
The parent node of this node.
- #ref_vars ⇒ Object
Instance Method Summary collapse
-
#<<(child) ⇒ Object
Appends a child to the node.
-
#==(other) ⇒ Boolean
Compares this node and another object (only other Nodes will be equal).
-
#contains_variables? ⇒ Boolean
Checks, whether a variable is referenced in the scope of the node.
-
#creates_context? ⇒ Boolean
Tells, whether the node creates a new context or variable environment.
-
#each {|node| ... } ⇒ Object
Iterates through each node in the tree rooted at this node in a pre-order walk.
-
#get_referenced_variable_names ⇒ Array<String>
Loops through the children nodes and searches for variable occurrences.
-
#initialize(parent) ⇒ Node
constructor
A new instance of Node.
-
#to_sass ⇒ Object
Converts this node to the equivalent Sass node.
-
#transform(env = nil) ⇒ Void
The base implementation of transform.
Constructor Details
#initialize(parent) ⇒ Node
Returns a new instance of Node.
49 50 51 52 53 |
# File 'lib/less2sass/less/tree/node.rb', line 49 def initialize(parent) @children = [] @parent = parent @creates_new_line = false end |
Instance Attribute Details
#children ⇒ Array<Less2Sass::Less::Tree::Node>
The child nodes of this node.
23 24 25 |
# File 'lib/less2sass/less/tree/node.rb', line 23 def children @children end |
#has_children ⇒ Boolean (readonly)
Whether or not this node has child nodes.
28 29 30 |
# File 'lib/less2sass/less/tree/node.rb', line 28 def has_children @has_children end |
#has_parent ⇒ Boolean (readonly)
Whether or not this node has parent node.
18 19 20 |
# File 'lib/less2sass/less/tree/node.rb', line 18 def has_parent @has_parent end |
#line(opt = :current) ⇒ Fixnum
Returns the line number and sets the class level current line number based on the passed option.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/less2sass/less/tree/node.rb', line 142 def line(opt = :current) case opt when :current @@lines when :new @@lines = @@lines.next else raise StandardError, "Option can't be other than :current or :new" end end |
#parent ⇒ Less2Sass::Less::Tree::Node
The parent node of this node.
13 14 15 |
# File 'lib/less2sass/less/tree/node.rb', line 13 def parent @parent end |
#ref_vars ⇒ Object
39 40 41 |
# File 'lib/less2sass/less/tree/node.rb', line 39 def ref_vars @ref_vars end |
Instance Method Details
#<<(child) ⇒ Object
Appends a child to the node.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/less2sass/less/tree/node.rb', line 84 def <<(child) return if child.nil? if child.is_a?(Array) child.each { |c| self << c } else @has_children = true child.parent = self @children << child end end |
#==(other) ⇒ Boolean
Compares this node and another object (only other Less2Sass::Less::Tree::Nodes will be equal).
100 101 102 |
# File 'lib/less2sass/less/tree/node.rb', line 100 def ==(other) self.class == other.class && other.children == children end |
#contains_variables? ⇒ Boolean
Checks, whether a variable is referenced in the scope of the node.
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/less2sass/less/tree/node.rb', line 184 def contains_variables? variables = nil each do |child| if child.is_a?(VariableNode) variables = true break end end variables end |
#creates_context? ⇒ Boolean
Tells, whether the node creates a new context or variable environment.
76 77 78 |
# File 'lib/less2sass/less/tree/node.rb', line 76 def creates_context? false end |
#each {|node| ... } ⇒ Object
Iterates through each node in the tree rooted at this node in a pre-order walk.
109 110 111 112 |
# File 'lib/less2sass/less/tree/node.rb', line 109 def each yield self children.each { |c| c.each { |n| yield n } } end |
#get_referenced_variable_names ⇒ Array<String>
Use on RuleNodes. It’s a good practice to check first if it’s a variable definition. See (Less2Sass::Less::Tree::RuleNode#is_variable_definition?)
Loops through the children nodes and searches for variable occurrences.
173 174 175 176 177 178 179 |
# File 'lib/less2sass/less/tree/node.rb', line 173 def get_referenced_variable_names if is_a?(VariableNode) [@name] else @children.inject([]) { |vars, c| vars + c.get_referenced_variable_names } end end |
#to_sass ⇒ Object
Converts this node to the equivalent Sass node
Should be overwritten by subclasses. All subclasses should also set the line number of the resulting Sass node.
161 162 163 |
# File 'lib/less2sass/less/tree/node.rb', line 161 def to_sass raise NotImplementedError end |
#transform(env = nil) ⇒ Void
The base implementation of transform.
Transforms the tree by its traversal. Each respective node type should implement the specific transformation and calling ‘#super`. The position, where `#super` is called determines the walking order. The standard should be post-order walk - transforming the child nodes first and self as last.
Should be overridden by subclasses adding specific implementation.
126 127 128 129 130 |
# File 'lib/less2sass/less/tree/node.rb', line 126 def transform(env = nil) # TODO: Consider to set a reference to the lexical scope (the environment) of the node. # @env = env @children.each { |c| c.transform(env) } end |