Class: ParseJS::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/parsejs/visitor.rb

Direct Known Subclasses

AST::ProcessVariables, CommentScanner, FindVars, Stringifier

Defined Under Namespace

Modules: ScopeManager

Instance Method Summary (collapse)

Instance Method Details

- (Object) accept(node)



3
4
5
# File 'lib/parsejs/visitor.rb', line 3

def accept(node)
  visit(node) if node
end

- (Object) map(node)



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

def map(node)
  node && node.map { |item| item ? accept(item) : nil }
end

- (Object) visit(node)



11
12
13
14
# File 'lib/parsejs/visitor.rb', line 11

def visit(node)
  type = node.class.name.split("::").last
  send("visit_#{type}", node)
end

- (Object) visit_ArrayExpression(expr)



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

def visit_ArrayExpression(expr)
  map(expr.elements)
end

- (Object) visit_AssignmentExpression(expr)



79
80
81
# File 'lib/parsejs/visitor.rb', line 79

def visit_AssignmentExpression(expr)
  [accept(expr.left), expr.op, accept(expr.right)]
end

- (Object) visit_BinaryExpression(expr)



111
112
113
# File 'lib/parsejs/visitor.rb', line 111

def visit_BinaryExpression(expr)
  [accept(expr.left), expr.op, accept(expr.right)]
end

- (Object) visit_BlockStatement(statement)



119
120
121
# File 'lib/parsejs/visitor.rb', line 119

def visit_BlockStatement(statement)
  map(statement.statements)
end

- (Object) visit_BreakStatement(statement)



204
205
206
# File 'lib/parsejs/visitor.rb', line 204

def visit_BreakStatement(statement)
  accept(statement.label) if statement.label
end

- (Object) visit_CallExpression(expr)



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

def visit_CallExpression(expr)
  [accept(expr.callee), map(expr.args)]
end

- (Object) visit_CatchClause(clause)



184
185
186
# File 'lib/parsejs/visitor.rb', line 184

def visit_CatchClause(clause)
  [accept(clause.param), accept(clause.body)]
end

- (Object) visit_Comment(comment)



216
217
218
# File 'lib/parsejs/visitor.rb', line 216

def visit_Comment(comment)
  [comment.type, comment.body, comment.newline]
end

- (Object) visit_CommentedStatement(statement)



99
100
101
# File 'lib/parsejs/visitor.rb', line 99

def visit_CommentedStatement(statement)
  [map(statement.comments), accept(statement.statement)]
end

- (Object) visit_ConditionalExpression(expr)



212
213
214
# File 'lib/parsejs/visitor.rb', line 212

def visit_ConditionalExpression(expr)
  [accept(expr.test), accept(expr.consequent), accept(expr.alternate)]
end

- (Object) visit_ContinueStatement(statement)



208
209
210
# File 'lib/parsejs/visitor.rb', line 208

def visit_ContinueStatement(statement)
  accept(statement.label) if statement.label
end

- (Object) visit_DebuggerStatement(expr)



51
52
53
# File 'lib/parsejs/visitor.rb', line 51

def visit_DebuggerStatement(expr)
  "debugger"
end

- (Object) visit_DoWhileStatement(statement)



135
136
137
# File 'lib/parsejs/visitor.rb', line 135

def visit_DoWhileStatement(statement)
  [accept(statement.body), accept(statement.test)]
end

- (Object) visit_EmptyStatement(empty)



55
56
57
# File 'lib/parsejs/visitor.rb', line 55

def visit_EmptyStatement(empty)
  ";"
end

- (Object) visit_ExpressionStatement(statement)



20
21
22
# File 'lib/parsejs/visitor.rb', line 20

def visit_ExpressionStatement(statement)
  accept(statement.expression)
end

- (Object) visit_ForInStatement(statement)



160
161
162
163
164
165
166
# File 'lib/parsejs/visitor.rb', line 160

def visit_ForInStatement(statement)
  [
    accept(statement.left),
    accept(statement.right),
    accept(statement.body)
  ]
end

- (Object) visit_ForStatement(statement)



139
140
141
142
143
144
145
146
# File 'lib/parsejs/visitor.rb', line 139

def visit_ForStatement(statement)
  [
    accept(statement.init),
    accept(statement.test),
    accept(statement.update),
    accept(statement.body)
  ]
end

- (Object) visit_FunctionDeclaration(decl)



188
189
190
191
192
193
194
# File 'lib/parsejs/visitor.rb', line 188

def visit_FunctionDeclaration(decl)
  [
    decl.id && accept(decl.id),
    accept(decl.params),
    map(decl.body)
  ]
end

- (Object) visit_FunctionExpression(expr)



200
201
202
# File 'lib/parsejs/visitor.rb', line 200

def visit_FunctionExpression(expr)
  visit_FunctionDeclaration(expr)
end

- (Object) visit_Identifier(id)



63
64
65
# File 'lib/parsejs/visitor.rb', line 63

def visit_Identifier(id)
  id.val
end

- (Object) visit_IfStatement(statement)



123
124
125
126
127
128
129
# File 'lib/parsejs/visitor.rb', line 123

