11
11
12
12
const EventEmitter = require ( "events" )
13
13
const NodeEventGenerator = require ( "eslint/lib/util/node-event-generator" )
14
+ const traverseNodes = require ( "./traverse-nodes" )
14
15
15
16
//------------------------------------------------------------------------------
16
17
// Helpers
17
18
//------------------------------------------------------------------------------
18
19
19
20
const emitters = new WeakMap ( )
20
- const KEYS = {
21
- AssignmentExpression : [ "left" , "right" ] ,
22
- AssignmentPattern : [ "left" , "right" ] ,
23
- ArrayExpression : [ "elements" ] ,
24
- ArrayPattern : [ "elements" ] ,
25
- ArrowFunctionExpression : [ "params" , "body" ] ,
26
- AwaitExpression : [ "argument" ] ,
27
- BlockStatement : [ "body" ] ,
28
- BinaryExpression : [ "left" , "right" ] ,
29
- BreakStatement : [ "label" ] ,
30
- CallExpression : [ "callee" , "arguments" ] ,
31
- CatchClause : [ "param" , "body" ] ,
32
- ClassBody : [ "body" ] ,
33
- ClassDeclaration : [ "id" , "superClass" , "body" ] ,
34
- ClassExpression : [ "id" , "superClass" , "body" ] ,
35
- ConditionalExpression : [ "test" , "consequent" , "alternate" ] ,
36
- ContinueStatement : [ "label" ] ,
37
- DebuggerStatement : [ ] ,
38
- DirectiveStatement : [ ] ,
39
- DoWhileStatement : [ "body" , "test" ] ,
40
- EmptyStatement : [ ] ,
41
- ExportAllDeclaration : [ "source" ] ,
42
- ExportDefaultDeclaration : [ "declaration" ] ,
43
- ExportNamedDeclaration : [ "declaration" , "specifiers" , "source" ] ,
44
- ExportSpecifier : [ "exported" , "local" ] ,
45
- ExpressionStatement : [ "expression" ] ,
46
- ForStatement : [ "init" , "test" , "update" , "body" ] ,
47
- ForInStatement : [ "left" , "right" , "body" ] ,
48
- ForOfStatement : [ "left" , "right" , "body" ] ,
49
- FunctionDeclaration : [ "id" , "params" , "body" ] ,
50
- FunctionExpression : [ "id" , "params" , "body" ] ,
51
- Identifier : [ ] ,
52
- IfStatement : [ "test" , "consequent" , "alternate" ] ,
53
- ImportDeclaration : [ "specifiers" , "source" ] ,
54
- ImportDefaultSpecifier : [ "local" ] ,
55
- ImportNamespaceSpecifier : [ "local" ] ,
56
- ImportSpecifier : [ "imported" , "local" ] ,
57
- Literal : [ ] ,
58
- LabeledStatement : [ "label" , "body" ] ,
59
- LogicalExpression : [ "left" , "right" ] ,
60
- MemberExpression : [ "object" , "property" ] ,
61
- MetaProperty : [ "meta" , "property" ] ,
62
- MethodDefinition : [ "key" , "value" ] ,
63
- ModuleSpecifier : [ ] ,
64
- NewExpression : [ "callee" , "arguments" ] ,
65
- ObjectExpression : [ "properties" ] ,
66
- ObjectPattern : [ "properties" ] ,
67
- Program : [ "body" ] ,
68
- Property : [ "key" , "value" ] ,
69
- RestElement : [ "argument" ] ,
70
- ReturnStatement : [ "argument" ] ,
71
- SequenceExpression : [ "expressions" ] ,
72
- SpreadElement : [ "argument" ] ,
73
- Super : [ ] ,
74
- SwitchStatement : [ "discriminant" , "cases" ] ,
75
- SwitchCase : [ "test" , "consequent" ] ,
76
- TaggedTemplateExpression : [ "tag" , "quasi" ] ,
77
- TemplateElement : [ ] ,
78
- TemplateLiteral : [ "quasis" , "expressions" ] ,
79
- ThisExpression : [ ] ,
80
- ThrowStatement : [ "argument" ] ,
81
- TryStatement : [ "block" , "handler" , "finalizer" ] ,
82
- UnaryExpression : [ "argument" ] ,
83
- UpdateExpression : [ "argument" ] ,
84
- VariableDeclaration : [ "declarations" ] ,
85
- VariableDeclarator : [ "id" , "init" ] ,
86
- WhileStatement : [ "test" , "body" ] ,
87
- WithStatement : [ "object" , "body" ] ,
88
- YieldExpression : [ "argument" ] ,
89
-
90
- VIdentifier : [ ] ,
91
- VText : [ ] ,
92
- VExpressionContainer : [ "expression" ] ,
93
- VDirectiveKey : [ ] ,
94
- VAttributeValue : [ ] ,
95
- VAttribute : [ "key" , "value" ] ,
96
- VStartTag : [ "id" , "attributes" ] ,
97
- VEndTag : [ "id" ] ,
98
- VElement : [ "startTag" , "children" , "endTag" ] ,
99
- }
100
-
101
- /**
102
- * Get the keys of the given node to traverse it.
103
- * @param {ASTNode } node The node to get.
104
- * @returns {string[] } The keys to traverse.
105
- */
106
- function fallback ( node ) {
107
- return Object . keys ( node ) . filter ( k =>
108
- k !== "parent" &&
109
- k !== "leadingComments" &&
110
- k !== "trailingComments" &&
111
- node [ k ] !== null &&
112
- typeof node [ k ] === "object"
113
- )
114
- }
115
-
116
- /**
117
- * Traverse the given node.
118
- * `NodeEventGenerator` supports AST selectors!
119
- * @param {ASTNode } node The node to traverse.
120
- * @param {NodeEventGenerator } generator The event generator.
121
- * @returns {void }
122
- */
123
- function traverse ( node , generator ) {
124
- let i = 0
125
- let j = 0
126
-
127
- generator . enterNode ( node )
128
-
129
- const keys = KEYS [ node . type ] || fallback ( node )
130
- for ( i = 0 ; i < keys . length ; ++ i ) {
131
- const child = node [ keys [ i ] ]
132
-
133
- if ( Array . isArray ( child ) ) {
134
- for ( j = 0 ; j < child . length ; ++ j ) {
135
- if ( child [ j ] ) {
136
- traverse ( child [ j ] , generator )
137
- }
138
- }
139
- }
140
- else if ( child ) {
141
- traverse ( child , generator )
142
- }
143
- }
144
-
145
- generator . leaveNode ( node )
146
- }
147
21
148
22
/**
149
23
* Get or create the event emitter to traverse.
@@ -162,7 +36,7 @@ function ensureEmitter(context) {
162
36
context . eslint . on ( "Program:exit" , ( node ) => {
163
37
if ( node . templateBody != null ) {
164
38
const generator = new NodeEventGenerator ( emitter )
165
- traverse ( node . templateBody , generator )
39
+ traverseNodes ( node . templateBody , generator )
166
40
}
167
41
} )
168
42
@@ -180,4 +54,3 @@ module.exports = (context, visitor) => {
180
54
emitter . on ( selector , visitor [ selector ] )
181
55
}
182
56
}
183
- module . exports . traverse = traverse
0 commit comments