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 | 1x
1x
1x
1x
33x
93x
93x
29x
16x
1x
1x
15x
7x
8x
3x
9x
5x
3x
2x
16x
16x
16x
16x
8x
2x
6x
2x
8x
8x
2x
8x
2x
8x
29x
2x
27x
27x
33x
8x
25x
2x
23x
15x
15x
15x
15x
15x
16x
2x
1x
3x
3x
2x
1x
36x
12x
1x
12x
54x
4x
2x
4x
3x
3x
| module.exports = function (babel) {
const { types: t } = babel
const logCallee = t.memberExpression(
t.identifier('console'),
t.identifier('log'),
false
)
const createGroupCallee = (end, collapsed = true) =>
t.memberExpression(
t.identifier('console'),
t.identifier(`group${end ? 'End' : collapsed ? 'Collapsed' : ''}`),
false
)
function getComments (node) {
return (node && node.leadingComments) || []
}
function hasSitrepComments (comments, label = 'sitrep') {
return comments.some(c => c.value.trim() === label)
}
function createLogStatement (thing) {
return t.callExpression(
logCallee,
thing.name && thing.name.indexOf('returnValue') > -1
? [t.stringLiteral('Return Value:'), thing]
: thing.name ? [t.stringLiteral(`${thing.name}: `), thing] : [thing]
)
}
function getName (path) {
if (path.parentPath.isClassProperty()) {
Eif (
path.parentPath.node.key &&
t.isIdentifier(path.parentPath.node.key)
) {
return path.parentPath.node.key.name
}
}
if (path.node.id && path.node.id.name) {
return path.node.id.name
}
if (path.node.key && t.isIdentifier(path.node.key)) {
return path.node.key.name
}
const variableParent = path.findParent(p => p.isVariableDeclarator())
if (variableParent && t.isIdentifier(variableParent.node.id)) {
return variableParent.node.id.name
}
return `function: ${path.getSource().split('\n')[0]}`
}
function functionVisitor (functionPath, state) {
let name = getName(functionPath)
functionPath
.get('body')
.unshiftContainer(
'body',
t.expressionStatement(
t.callExpression(createGroupCallee(false, state.opts.collapsed), [
t.stringLiteral(name)
])
)
)
let didWriteGroupEnd = false
functionPath.traverse({
AssignmentExpression (path) {
if (t.isPattern(path.node.left)) {
return
}
path.insertAfter(
t.expressionStatement(createLogStatement(path.node.left))
)
},
ArrayPattern (arrPatternPath) {
arrPatternPath.node.elements
.slice()
.reverse()
.forEach(element => {
let id = element
if (t.isAssignmentPattern(element)) {
id = element.left
}
arrPatternPath
.getStatementParent()
.insertAfter(
t.expressionStatement(
t.callExpression(logCallee, [t.stringLiteral(id.name), id])
)
)
})
},
ObjectPattern (objPatternPath) {
objPatternPath.node.properties
.slice()
.reverse()
.forEach(prop => {
objPatternPath
.getStatementParent()
.insertAfter(
t.expressionStatement(
t.callExpression(logCallee, [
t.isIdentifier(prop.value)
? t.stringLiteral(prop.value.name)
: t.stringLiteral(prop.key.name),
t.isIdentifier(prop.value) ? prop.value : prop.key
])
)
)
})
},
VariableDeclaration (variableDeclPath) {
if (!variableDeclPath.parentPath.isBlockStatement()) {
return
}
const decls = variableDeclPath.node.declarations
decls.forEach(dec => {
if (!dec.init) {
return
}
if (t.isPattern(dec.id)) {
return
}
variableDeclPath.insertAfter(
t.expressionStatement(createLogStatement(dec.id))
)
})
},
ReturnStatement (returnPath) {
const id = returnPath.scope.generateUidIdentifier('returnValue')
returnPath.insertBefore(
t.variableDeclaration('var', [
t.variableDeclarator(id, returnPath.node.argument)
])
)
returnPath.insertBefore(
t.expressionStatement(
t.callExpression(createGroupCallee(true, state.opts.collapsed), [
t.stringLiteral(name)
])
)
)
didWriteGroupEnd = true
returnPath.node.argument = id
}
})
if (!didWriteGroupEnd) {
functionPath
.get('body')
.pushContainer(
'body',
t.expressionStatement(
t.callExpression(createGroupCallee(true, state.opts.collapsed), [
t.stringLiteral(name)
])
)
)
}
}
return {
name: 'babel-plugin-sitrep',
visitor: {
Class (path, state) {
path.traverse({
ClassProperty (path) {
if (hasSitrepComments(getComments(path.node), state.opts.label)) {
path.traverse({
Function (path) {
functionVisitor(path, state)
}
})
}
}
})
},
Function (path, state) {
if (hasSitrepComments(getComments(path.node), state.opts.label)) {
if (path.isArrowFunctionExpression()) {
path.arrowFunctionToShadowed()
}
functionVisitor(path, state)
}
},
VariableDeclarator (path, state) {
if (
hasSitrepComments(getComments(path.parentPath.node), state.opts.label)
) {
if (t.isArrowFunctionExpression(path.node.init)) {
path.get('init').arrowFunctionToShadowed()
}
if (t.isFunction(path.node.init)) {
path.traverse({
Function (path) {
functionVisitor(path, state)
}
})
}
}
}
}
}
}
|