-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathuse-true-false.js
98 lines (88 loc) · 2.31 KB
/
use-true-false.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
'use strict';
const {isDeepStrictEqual} = require('node:util');
const espree = require('espree');
const espurify = require('espurify');
const {visitIf} = require('enhance-visitors');
const util = require('../util');
const createAvaRule = require('../create-ava-rule');
const booleanBinaryOperators = new Set([
'==',
'===',
'!=',
'!==',
'<',
'<=',
'>',
'>=',
]);
const knownBooleanSignatures = [
'isFinite()',
'isNaN()',
'Object.is()',
'Object.isExtensible()',
'Object.isFrozen()',
'Object.isSealed()',
'Boolean()',
'Number.isNaN()',
'Number.isFinite()',
'Number.isInteger()',
'Number.isSafeInteger()',
'Array.isArray()',
'ArrayBuffer.isView()',
'SharedArrayBuffer.isView()',
'Reflect.has()',
'Reflect.isExtensible()',
].map(signature => espurify(espree.parse(signature).body[0].expression.callee));
function matchesKnownBooleanExpression(argument) {
if (argument.type !== 'CallExpression') {
return false;
}
const callee = espurify(argument.callee);
return knownBooleanSignatures.some(signature => isDeepStrictEqual(callee, signature));
}
const create = context => {
const ava = createAvaRule();
return ava.merge({
CallExpression: visitIf([
ava.isInTestFile,
ava.isInTestNode,
])(node => {
if (
node.callee.type === 'MemberExpression'
&& (node.callee.property.name === 'truthy' || node.callee.property.name === 'falsy')
&& node.callee.object.name === 't'
) {
const argument = node.arguments[0];
if (argument
&& ((argument.type === 'BinaryExpression' && booleanBinaryOperators.has(argument.operator))
|| (argument.type === 'UnaryExpression' && argument.operator === '!')
|| (argument.type === 'Literal' && argument.value === Boolean(argument.value))
|| (matchesKnownBooleanExpression(argument)))
) {
if (node.callee.property.name === 'falsy') {
context.report({
node,
message: '`t.false()` should be used instead of `t.falsy()`.',
});
} else {
context.report({
node,
message: '`t.true()` should be used instead of `t.truthy()`.',
});
}
}
}
}),
});
};
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Ensure that `t.true()`/`t.false()` are used instead of `t.truthy()`/`t.falsy()`.',
url: util.getDocsUrl(__filename),
},
schema: [],
},
};