-
Notifications
You must be signed in to change notification settings - Fork 6
/
inject.js
79 lines (63 loc) · 2.05 KB
/
inject.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
/* global define: false, module: false */
(function (mod) {
'use strict';
if (typeof module === 'object' && module.exports) { // CommonJS
module.exports = mod();
return;
}
if (typeof define === 'function' && define.amd) { // AMD
define(mod);
return;
}
var global = Function('return this;')(); // eslint-disable-line no-new-func
global.acornJSXWalk = mod(); // Global
}(function () {
'use strict';
var ignore = function () {};
var inject = function (visitor) {
visitor.JSXIdentifier = ignore;
visitor.JSXMemberExpression = function (node, st, c) {
c(node.object, st);
// JSX does not have computed properties, and the default MemberExpression
// vistor only visits computed properties, so don't visit any JSX
// properties either.
};
// I'm not entirely sure how to walk this, but traversing its children just
// doesn't feel quite "right"...
visitor.JSXNamespacedName = ignore;
visitor.JSXEmptyExpression = ignore;
visitor.JSXExpressionContainer = function (node, st, c) {
c(node.expression, st);
};
visitor.JSXOpeningElement = function (node, st, c) {
c(node.name, st);
for (var i = 0; i < node.attributes.length; i += 1) {
c(node.attributes[i], st);
}
};
visitor.JSXClosingElement = function (node, st, c) {
c(node.name, st);
};
visitor.JSXAttribute = function (node, st, c) {
// The default Property visitor only visits computed Property key nodes,
// so behave similarly here: There are no computed JSXAttribute name
// nodes, so don't traverse them.
if (node.value) {
c(node.value, st);
}
};
visitor.JSXSpreadAttribute = function (node, st, c) {
c(node.argument, st);
};
visitor.JSXElement = function (node, st, c) {
c(node.openingElement, st);
for (var i = 0; i < node.children.length; i += 1) {
c(node.children[i], st);
}
if (node.closingElement) {
c(node.closingElement, st);
}
};
};
return inject;
}));