Skip to content

Commit

Permalink
no-useless-undefined: Ignore Set#add() Map#set() `Array#{push,u…
Browse files Browse the repository at this point in the history
…nshift}()` (#1353)
  • Loading branch information
fisker authored Jun 15, 2021
1 parent 4bf97a4 commit c6359c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 11 additions & 3 deletions rules/no-useless-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const compareFunctionNames = new Set([
'strictSame',
'strictNotSame'
]);
const isCompareFunction = node => {
const shouldIgnore = node => {
let name;

if (node.type === 'Identifier') {
Expand All @@ -70,7 +70,15 @@ const isCompareFunction = node => {
name = node.property.name;
}

return compareFunctionNames.has(name);
return compareFunctionNames.has(name) ||
// `set.add(undefined)`
name === 'add' ||
// `map.set(foo, undefined)`
name === 'set' ||
// `array.push(undefined)`
name === 'push' ||
// `array.unshift(undefined)`
name === 'unshift';
};

const getFunction = scope => {
Expand Down Expand Up @@ -126,7 +134,7 @@ const create = context => {

if (options.checkArguments) {
listeners.CallExpression = node => {
if (isCompareFunction(node.callee)) {
if (shouldIgnore(node.callee)) {
return;
}

Expand Down
6 changes: 6 additions & 0 deletions test/no-useless-undefined.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ test({
't.strictSame(foo, undefined)',
't.strictNotSame(foo, undefined)',
'expect(someFunction).toHaveBeenCalledWith(1, 2, undefined);',
'set.add(undefined);',
'map.set(foo, undefined);',
'array.push(foo, undefined);',
'array.push(undefined);',
'array.unshift(foo, undefined);',
'array.unshift(undefined);',

// `checkArguments: false`
{
Expand Down

0 comments on commit c6359c3

Please sign in to comment.