Class: Less2Sass::Less::Tree::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/less2sass/less/tree/node.rb

Overview

The base node class of the Less AST.

Constant Summary collapse

@@lines =

The current line number the conversion process is at

0

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenArray<Less2Sass::Less::Tree::Node>

The child nodes of this node.

Returns:



23
24
25
# File 'lib/less2sass/less/tree/node.rb', line 23

def children
  @children
end

#has_childrenBoolean (readonly)

Whether or not this node has child nodes.

Returns:

  • (Boolean)


28
29
30
# File 'lib/less2sass/less/tree/node.rb', line 28

def has_children
  @has_children
end

#has_parentBoolean (readonly)

Whether or not this node has parent node.

Returns:

  • (Boolean)


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.

Parameters:

  • opt (Symbol) (defaults to: :current)

    the line number option

Options Hash (opt):

  • :new (Symbol)

    tells the method to return the next line and increments @@lines

  • :current (Symbol)

    tells the method to return the current line the converter is “processing”

Returns:

  • (Fixnum)

    the line number based on the given 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

#parentLess2Sass::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_varsObject



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.

Parameters:



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).

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

    Whether or not this node and the other object are the same



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Yields:

  • node

Yield Parameters:



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_namesArray<String>

Note:

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.

Returns:

  • (Array<String>)

    list of variable names contained in the value’s expressions



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_sassObject

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.

Raises:

  • NotImplementedError if called on the base node object or subclass method not implemented, yet.



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.

Parameters:

Returns:

  • (Void)


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