Class: Less2Sass::Less::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/less2sass/less/environment.rb

Overview

The lexical environment for Less. Keeps track of variable and mixin definitions. The environment differentiates between simple and constructed(dynamic) variable definitions.

A new environment is created for each level of Less nesting. This allows variables to be lexically scoped. Each environment refers to the environment in the upper scope, unless it is the global environment, thus having access to variables defined in enclosing scopes. New variables are defined locally.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Environment

Returns a new instance of Environment.



42
43
44
45
46
47
48
49
50
# File 'lib/less2sass/less/environment.rb', line 42

def initialize(parent = nil)
  @parent = parent
  @variables_to_reorder = {}
  @static_var_def_rules = {}
  @dynamic_var_def_rules = []
  @rules = []
  @rest = []
  @mixin_call_rules = []
end

Instance Attribute Details

#dynamic_var_def_rulesArray<Less2Sass::Less::Tree::RuleNode> (readonly)

Ordered list of constructed variable definitions. Examples:

- @var: @var2;
- @var1: @var2 + 10px;


34
35
36
# File 'lib/less2sass/less/environment.rb', line 34

def dynamic_var_def_rules
  @dynamic_var_def_rules
end

#mixin_call_rulesArray<Less2Sass::Less::Tree::RuleNode> (readonly)

A list of mixin calls.



39
40
41
# File 'lib/less2sass/less/environment.rb', line 39

def mixin_call_rules
  @mixin_call_rules
end

#parentLess2Sass::Less::Environment (readonly)

The enclosing environment, or nil if it’s the global environment.



19
20
21
# File 'lib/less2sass/less/environment.rb', line 19

def parent
  @parent
end

#static_var_def_rulesArray<Less2Sass::Less::Tree::RuleNode> (readonly)

Ordered list of static variable definitions. Examples:

- @var: 50px;


26
27
28
# File 'lib/less2sass/less/environment.rb', line 26

def static_var_def_rules
  @static_var_def_rules
end

Instance Method Details

#buildvoid

TODO:

implement detection in future releases

Note:

does not detect recursive variable definitions

This method returns an undefined value.

Builds the ‘dynamic_var_def_rules` ordered array.

Processes lazy loaded variables by importing their definition if they are overridden of this environment’s scope. Furthermore, it reorders the variable definitions. The only rule is, that the referenced variables should be placed before their first referral. If no variable definition references the the current iteration’s variable, it shall be put at the end of the array, since it could reference any of the previously defined variables.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/less2sass/less/environment.rb', line 75

def build
  process_lazy_loading
  to_reorder = @variables_to_reorder.values
  unless to_reorder.empty?
    @dynamic_var_def_rules << to_reorder.shift
    to_reorder.each do |var|
      @dynamic_var_def_rules.each_with_index do |o_var, index|
        if o_var.references?(var.name)
          @dynamic_var_def_rules.insert(index, var)
          break
        end
        @dynamic_var_def_rules.insert(index + 1, var) if @dynamic_var_def_rules.last.eql?(o_var)
      end
    end
  end
end

#find_variable_definition_if_dynamic(name) ⇒ Object



124
125
126
127
128
# File 'lib/less2sass/less/environment.rb', line 124

def find_variable_definition_if_dynamic(name)
  variable = @variables_to_reorder[name]
  return variable if variable
  @parent ? @parent.find_variable_definition_if_dynamic(name) : nil
end

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

Reorders the child nodes of another node, that creates an environment, resp. an execution context.

The order should be:

1. Simple/static variables
These do not reference any other variable(s) in their definition.

2. Complex/dynamic variables
These can reference either the static variables in the same context,
other variables from the outer environment, or each other without any
cross-reference, also known as recursive reference.

TODO: maybe directives should be also sorted out and put before the mixin calls
3. Other nodes (directives, rules, rule sets, etc.)

4. Rules (selector + declarations)
The Sass rules (Less rule sets) create a new environment and could redefine
the variables' used after their declaration.

5. Mixin calls
These mixins can reference the rule sets created one step above.

Returns:



116
117
118
# File 'lib/less2sass/less/environment.rb', line 116

def get_ordered_child_nodes
  @static_var_def_rules.values + @dynamic_var_def_rules + @rest + @rules + @mixin_call_rules
end

#set_environment(objs) ⇒ Object

Places an array of nodes into this environment.

Parameters:

See Also:

  • #put


57
58
59
# File 'lib/less2sass/less/environment.rb', line 57

def set_environment(objs)
  objs.each { |obj| set(obj) }
end

#variable_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/less2sass/less/environment.rb', line 120

def variable_defined?(name)
  @static_var_def_rules[name] || @variables_to_reorder[name]
end