-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
192 lines (155 loc) · 4.31 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
const t = require('babel-types');
const SPACE_REGEX = /\s+/;
const BlockVisitor = function (path, args) {
const body = path.node.body || [];
for (let i = 0, len = body.length; i < len; i++) {
const node = body[i];
if (node) {
const leadingComments = node.leadingComments || [];
const trailingComments = node.trailingComments || [];
const allComments = leadingComments.concat(trailingComments);
if (allComments.length) {
for (var j = 0, length = allComments.length; j < length; j++) {
const cNode = allComments[j];
if (cNode.type === 'CommentLine') {
const value = (cNode.value || '').replace(SPACE_REGEX, '');
if (value === 'profile') {
args.state.gotProfileComment = true;
args.state.path = path;
}
return;
}
}
}
}
}
}
const generateProfileStart = functionLabel => {
const args = functionLabel ? [ t.stringLiteral(functionLabel) ] : [];
return t.expressionStatement(
t.callExpression(
t.memberExpression(t.identifier('console'), t.identifier('profile')),
args
)
);
}
const generateProfileEnd = () =>
t.expressionStatement(
t.callExpression(
t.memberExpression(t.identifier('console'), t.identifier('profileEnd')),
[]
)
);
const generateIdentifier = () => t.identifier('_babel_temp_alias');
const assignArgument = (identifier, argument, opts) => {
if (opts.blockDepth === 1) {
return t.variableDeclaration(
'let',
[
t.variableDeclarator(
t.identifier('_babel_temp_alias'),
argument
)
]
);
} else {
return t.expressionStatement(
t.assignmentExpression(
"=",
t.identifier('_babel_temp_alias'),
argument
)
);
}
}
const SkipVisitor = function (path, args) {
path.skip();
return;
}
const ReturnVisitor = {
ReturnStatement: function (path, args) {
const argument = path.node.argument;
args.state.gotReturn = true;
args.state.blockDepth++;
if (args.state.gotProfileComment) {
if (!argument || argument.type === 'Literal' || argument.type === 'NumericLiteral' || argument.type === 'StringLiteral' || argument.type === 'Identifier') {
// This is the simplest possible case. We don't need to alias anything
path.insertBefore(generateProfileEnd());
return;
} else {
// assign the argument to some variable
// add profile end statement
// set the argument of return to the variable identifier
const identifier = generateIdentifier();
const assignment = assignArgument(identifier, argument, {
blockDepth: args.state.blockDepth
});
path.insertBefore(assignment);
path.insertBefore(generateProfileEnd());
path.node.argument = identifier;
return;
}
}
},
FunctionDeclaration: SkipVisitor,
ArrowFunctionExpression: SkipVisitor,
ClassMethod: SkipVisitor,
FunctionExpression: SkipVisitor
}
const isPathTypeAFunction = p =>
p.isFunctionDeclaration() ||
p.isArrowFunctionExpression() ||
p.isClassMethod() ||
p.isFunctionExpression();
const getName = path => {
const fParent = path.findParent(p => isPathTypeAFunction(p));
let name;
switch (fParent.type) {
case 'FunctionDeclaration':
name = fParent.node.id.name;
break;
case 'FunctionExpression':
case 'ArrowFunctionExpression':
if (fParent.parent.id && fParent.parent.id.name) {
name = fParent.parent.id.name
}
if (fParent.node.id && fParent.node.id.name) {
name = fParent.node.id.name
}
if (fParent.parent.left && fParent.parent.left.property && fParent.parent.left.property.name) {
name = fParent.parent.left.property.name;
}
break;
case 'ClassMethod':
name = fParent.node.key.name;
}
return name;
}
const ConsoleProfileVisitor = function (babel) {
return {
visitor: {
// ArrowFunctionExpression: FunctionVisitor
BlockStatement: function (path, args) {
let state = {
gotProfileComment: false,
addedProfileComment: false,
gotReturn: false,
blockDepth: 0
};
BlockVisitor(path, { state });
if (state.gotProfileComment) {
path.unshiftContainer(
'body',
generateProfileStart(getName(path))
);
state.addedProfileComment = true;
}
path.traverse(ReturnVisitor, { state });
if (state.addedProfileComment && !state.gotReturn) {
path.pushContainer('body', generateProfileEnd());
}
}
}
}
}
module.exports = ConsoleProfileVisitor;