-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
87 lines (67 loc) · 2.85 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
const { parse } = require('@babel/parser');
const innerVariable = '__cypressSyncVar__';
const thenifyName = "thenify";
module.exports = function plugin({types}) {
let expName
function processCyExpression(statement, path, cyExprNodePath) {
const cyexpr = cyExprNodePath.node
const nextNodes = statement.getAllNextSiblings().map(n => n.node);
const newNode = statement.getSource().replace(path.getSource(), innerVariable)
const parsedNode = parse(newNode)
nextNodes.unshift(parsedNode.program.body[0])
const body = types.blockStatement(nextNodes)
const param = types.identifier(innerVariable)
const callback = types.arrowFunctionExpression([param], body)
const member = types.memberExpression(cyexpr, types.identifier("then"))
cyExprNodePath.replaceWith(types.identifier(innerVariable))
const full = types.expressionStatement(types.callExpression(member, [callback]))
statement.replaceWith(full)
statement.getAllNextSiblings().forEach(s => s.remove())
}
return {
name: 'cyThenify',
visitor: {
Identifier: {
exit(path, state) {
if (state.opts.total_thenify !== 'true') return
if (path.node.name !== 'cy') return
if (path.node.start === undefined) return // do not process newly added cy expressions
const statement = path.getStatementParent();
if (!statement) return
if (types.isVariableDeclaration(statement)) {
let callExpr = path.find(p => types.isCallExpression(p) && types.isVariableDeclarator(p.parentPath))
if (callExpr != null) {
processCyExpression(statement, callExpr, callExpr)
return
}
}
const nextNodes = statement.getAllNextSiblings().map(n => n.node);
const body = types.blockStatement(nextNodes)
const callback = types.arrowFunctionExpression([], body)
const member = types.memberExpression(types.identifier('cy'), types.identifier("then"))
const full = types.expressionStatement(types.callExpression(member, [callback]))
statement.insertAfter(full)
const allNextSiblings = statement.getAllNextSiblings();
allNextSiblings.shift()
allNextSiblings.forEach(s => s.remove())
}
},
CallExpression: {
exit(path, state) {
const callee = path.node.callee
const property = callee.property;
if (!property) return
const funName = property.name;
if (!funName) return
if (!expName) {
expName = state.opts.thenify_function_name || thenifyName
}
if (funName !== expName) return
const statement = path.getStatementParent()
if (!statement) return
processCyExpression(statement, path, path.get("callee").get("object"));
}
},
}
}
}