def visit_IfStatement(statement)
  [
    accept(statement.test),
    accept(statement.consequent),
    statement.alternate && accept(statement.alternate)
  ]
end

- (Object) visit_Literal(literal)



28
29
30
31
32
33
34
35
36
37
# File 'lib/parsejs/visitor.rb', line 28

def visit_Literal(literal)
  case val = literal.val
  when nil
    "null"
  when ParseJS::AST::Node
    accept val
  else
    val
  end
end

- (Object) visit_LogicalExpression(expr)



115
116
117
# File 'lib/parsejs/visitor.rb', line 115

def visit_LogicalExpression(expr)
  visit_BinaryExpression(expr)
end

- (Object) visit_MemberExpression(expr)



103
104
105
# File 'lib/parsejs/visitor.rb', line 103

def visit_MemberExpression(expr)
  [accept(expr.object), accept(expr.property), expr.computed]
end

- (Object) visit_NewExpression(expr)



107
108
109
# File 'lib/parsejs/visitor.rb', line 107

def visit_NewExpression(expr)
  [accept(expr.callee), expr.args && map(expr.args)]
end

- (Object) visit_Number(number)



39
40
41
# File 'lib/parsejs/visitor.rb', line 39

def visit_Number(number)
  number.val
end

- (Object) visit_ObjectExpression(expr)



91
92
93
# File 'lib/parsejs/visitor.rb', line 91

def visit_ObjectExpression(expr)
  map(expr.properties)
end

- (Object) visit_ParameterList(params)



196
197
198
# File 'lib/parsejs/visitor.rb', line 196

def visit_ParameterList(params)
  map(params.list)
end

- (Object) visit_Program(program)



16
17
18
# File 'lib/parsejs/visitor.rb', line 16

def visit_Program(program)
  map(program.elements)
end

- (Object) visit_Property(prop)



95
96
97
# File 'lib/parsejs/visitor.rb', line 95

def visit_Property(prop)
  [map(prop.comments), accept(prop.key), accept(prop.value)]
end

- (Object) visit_RegExp(regex)



59
60
61
# File 'lib/parsejs/visitor.rb', line 59

def visit_RegExp(regex)
  [regex.body, regex.flags]
end

- (Object) visit_ReturnStatement(statement)



71
72
73
# File 'lib/parsejs/visitor.rb', line 71

def visit_ReturnStatement(statement)
  accept(statement.label) if statement.label
end

- (Object) visit_SequenceExpression(expression)



24
25
26
# File 'lib/parsejs/visitor.rb', line 24

def visit_SequenceExpression(expression)
  [expression.parens, map(expression.expressions)]
end

- (Object) visit_String(string)



43
44
45
# File 'lib/parsejs/visitor.rb', line 43

def visit_String(string)
  string.val
end

- (Object) visit_SwitchCase(switch)



172
173
174
# File 'lib/parsejs/visitor.rb', line 172

def visit_SwitchCase(switch)
  [switch.test && accept(switch.test), map(switch.consequent)]
end

- (Object) visit_SwitchStatement(statement)



168
169
170
# File 'lib/parsejs/visitor.rb', line 168

def visit_SwitchStatement(statement)
  [accept(statement.discriminant), map(statement.cases)]
end

- (Object) visit_ThisExpression(statement)



47
48
49
# File 'lib/parsejs/visitor.rb', line 47

def visit_ThisExpression(statement)
  "this"
end

- (Object) visit_ThrowStatement(statement)



67
68
69
# File 'lib/parsejs/visitor.rb', line 67

def visit_ThrowStatement(statement)
  accept(statement.argument)
end

- (Object) visit_TryStatement(statement)



176
177
178
179
180
181
182
# File 'lib/parsejs/visitor.rb', line 176

def visit_TryStatement(statement)
  [
    statement.block && accept(statement.block),
    statement.handler && accept(statement.handler),
    statement.finalizer && accept(statement.finalizer)
  ]
end

- (Object) visit_UnaryExpression(unary)



75
76
77
# File 'lib/parsejs/visitor.rb', line 75

def visit_UnaryExpression(unary)
  [unary.op, accept(unary.argument)]
end

- (Object) visit_UpdateExpression(expr)



156
157
158
# File 'lib/parsejs/visitor.rb', line 156

def visit_UpdateExpression(expr)
  [expr.op, expr.prefix, accept(expr.argument)]
end

- (Object) visit_VariableDeclaration(decl)



148
149
150
# File 'lib/parsejs/visitor.rb', line 148

def visit_VariableDeclaration(decl)
  [decl.kind, map(decl.declarations), decl.semicolon]
end

- (Object) visit_VariableDeclarator(decl)



152
153
154
# File 'lib/parsejs/visitor.rb', line 152

def visit_VariableDeclarator(decl)
  [accept(decl.id), decl.init && accept(decl.init)]
end

- (Object) visit_WhileStatement(statement)



131
132
133
# File 'lib/parsejs/visitor.rb', line 131

def visit_WhileStatement(statement)
  [accept(statement.test), accept(statement.body)]
end

- (Object) visit_WithStatement(statement)



220
221
222
# File 'lib/parsejs/visitor.rb', line 220

def visit_WithStatement(statement)
  [accept(statement.object), accept(statement.body)]
end