Module: ParseJS::AST::Scope

Included in:
FunctionDeclaration, FunctionExpression, Program
Defined in:
lib/parsejs/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#child_scopesObject

Returns the value of attribute child_scopes.



7
8
9
# File 'lib/parsejs/scope.rb', line 7

def child_scopes
  @child_scopes
end

#parent_scopeObject

Returns the value of attribute parent_scope.



7
8
9
# File 'lib/parsejs/scope.rb', line 7

def parent_scope
  @parent_scope
end

#parent_variablesObject

Returns the value of attribute parent_variables.



6
7
8
# File 'lib/parsejs/scope.rb', line 6

def parent_variables
  @parent_variables
end

#scope_variablesObject

Returns the value of attribute scope_variables.



6
7
8
# File 'lib/parsejs/scope.rb', line 6

def scope_variables
  @scope_variables
end

#variable_accessObject

Returns the value of attribute variable_access.



6
7
8
# File 'lib/parsejs/scope.rb', line 6

def variable_access
  @variable_access
end

Instance Method Details

#any_child_references_parent_variable?(name) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/parsejs/scope.rb', line 54

def any_child_references_parent_variable?(name)
  unless child_scopes.nil?
    # this should really check if all descendent scopes see a
    # scope variable before they see a parent refernce

    return false if child_scopes.all? { |s| s.scope_variable?(name) }

    child_scopes.any? do |s|
      s.parent_variable_access?(name) ||
        s.parent_variable?(name) ||
        s.any_child_references_parent_variable?(name)
    end
  end
end

#available_variable?(name) ⇒ Boolean

determine whether a variable can be added without causing damage to child scopes.

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/parsejs/scope.rb', line 44

def available_variable?(name)
  # if the current scope is already using the variable, it's
  # unavailable.
  return false if variable?(name)

  # if any of the child scopes reference the variable as a
  # parent variable, it's not available.
  !any_child_references_parent_variable?(name)
end

#parent_variable?(name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/parsejs/scope.rb', line 22

def parent_variable?(name)
  parent_variables && parent_variables.include?(name)
end

#parent_variable_access?(name) ⇒ Boolean

determine whether there is a reference for a particular variable to a parent scope.

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
# File 'lib/parsejs/scope.rb', line 32

def parent_variable_access?(name)
  # if this scope has a "var x = 1" type of declaration for this name,
  # it is not referencing a parent scope.
  return false if scope_variable?(name)

  # otherwise, if there is a variable access for this name, it's
  # referencing a parent scope.
  variable_access?(name)
end

#scope_variable?(name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/parsejs/scope.rb', line 18

def scope_variable?(name)
  scope_variables && scope_variables.include?(name)
end

#variable?(name) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/parsejs/scope.rb', line 14

def variable?(name)
  scope_variable?(name) || parent_variable?(name) || variable_access?(name)
end

#variable_access?(name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/parsejs/scope.rb', line 26

def variable_access?(name)
  variable_access && variable_access.include?(name)
end

#variable_in_scope?(name) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/parsejs/scope.rb', line 9

def variable_in_scope?(name)
  return true if variable?(name)
  parent_scope && parent_scope.variable_in_scope?(name)
end