Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-useless-undefined: Ignore undefined use in compare functions #758

Merged
merged 3 commits into from Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions rules/no-useless-undefined.js
Expand Up @@ -32,6 +32,43 @@ const assignmentPatternSelector = getSelector('AssignmentPattern', 'right');

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

const compareFunctionNames = new Set([
'is',
'equal',
'notEqual',
'strictEqual',
'notStrictEqual',
'propertyVal',
'notPropertyVal',
'not',
'include',
'property',
'toBe',
'toContain',
'toContainEqual',
'toEqual',
'same',
'notSame',
'strictSame',
'strictNotSame'
]);
const isCompareFunction = node => {
let name;

if (node.type === 'Identifier') {
name = node.name;
} else if (
node.type === 'MemberExpression' &&
node.computed === false &&
node.property &&
node.property.type === 'Identifier'
) {
name = node.property.name;
}

return compareFunctionNames.has(name);
};

const create = context => {
const listener = fix => node => {
context.report({
Expand Down Expand Up @@ -64,6 +101,10 @@ const create = context => {
(node, fixer) => fixer.removeRange([node.parent.left.range[1], node.range[1]])
),
CallExpression: node => {
if (isCompareFunction(node.callee)) {
return;
}

const argumentNodes = node.arguments;
const undefinedArguments = [];
for (let index = argumentNodes.length - 1; index >= 0; index--) {
Expand Down
23 changes: 22 additions & 1 deletion test/no-useless-undefined.js
Expand Up @@ -31,7 +31,28 @@ ruleTester.run('no-useless-undefined', rule, {
'function foo({bar} = {}) {}',
'function foo(bar) {}',
// I guess nobody use this, but `yield* undefined;` is valid code, and `yield*;` is not
'function* foo() {yield* undefined;}'
'function* foo() {yield* undefined;}',

// Ignored
'if (Object.is(foo, undefined)){}',
't.is(foo, undefined)',
'assert.equal(foo, undefined, message)',
'assert.notEqual(foo, undefined, message)',
'assert.strictEqual(foo, undefined, message)',
'assert.notStrictEqual(foo, undefined, message)',
'assert.propertyVal(foo, "bar", undefined, message)',
'assert.notPropertyVal(foo, "bar", undefined, message)',
'expect(foo).not(undefined)',
'expect(foo).to.have.property("bar", undefined)',
'expect(foo).to.have.property("bar", undefined)',
'expect(foo).toBe(undefined)',
'expect(foo).toContain(undefined)',
'expect(foo).toContainEqual(undefined)',
'expect(foo).toEqual(undefined)',
't.same(foo, undefined)',
't.notSame(foo, undefined)',
't.strictSame(foo, undefined)',
't.strictNotSame(foo, undefined)'
],
invalid: [
{
Expand Down