-
Notifications
You must be signed in to change notification settings - Fork 648
Expand file tree
/
Copy pathprefer-sinon-assert.ts
More file actions
151 lines (143 loc) · 6.41 KB
/
prefer-sinon-assert.ts
File metadata and controls
151 lines (143 loc) · 6.41 KB
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type {TSESTree} from '@typescript-eslint/utils';
import {createRule} from './utils/ruleCreator.ts';
function isAssert(calleeNode: TSESTree.Expression) {
if (calleeNode.type === 'Identifier' && calleeNode.name === 'assert') {
return true;
}
if (calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier') {
return ['isNotFalse', 'isOk', 'isTrue', 'ok'].includes(calleeNode.property.name);
}
return false;
}
function isAssertFalsy(node: TSESTree.Expression) {
if (node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'assert' &&
node.property.type === 'Identifier') {
return ['isFalse', 'isNotOk', 'isNotTrue', 'notOk'].includes(node.property.name);
}
return false;
}
function isAssertEquality(node: TSESTree.Expression) {
if (node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'assert' &&
node.property.type === 'Identifier') {
return ['deepEqual', 'equal', 'strictEqual'].includes(node.property.name);
}
return false;
}
type MessageIds = 'useSinonAssertInsteadOfAssert'|'useSinonAssertCalledInsteadOfAssert'|
'useSinonAssertNotCalledInsteadOfAssert'|'useSinonAssertCallCountInsteadOfAssert';
export default createRule<unknown[], MessageIds>({
name: 'prefer-sinon-assert',
meta: {
type: 'suggestion',
docs: {
description:
'Prefer `sinon.assert` over `assert` with spy/stub call checks, as it provides a much better error message.',
category: 'Best Practices',
},
messages: {
useSinonAssertInsteadOfAssert:
'Use `sinon.assert.{{ methodName }}(spy)` instead of `assert(spy.{{ methodName }})`',
useSinonAssertCalledInsteadOfAssert:
'Use `sinon.assert.called(spy)` instead of asserting that `spy.notCalled` doesn\'t hold true',
useSinonAssertNotCalledInsteadOfAssert:
'Use `sinon.assert.notCalled(spy)` instead of asserting that `spy.called` doesn\'t hold true',
useSinonAssertCallCountInsteadOfAssert:
'Use `sinon.assert.{{ methodName }}(spy, num)` instead of asserting equality of `spy.callCount` with `num`',
},
fixable: 'code',
schema: [], // no options
},
defaultOptions: [],
create: function(context) {
function reportError(
node: TSESTree.CallExpression,
methodName: string,
firstArgNodes: TSESTree.Node|TSESTree.Node[],
messageId: MessageIds,
) {
context.report({
node,
messageId,
data: {
methodName,
},
fix(fixer) {
const {sourceCode} = context;
let firstArgText = '';
if (Array.isArray(firstArgNodes)) {
firstArgText = firstArgNodes.map(node => sourceCode.getText(node)).join(', ');
} else {
firstArgText = sourceCode.getText(firstArgNodes);
}
return [
fixer.replaceText(node.arguments[0], firstArgText),
fixer.replaceText(node.callee, `sinon.assert.${methodName}`),
];
}
});
}
return {
CallExpression(node) {
if (node.arguments.length === 1) {
const [argumentNode] = node.arguments;
if (isAssert(node.callee)) {
if (argumentNode.type === 'CallExpression' && argumentNode.callee.type === 'MemberExpression' &&
argumentNode.callee.property.type === 'Identifier') {
const {name} = argumentNode.callee.property;
if ([
'calledOn',
'alwaysCalledOn',
'calledWith',
'calledWithExactly',
'calledOnceWithExactly',
'alwaysCalledWithExactly',
'alwaysCalledWith',
'neverCalledWith',
'calledWithMatch',
'calledOnceWithMatch',
'alwaysCalledWithMatch',
].includes(name)) {
const argumentNodes = [argumentNode.callee.object, ...argumentNode.arguments];
reportError(node, name, argumentNodes, 'useSinonAssertInsteadOfAssert');
}
} else if (argumentNode.type === 'MemberExpression' && argumentNode.property.type === 'Identifier') {
const {name} = argumentNode.property;
if (['notCalled', 'called', 'calledOnce', 'calledTwice', 'calledThrice'].includes(name)) {
reportError(node, name, argumentNode.object, 'useSinonAssertInsteadOfAssert');
}
} else if (argumentNode.type === 'UnaryExpression' && argumentNode.operator === '!') {
const expressionNode = argumentNode.argument;
if (expressionNode.type === 'MemberExpression' && expressionNode.property.type === 'Identifier') {
if (expressionNode.property.name === 'notCalled') {
reportError(node, 'called', expressionNode.object, 'useSinonAssertCalledInsteadOfAssert');
} else if (expressionNode.property.name === 'called') {
reportError(node, 'notCalled', expressionNode.object, 'useSinonAssertNotCalledInsteadOfAssert');
}
}
}
} else if (isAssertFalsy(node.callee)) {
if (argumentNode.type === 'MemberExpression' && argumentNode.property.type === 'Identifier') {
if (argumentNode.property.name === 'notCalled') {
reportError(node, 'called', argumentNode.object, 'useSinonAssertCalledInsteadOfAssert');
} else if (argumentNode.property.name === 'called') {
reportError(node, 'notCalled', argumentNode.object, 'useSinonAssertNotCalledInsteadOfAssert');
}
}
}
} else if (node.arguments.length === 2) {
const [argumentNode] = node.arguments;
if (isAssertEquality(node.callee)) {
if (argumentNode.type === 'MemberExpression' && argumentNode.property.type === 'Identifier' &&
argumentNode.property.name === 'callCount') {
reportError(node, 'callCount', argumentNode.object, 'useSinonAssertCallCountInsteadOfAssert');
}
}
}
}
};
},
});