/
no-test-callback.js
80 lines (67 loc) · 2.32 KB
/
no-test-callback.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
'use strict';
const getDocsUrl = require('./util').getDocsUrl;
const isTestCase = require('./util').isTestCase;
module.exports = {
meta: {
docs: {
url: getDocsUrl(__filename),
},
fixable: 'code',
},
create(context) {
return {
CallExpression(node) {
if (!isTestCase(node) || node.arguments.length !== 2) {
return;
}
const callback = node.arguments[1];
if (
!/^(Arrow)?FunctionExpression$/.test(callback.type) ||
callback.params.length !== 1
) {
return;
}
const argument = callback.params[0];
context.report({
node: argument,
message: 'Illegal usage of test callback',
fix(fixer) {
const sourceCode = context.getSourceCode();
const body = callback.body;
const firstBodyToken = sourceCode.getFirstToken(body);
const lastBodyToken = sourceCode.getLastToken(body);
const tokenBeforeArgument = sourceCode.getTokenBefore(argument);
const tokenAfterArgument = sourceCode.getTokenAfter(argument);
const argumentInParens =
tokenBeforeArgument.value === '(' &&
tokenAfterArgument.value === ')';
let argumentFix = fixer.replaceText(argument, '()');
if (argumentInParens) {
argumentFix = fixer.remove(argument);
}
let newCallback = argument.name;
if (argumentInParens) {
newCallback = `(${newCallback})`;
}
let beforeReplacement = `new Promise(${newCallback} => `;
let afterReplacement = ')';
let replaceBefore = true;
if (body.type === 'BlockStatement') {
const keyword = callback.async ? 'await' : 'return';
beforeReplacement = `${keyword} ${beforeReplacement}{`;
afterReplacement += '}';
replaceBefore = false;
}
return [
argumentFix,
replaceBefore
? fixer.insertTextBefore(firstBodyToken, beforeReplacement)
: fixer.insertTextAfter(firstBodyToken, beforeReplacement),
fixer.insertTextAfter(lastBodyToken, afterReplacement),
];
},
});
},
};
},
};