Skip to content

Commit 6d36407

Browse files
authored
no-useless-undefined: Ignore undefined use in compare functions (#758)
1 parent 12b46da commit 6d36407

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

rules/no-useless-undefined.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,43 @@ const assignmentPatternSelector = getSelector('AssignmentPattern', 'right');
3232

3333
const isUndefined = node => node && node.type === 'Identifier' && node.name === 'undefined';
3434

35+
const compareFunctionNames = new Set([
36+
'is',
37+
'equal',
38+
'notEqual',
39+
'strictEqual',
40+
'notStrictEqual',
41+
'propertyVal',
42+
'notPropertyVal',
43+
'not',
44+
'include',
45+
'property',
46+
'toBe',
47+
'toContain',
48+
'toContainEqual',
49+
'toEqual',
50+
'same',
51+
'notSame',
52+
'strictSame',
53+
'strictNotSame'
54+
]);
55+
const isCompareFunction = node => {
56+
let name;
57+
58+
if (node.type === 'Identifier') {
59+
name = node.name;
60+
} else if (
61+
node.type === 'MemberExpression' &&
62+
node.computed === false &&
63+
node.property &&
64+
node.property.type === 'Identifier'
65+
) {
66+
name = node.property.name;
67+
}
68+
69+
return compareFunctionNames.has(name);
70+
};
71+
3572
const create = context => {
3673
const listener = fix => node => {
3774
context.report({
@@ -64,6 +101,10 @@ const create = context => {
64101
(node, fixer) => fixer.removeRange([node.parent.left.range[1], node.range[1]])
65102
),
66103
CallExpression: node => {
104+
if (isCompareFunction(node.callee)) {
105+
return;
106+
}
107+
67108
const argumentNodes = node.arguments;
68109
const undefinedArguments = [];
69110
for (let index = argumentNodes.length - 1; index >= 0; index--) {

test/no-useless-undefined.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,28 @@ ruleTester.run('no-useless-undefined', rule, {
3131
'function foo({bar} = {}) {}',
3232
'function foo(bar) {}',
3333
// I guess nobody use this, but `yield* undefined;` is valid code, and `yield*;` is not
34-
'function* foo() {yield* undefined;}'
34+
'function* foo() {yield* undefined;}',
35+
36+
// Ignored
37+
'if (Object.is(foo, undefined)){}',
38+
't.is(foo, undefined)',
39+
'assert.equal(foo, undefined, message)',
40+
'assert.notEqual(foo, undefined, message)',
41+
'assert.strictEqual(foo, undefined, message)',
42+
'assert.notStrictEqual(foo, undefined, message)',
43+
'assert.propertyVal(foo, "bar", undefined, message)',
44+
'assert.notPropertyVal(foo, "bar", undefined, message)',
45+
'expect(foo).not(undefined)',
46+
'expect(foo).to.have.property("bar", undefined)',
47+
'expect(foo).to.have.property("bar", undefined)',
48+
'expect(foo).toBe(undefined)',
49+
'expect(foo).toContain(undefined)',
50+
'expect(foo).toContainEqual(undefined)',
51+
'expect(foo).toEqual(undefined)',
52+
't.same(foo, undefined)',
53+
't.notSame(foo, undefined)',
54+
't.strictSame(foo, undefined)',
55+
't.strictNotSame(foo, undefined)'
3556
],
3657
invalid: [
3758
{

0 commit comments

Comments
 (0)