Class: ParseJS::AST::ProcessVariables

Inherits:
Visitor
  • Object
show all
Includes:
Visitor::ScopeManager
Defined in:
lib/parsejs/scope.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Visitor::ScopeManager

#current_scope, #initialize, #push_child, #visit_FunctionDeclaration, #visit_Program

Methods inherited from Visitor

#accept, #map, #visit, #visit_BlockStatement, #visit_BreakStatement, #visit_CatchClause, #visit_Comment, #visit_CommentedStatement, #visit_ContinueStatement, #visit_DebuggerStatement, #visit_DoWhileStatement, #visit_EmptyStatement, #visit_ExpressionStatement, #visit_ForInStatement, #visit_ForStatement, #visit_FunctionDeclaration, #visit_FunctionExpression, #visit_Identifier, #visit_IfStatement, #visit_Literal, #visit_LogicalExpression, #visit_Number, #visit_ObjectExpression, #visit_ParameterList, #visit_Program, #visit_RegExp, #visit_ReturnStatement, #visit_String, #visit_SwitchCase, #visit_SwitchStatement, #visit_ThisExpression, #visit_ThrowStatement, #visit_TryStatement, #visit_VariableDeclaration, #visit_WhileStatement, #visit_WithStatement

Class Method Details

.process(ast) ⇒ Object



73
74
75
# File 'lib/parsejs/scope.rb', line 73

def self.process(ast)
  new.visit(ast)
end

Instance Method Details

#possible_variable_access(var) ⇒ Object



92
93
94
# File 'lib/parsejs/scope.rb', line 92

def possible_variable_access(var)
  push_variable_access var.val if var && var.type?("Identifier")
end

#possible_variable_access_array(arr) ⇒ Object



96
97
98
# File 'lib/parsejs/scope.rb', line 96

def possible_variable_access_array(arr)
  arr.each { |arg| possible_variable_access arg }
end

#push_parent_variable(name) ⇒ Object



82
83
84
85
# File 'lib/parsejs/scope.rb', line 82

def push_parent_variable(name)
  vars = current_scope.parent_variables ||= Set.new
  vars << name
end

#push_scope_variable(name) ⇒ Object



77
78
79
80
# File 'lib/parsejs/scope.rb', line 77

def push_scope_variable(name)
  vars = current_scope.scope_variables ||= Set.new
  vars << name
end

#push_variable_access(name) ⇒ Object



87
88
89
90
# File 'lib/parsejs/scope.rb', line 87

def push_variable_access(name)
  vars = current_scope.variable_access ||= Set.new
  vars << name
end

#visit_ArrayExpression(expr) ⇒ Object



127
128
129
130
131
# File 'lib/parsejs/scope.rb', line 127

def visit_ArrayExpression(expr)
  possible_variable_access_array expr.elements

  super
end

#visit_AssignmentExpression(expr) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/parsejs/scope.rb', line 133

def visit_AssignmentExpression(expr)
  if expr.left.type?("Identifier")
    push_parent_variable expr.left.val
  end

  possible_variable_access expr.right

  super
end

#visit_BinaryExpression(expr) ⇒ Object



167
168
169
170
171
172
# File 'lib/parsejs/scope.rb', line 167

def visit_BinaryExpression(expr)
  possible_variable_access expr.left
  possible_variable_access expr.right

  super
end

#visit_CallExpression(expr) ⇒ Object



113
114
115
116
117
118
# File 'lib/parsejs/scope.rb', line 113

def visit_CallExpression(expr)
  possible_variable_access expr.callee
  possible_variable_access_array expr.args

  super
end

#visit_ConditionalExpression(expr) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/parsejs/scope.rb', line 174

def visit_ConditionalExpression(expr)
  possible_variable_access expr.alternate
  possible_variable_access expr.consequent
  possible_variable_access expr.test

  super
end

#visit_MemberExpression(expr) ⇒ Object



107
108
109
110
111
# File 'lib/parsejs/scope.rb', line 107

def visit_MemberExpression(expr)
  possible_variable_access expr.object

  super
end

#visit_NewExpression(expr) ⇒ Object



120
121
122
123
124
125
# File 'lib/parsejs/scope.rb', line 120

def visit_NewExpression(expr)
  possible_variable_access expr.callee
  possible_variable_access_array expr.args if expr.args

  super
end

#visit_Property(prop) ⇒ Object



143
144
145
146
147
# File 'lib/parsejs/scope.rb', line 143

def visit_Property(prop)
  possible_variable_access prop.value

  super
end

#visit_SequenceExpression(expr) ⇒ Object



149
150
151
152
153
# File 'lib/parsejs/scope.rb', line 149

def visit_SequenceExpression(expr)
  possible_variable_access_array expr.expressions

  super
end

#visit_UnaryExpression(expr) ⇒ Object



161
162
163
164
165
# File 'lib/parsejs/scope.rb', line 161

def visit_UnaryExpression(expr)
  possible_variable_access expr.argument

  super
end

#visit_UpdateExpression(expr) ⇒ Object



155
156
157
158
159
# File 'lib/parsejs/scope.rb', line 155

def visit_UpdateExpression(expr)
  possible_variable_access expr.argument

  super
end

#visit_VariableDeclarator(decl) ⇒ Object



100
101
102
103
104
105
# File 'lib/parsejs/scope.rb', line 100

def visit_VariableDeclarator(decl)
  push_scope_variable decl.id.val
  possible_variable_access decl.init

  super
end