-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathno-assert-equal-boolean-null-undefined.ts
178 lines (161 loc) · 7.64 KB
/
no-assert-equal-boolean-null-undefined.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2024 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.
/**
* @fileoverview Don't use assert equality on `boolean`, `null` or `undefined`.
*
* Prefer the more specific `assert.isTrue`, `assert.notIsTrue`, `assert.isFalse`,
* `assert.isNotFalse`, `assert.isNull`, `assert.isNotNull`, `assert.isDefined`,
* and `assert.isUndefined` methods.
*/
import type {TSESTree} from '@typescript-eslint/utils';
import {createRule} from './utils/ruleCreator.ts';
// Define message IDs based on the meta.messages keys
type MessageIds = 'useAssertIsDefined'|'useAssertIsFalse'|'useAssertIsNotFalse'|'useAssertIsNotNull'|
'useAssertIsNotTrue'|'useAssertIsNull'|'useAssertIsTrue'|'useAssertIsUndefined';
type AssertMemberExpression<T extends Set<string> = Set<string>> = TSESTree.MemberExpression&{
object: TSESTree.Identifier & {name: 'assert'},
property: TSESTree.Identifier & {name: keyof T},
};
const EQUALITY_ASSERTIONS = new Set(['deepEqual', 'strictEqual']);
const INEQUALITY_ASSERTIONS = new Set(['notDeepEqual', 'notStrictEqual']);
function isAssertMemberExpression(node: TSESTree.Node): node is AssertMemberExpression {
return node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'assert' &&
node.property.type === 'Identifier';
}
function isAssertEquality(node: TSESTree.CallExpression): node is TSESTree.CallExpression&{
callee: AssertMemberExpression<typeof EQUALITY_ASSERTIONS>,
}
{
const calleeNode = node.callee;
return isAssertMemberExpression(calleeNode) && EQUALITY_ASSERTIONS.has(calleeNode.property.name);
}
function isAssertInequality(node: TSESTree.CallExpression): node is TSESTree.CallExpression&{
callee: AssertMemberExpression<typeof INEQUALITY_ASSERTIONS>,
}
{
const calleeNode = node.callee;
return isAssertMemberExpression(calleeNode) && INEQUALITY_ASSERTIONS.has(calleeNode.property.name);
}
// Type guard for Literal nodes with specific values
function isLiteral<T extends string|boolean|null|number|RegExp>(
argumentNode: TSESTree.Node, value: T): argumentNode is TSESTree.Literal&{
value: T,
}
{
return argumentNode.type === 'Literal' && argumentNode.value === value;
}
// Type guard for the `undefined` identifier
function isUndefinedIdentifier(argumentNode: TSESTree.Node): argumentNode is TSESTree.Identifier&{
name: 'undefined',
}
{
return argumentNode.type === 'Identifier' && argumentNode.name === 'undefined';
}
export default createRule<[], MessageIds>({
name: 'no-assert-equal-boolean-null-undefined',
meta: {
type: 'suggestion',
docs: {
description: 'Don\'t use equality assertions on `boolean`, `null` or `undefined`.',
category: 'Best Practices',
},
messages: {
useAssertIsDefined:
'Use `assert.isDefined` instead of `assert.{{ methodName }}` to check that a value is not `undefined`',
useAssertIsFalse: 'Use `assert.isFalse` instead of `assert.{{ methodName }}` to check that a value is `false`',
useAssertIsNotFalse:
'Use `assert.isNotFalse` instead of `assert.{{ methodName }}` to check that a value is not `false`', // Corrected description
useAssertIsNotNull:
'Use `assert.isNotNull` instead of `assert.{{ methodName }}` to check that a value is not `null`',
useAssertIsNotTrue:
'Use `assert.isNotTrue` instead of `assert.{{ methodName }}` to check that a value is not `true`',
useAssertIsNull: 'Use `assert.isNull` instead of `assert.{{ methodName }}` to check that a value is `null`',
useAssertIsTrue: 'Use `assert.isTrue` instead of `assert.{{ methodName }}` to check that a value is `true`',
useAssertIsUndefined:
'Use `assert.isUndefined` instead of `assert.{{ methodName }}` to check that a value is `undefined`',
},
fixable: 'code',
schema: [], // no options
},
defaultOptions: [],
create: function(context) {
const sourceCode = context.sourceCode;
function reportError(
node: TSESTree.CallExpression&{callee: AssertMemberExpression}, calleeText: string, argumentIndex: number,
messageId: MessageIds): void {
// Type assertion is safe here because reportError is only called after isAssertEquality/Inequality checks
const methodName = node.callee.property.name;
context.report({
node,
messageId,
data: {methodName},
fix(fixer) {
// Ensure the argument exists before accessing it
const argToKeep = node.arguments[argumentIndex];
if (!argToKeep) {
// Should not happen based on the logic, but good practice to check
// Return an empty fix or handle appropriately
console.warn(`ESLint rule ${context.id}: Expected argument at index ${argumentIndex} not found.`);
return fixer.replaceText(node, sourceCode.getText(node)); // No change
}
// Keep the argument at argumentIndex and any subsequent arguments (e.g., for messages)
const args = [argToKeep].concat(node.arguments.slice(2));
const argsText = args.map(argNode => sourceCode.getText(argNode)).join(', ');
return fixer.replaceText(node, `${calleeText}(${argsText})`);
}
});
}
return {
CallExpression(node): void {
// Need at least two arguments for equality/inequality checks
if (node.arguments.length < 2) {
return;
}
// Ensure arguments are valid nodes before proceeding
const arg0 = node.arguments[0];
const arg1 = node.arguments[1];
if (!arg0 || !arg1) {
return;
}
if (isAssertEquality(node)) {
if (isLiteral(arg1, false)) {
reportError(node, 'assert.isFalse', 0, 'useAssertIsFalse');
} else if (isLiteral(arg0, false)) {
reportError(node, 'assert.isFalse', 1, 'useAssertIsFalse');
} else if (isLiteral(arg1, null)) {
reportError(node, 'assert.isNull', 0, 'useAssertIsNull');
} else if (isLiteral(arg0, null)) {
reportError(node, 'assert.isNull', 1, 'useAssertIsNull');
} else if (isLiteral(arg0, true)) {
reportError(node, 'assert.isTrue', 1, 'useAssertIsTrue');
} else if (isLiteral(arg1, true)) {
reportError(node, 'assert.isTrue', 0, 'useAssertIsTrue');
} else if (isUndefinedIdentifier(arg1)) {
reportError(node, 'assert.isUndefined', 0, 'useAssertIsUndefined');
} else if (isUndefinedIdentifier(arg0)) {
reportError(node, 'assert.isUndefined', 1, 'useAssertIsUndefined');
}
} else if (isAssertInequality(node)) {
if (isLiteral(arg1, false)) {
reportError(node, 'assert.isNotFalse', 0, 'useAssertIsNotFalse');
} else if (isLiteral(arg0, false)) {
reportError(node, 'assert.isNotFalse', 1, 'useAssertIsNotFalse');
} else if (isLiteral(arg1, null)) {
reportError(node, 'assert.isNotNull', 0, 'useAssertIsNotNull');
} else if (isLiteral(arg0, null)) {
reportError(node, 'assert.isNotNull', 1, 'useAssertIsNotNull');
} else if (isLiteral(arg0, true)) {
reportError(node, 'assert.isNotTrue', 1, 'useAssertIsNotTrue');
} else if (isLiteral(arg1, true)) {
reportError(node, 'assert.isNotTrue', 0, 'useAssertIsNotTrue');
} else if (isUndefinedIdentifier(arg1)) {
reportError(node, 'assert.isDefined', 0, 'useAssertIsDefined');
} else if (isUndefinedIdentifier(arg0)) {
reportError(node, 'assert.isDefined', 1, 'useAssertIsDefined');
}
}
}
};
},
});