OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @constructor | |
7 * @param {function(!ESTree.Node):(!Object|undefined)} beforeVisit | |
8 * @param {function(!ESTree.Node)=} afterVisit | |
9 */ | |
10 WebInspector.ESTreeWalker = function(beforeVisit, afterVisit) | |
11 { | |
12 this._beforeVisit = beforeVisit; | |
13 this._afterVisit = afterVisit || new Function(); | |
14 this._walkNulls = false; | |
15 } | |
16 | |
17 /** @typedef {!Object} WebInspector.ESTreeWalker.SkipSubtree */ | |
18 WebInspector.ESTreeWalker.SkipSubtree = {}; | |
19 | |
20 WebInspector.ESTreeWalker.prototype = { | |
21 /** | |
22 * @param {boolean} value | |
23 */ | |
24 setWalkNulls: function(value) | |
25 { | |
26 this._walkNulls = value; | |
27 }, | |
28 | |
29 /** | |
30 * @param {!ESTree.Node} ast | |
31 */ | |
32 walk: function(ast) | |
33 { | |
34 this._innerWalk(ast, null); | |
35 }, | |
36 | |
37 /** | |
38 * @param {!ESTree.Node} node | |
39 * @param {?ESTree.Node} parent | |
40 */ | |
41 _innerWalk: function(node, parent) | |
42 { | |
43 if (!node && parent && this._walkNulls) { | |
44 node = /** @type {!ESTree.Node} */ ({ | |
45 type: "Literal", | |
46 raw: "null", | |
47 value: null | |
48 }); | |
49 } | |
50 | |
51 if (!node) | |
52 return; | |
53 node.parent = parent; | |
54 | |
55 if (this._beforeVisit.call(null, node) === WebInspector.ESTreeWalker.Ski
pSubtree) { | |
56 this._afterVisit.call(null, node); | |
57 return; | |
58 } | |
59 | |
60 var walkOrder = WebInspector.ESTreeWalker._walkOrder[node.type]; | |
61 if (!walkOrder) { | |
62 console.error("Walk order not defined for " + node.type); | |
63 return; | |
64 } | |
65 | |
66 if (node.type === "TemplateLiteral") { | |
67 var templateLiteral = /** @type {!ESTree.TemplateLiteralNode} */ (no
de); | |
68 var expressionsLength = templateLiteral.expressions.length; | |
69 for (var i = 0; i < expressionsLength; ++i) { | |
70 this._innerWalk(templateLiteral.quasis[i], templateLiteral); | |
71 this._innerWalk(templateLiteral.expressions[i], templateLiteral)
; | |
72 } | |
73 this._innerWalk(templateLiteral.quasis[expressionsLength], templateL
iteral); | |
74 } else { | |
75 for (var i = 0; i < walkOrder.length; ++i) { | |
76 var entity = node[walkOrder[i]]; | |
77 if (Array.isArray(entity)) | |
78 this._walkArray(entity, node); | |
79 else | |
80 this._innerWalk(entity, node); | |
81 } | |
82 } | |
83 | |
84 this._afterVisit.call(null, node); | |
85 }, | |
86 | |
87 /** | |
88 * @param {!Array.<!ESTree.Node>} nodeArray | |
89 * @param {?ESTree.Node} parentNode | |
90 */ | |
91 _walkArray: function(nodeArray, parentNode) | |
92 { | |
93 for (var i = 0; i < nodeArray.length; ++i) | |
94 this._innerWalk(nodeArray[i], parentNode); | |
95 }, | |
96 } | |
97 | |
98 /** @enum {!Array.<string>} */ | |
99 WebInspector.ESTreeWalker._walkOrder = { | |
100 "ArrayExpression": ["elements"], | |
101 "ArrowFunctionExpression": ["params", "body"], | |
102 "AssignmentExpression": ["left", "right"], | |
103 "BinaryExpression": ["left", "right"], | |
104 "BlockStatement": ["body"], | |
105 "BreakStatement": ["label"], | |
106 "CallExpression": ["callee", "arguments"], | |
107 "CatchClause": ["param", "body"], | |
108 "ClassBody": ["body"], | |
109 "ClassDeclaration": ["id", "superClass", "body"], | |
110 "ClassExpression": ["id", "superClass", "body"], | |
111 "ConditionalExpression": ["test", "consequent", "alternate"], | |
112 "ContinueStatement": ["label"], | |
113 "DebuggerStatement": [], | |
114 "DoWhileStatement": ["body", "test"], | |
115 "EmptyStatement": [], | |
116 "ExpressionStatement": ["expression"], | |
117 "ForInStatement": ["left", "right", "body"], | |
118 "ForOfStatement": ["left", "right", "body"], | |
119 "ForStatement": ["init", "test", "update", "body"], | |
120 "FunctionDeclaration": ["id", "params", "body"], | |
121 "FunctionExpression": ["id", "params", "body"], | |
122 "Identifier": [], | |
123 "IfStatement": ["test", "consequent", "alternate"], | |
124 "LabeledStatement": ["label", "body"], | |
125 "Literal": [], | |
126 "LogicalExpression": ["left", "right"], | |
127 "MemberExpression": ["object", "property"], | |
128 "MethodDefinition": ["key", "value"], | |
129 "NewExpression": ["callee", "arguments"], | |
130 "ObjectExpression": ["properties"], | |
131 "Program": ["body"], | |
132 "Property": ["key", "value"], | |
133 "ReturnStatement": ["argument"], | |
134 "SequenceExpression": ["expressions"], | |
135 "Super": [], | |
136 "SwitchCase": ["test", "consequent"], | |
137 "SwitchStatement": ["discriminant", "cases"], | |
138 "TaggedTemplateExpression": ["tag", "quasi"], | |
139 "TemplateElement": [], | |
140 "TemplateLiteral": ["quasis", "expressions"], | |
141 "ThisExpression": [], | |
142 "ThrowStatement": ["argument"], | |
143 "TryStatement": ["block", "handler", "finalizer"], | |
144 "UnaryExpression": ["argument"], | |
145 "UpdateExpression": ["argument"], | |
146 "VariableDeclaration": ["declarations"], | |
147 "VariableDeclarator": ["id", "init"], | |
148 "WhileStatement": ["test", "body"], | |
149 "WithStatement": ["object", "body"], | |
150 "YieldExpression": ["argument"] | |
151 } | |
OLD | NEW |