-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
224 lines (205 loc) · 8.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// returns true if selector matches the given esprima node
// example selectors:
// *
// statement.return
// expression.identifier
// program declaration.function > block
// statement.return > expression
// expression.literal, expression.identifier
function test(selectorString, node) {
// TODO: complain about syntax errors
var alternates = selectorString.split(",");
return alternates.some(function (selectorString) {
var selector = splitSelector(selectorString + '>');
return matchUpward(selector, node);
});
}
// accepts an array of objects that look like this:
// { selector: "statement.return", callback: function (node) { } }
// returns a function that takes a node and calls the callbacks that have matching selectors.
// useful as a falafel callback.
function tester(selectors) {
return function (node) {
var self = this, args = Array.prototype.slice.apply(arguments);
selectors.forEach(function (s) {
if (test(s.selector, node)) {
s.callback.apply(self, args);
}
});
};
}
function splitSelector(selectorString) {
var regexp = /([\.a-z]+)(?:\s*(>))?/ig;
var m, pieces = [];
while (m = regexp.exec(selectorString)) {
var selector = {};
var namePieces = m[1].split('.');
if (namePieces[0].length > 0) selector.name = namePieces[0];
if (namePieces.length > 1) selector.classes = namePieces.slice(1);
if (m[2]) {
selector.directDescendant = true;
}
pieces.push(selector);
}
return pieces;
}
function matchUpward(selector, node) {
if (selector.length === 0) {
return true;
}
if (!node) {
return false;
}
var last = selector[selector.length - 1];
var tag = nodeTag(node);
if (tag && isMatch(tag, last)) {
return matchUpward(selector.slice(0, selector.length - 1), node.parent);
} else if (!last.directDescendant) {
return matchUpward(selector, node.parent);
} else {
return false;
}
}
function nodeTag(node) {
if (node.type === 'Identifier') {
var exprTag = { name: 'expression', classes: ['identifier'] };
switch (node.parent.type) {
case 'VariableDeclarator': if (node.parent.init === node) return decorate(exprTag); break;
case 'AssignmentExpression': if (node.parent.right === node) return decorate(exprTag); break;
case 'MemberExpression': if ((node.parent.object === node) || (node.parent.computed && node.parent.property === node)) return decorate(exprTag); break;
case 'Property': if (node.parent.value === node) return decorate(exprTag); break;
case 'ArrayExpression': return decorate(exprTag);
case 'CallExpression': return decorate(exprTag);
case 'BinaryExpression': return decorate(exprTag);
case 'ConditionalExpression': return decorate(exprTag);
case 'ReturnStatement': return decorate(exprTag);
case 'UnaryExpression': return decorate(exprTag);
case 'NewExpression': return decorate(exprTag);
case 'LogicalExpression': return decorate(exprTag);
case 'UpdateExpression': return decorate(exprTag);
case 'SequenceExpression': return decorate(exprTag);
case 'CatchClause': return decorate(exprTag);
case 'ExpressionStatement': return decorate(exprTag);
case 'IfStatement': return decorate(exprTag);
case 'SwitchStatement': return decorate(exprTag);
case 'ForStatement': return decorate(exprTag);
case 'ForInStatement': return decorate(exprTag);
case 'WhileStatement': return decorate(exprTag);
case 'DoWhileStatement': return decorate(exprTag);
case 'ThrowStatement': return decorate(exprTag);
case 'FunctionDeclaration': break;
case 'FunctionExpression': break;
case 'LabeledStatement': break;
case 'BreakStatement': break;
default: throw new Error('unrecognized identifier parent ' + node.parent.type);
}
return undefined;
} else if (node.type === 'Literal') {
return decorate({ name: 'expression', classes: ['literal'] });
} else if (node.type === 'ThisExpression') {
return decorate({ name: 'expression', classes: ['this'] });
} else if (node.type === 'CallExpression') {
return decorate({ name: 'expression', classes: ['call'] });
} else if (node.type === 'UnaryExpression') {
return decorate({ name: 'expression', classes: ['unary'] });
} else if (node.type === 'UpdateExpression') {
return decorate({ name: 'expression', classes: ['update'] });
} else if (node.type === 'BinaryExpression') {
return decorate({ name: 'expression', classes: ['binary'] });
} else if (node.type === 'ArrayExpression') {
return decorate({ name: 'expression', classes: ['array'] });
} else if (node.type === 'AssignmentExpression') {
return decorate({ name: 'expression', classes: ['assignment'] });
} else if (node.type === 'MemberExpression') {
return decorate({ name: 'expression', classes: ['member'] });
} else if (node.type === 'LogicalExpression') {
return decorate({ name: 'expression', classes: ['logical'] });
} else if (node.type === 'ConditionalExpression') {
return decorate({ name: 'expression', classes: ['ternary'] });
} else if (node.type === 'SequenceExpression') {
return decorate({ name: 'expression', classes: ['comma'] });
} else if (node.type === 'ObjectExpression') {
return decorate({ name: 'expression', classes: ['object'] });
} else if (node.type === 'NewExpression') {
return decorate({ name: 'expression', classes: ['new'] });
} else if (node.type === 'FunctionExpression') {
return decorate({ name: 'expression', classes: ['function'] });
} else if (node.type === 'ReturnStatement') {
return decorate({ name: 'statement', classes: ['return'] });
} else if (node.type === 'BreakStatement') {
return decorate({ name: 'statement', classes: ['break'] });
} else if (node.type === 'ContinueStatement') {
return decorate({ name: 'statement', classes: ['break'] });
} else if (node.type === 'LabeledStatement') {
return decorate({ name: 'statement', classes: ['label'] });
} else if (node.type === 'ExpressionStatement') {
return decorate({ name: 'statement', classes: ['expression'] });
} else if (node.type === 'IfStatement') {
return decorate({ name: 'statement', classes: ['if'] });
} else if (node.type === 'WhileStatement') {
return decorate({ name: 'statement', classes: ['while'] });
} else if (node.type === 'DoWhileStatement') {
return decorate({ name: 'statement', classes: ['do-while'] });
} else if (node.type === 'ThrowStatement') {
return decorate({ name: 'statement', classes: ['throw'] });
} else if (node.type === 'TryStatement') {
return decorate({ name: 'statement', classes: ['try'] });
} else if (node.type === 'CatchClause') {
return decorate({ name: 'clause', classes: ['catch'] });
} else if (node.type === 'ForStatement') {
return decorate({ name: 'statement', classes: ['for'] });
} else if (node.type === 'ForInStatement') {
return decorate({ name: 'statement', classes: ['forin'] });
} else if (node.type === 'SwitchStatement') {
return decorate({ name: 'statement', classes: ['switch'] });
} else if (node.type === 'DebuggerStatement') {
return decorate({ name: 'statement', classes: ['debugger'] });
} else if (node.type === 'BlockStatement') {
return decorate({ name: 'block', classes: [] });
} else if (node.type === 'EmptyStatement') {
return undefined;
} else if (node.type === 'VariableDeclaration') {
return decorate({ name: 'declaration', classes: ['variable'] });
} else if (node.type === 'FunctionDeclaration') {
return decorate({ name: 'declaration', classes: ['function'] });
} else if (node.type === 'VariableDeclarator') {
return decorate({ name: 'declarator', classes: [] });
} else if (node.type === 'Property') {
return decorate({ name: 'property', classes: [] });
} else if (node.type === 'SwitchCase') {
return decorate({ name: 'switch-case', classes: [] });
} else if (node.type === 'Program') {
return decorate({ name: 'program', classes: [] });
}
// console.error(node);
// console.error(node.source());
throw new Error('tag not found for ' + node.type);
function decorate(tag) {
if (node.parent) {
if (node.parent.type === 'IfStatement') {
if (node.parent.consequent === node) {
tag.classes.push('branch', 'consequent');
} else if (node.parent.alternate === node) {
tag.classes.push('branch', 'alternate');
}
}
}
return tag;
}
}
function isMatch(tag, selector) {
if (('name' in selector) && selector.name !== '*' && tag.name !== selector.name) {
return false;
}
if (('classes' in selector)) {
for (var i in selector.classes) {
if (tag.classes.indexOf(selector.classes[i]) === -1) {
return false;
}
}
}
return true;
}
module.exports = test;
module.exports.tester = tester;
module.exports.nodeTag = nodeTag;