-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathprefer-sinon-assert.ts
143 lines (136 loc) · 6.26 KB
/
prefer-sinon-assert.ts
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
// Copyright 2025 The Chromium Authors. All rights reserved.
// 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';
export default createRule({
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 isAssert(calleeNode) {
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(calleeNode) {
if (calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier') {
return ['isFalse', 'isNotOk', 'isNotTrue', 'notOk'].includes(calleeNode.property.name);
}
return false;
}
function isAssertEquality(calleeNode) {
if (calleeNode.type === 'MemberExpression' && calleeNode.object.type === 'Identifier' &&
calleeNode.object.name === 'assert' && calleeNode.property.type === 'Identifier') {
return ['deepEqual', 'equal', 'strictEqual'].includes(calleeNode.property.name);
}
return false;
}
function reportError(node, methodName: string, firstArgNodes: TSESTree.Node|TSESTree.Node[], messageId) {
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');
}
}
}
}
};
},
});