This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
forked from atom/symbols-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert-async-plugin.js
67 lines (57 loc) · 2.24 KB
/
assert-async-plugin.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
module.exports = function({types: t, template}) {
const generator = template(`
UNTIL_FUNC(fail => {
try {
ASSERT_CALL;
return true;
} catch (err) {
return fail(err);
}
}, UNTIL_ARGS)
`);
return {
name: 'assert-async',
visitor: {
CallExpression(path, state) {
// assuming `assert.async.something(arg1, arg2)`
// assert.async.something
const callee = path.node.callee;
if (!t.isMemberExpression(callee)) { return; }
// assert.async
let calleeObject = callee.object;
let asyncArgs = [];
// support `assert.async(200).something(arg1, arg2)` for setting timeout
// in this case, `calleeObject` is actually `assert.async(200)` not `assert.async`
if (t.isCallExpression(calleeObject)) {
asyncArgs = calleeObject.arguments; // [200]
calleeObject = calleeObject.callee; // assert.async
}
if (!t.isMemberExpression(calleeObject)) { return; }
const calleeObjectObject = calleeObject.object;
const calleeObjectProperty = calleeObject.property;
if (!t.isIdentifier(calleeObjectObject) || calleeObjectObject.name !== 'assert') { return; }
if (!t.isIdentifier(calleeObjectProperty) || calleeObjectProperty.name !== 'async') { return; }
// path === assert.async.something(arg1, arg2)
// [arg1, arg2]
const callArgs = path.node.arguments;
// "assert"
const assert = calleeObject.object.name;
// "something"
const meth = callee.property.name;
if (t.isExpressionStatement(path.parent)) {
const error = `${assert}.async.${meth} can not be used as a statement (did you forget to 'await' it?)`;
throw path.buildCodeFrameError(error);
}
// assert.something
const assertDotMethod = t.memberExpression(t.identifier(assert), t.identifier(meth));
// assert.something(arg1, arg2)
const assertExpression = t.callExpression(assertDotMethod, callArgs);
path.replaceWith(generator({
ASSERT_CALL: assertExpression,
UNTIL_ARGS: asyncArgs,
UNTIL_FUNC: state.addImport('test-until', 'default', 'until'),
}));
},
},
};
};