Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rules/no-useless-collection-argument.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

💼 This rule is enabled in the following [configs](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config): ✅ `recommended`, ☑️ `unopinionated`.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
🔧💡 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix) and manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

<!-- end auto-generated rule header -->
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` -->
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default [
| [no-unreadable-array-destructuring](docs/rules/no-unreadable-array-destructuring.md) | Disallow unreadable array destructuring. | ✅ ☑️ | 🔧 | |
| [no-unreadable-iife](docs/rules/no-unreadable-iife.md) | Disallow unreadable IIFEs. | ✅ ☑️ | | |
| [no-unused-properties](docs/rules/no-unused-properties.md) | Disallow unused object properties. | | | |
| [no-useless-collection-argument](docs/rules/no-useless-collection-argument.md) | Disallow useless values or fallbacks in `Set`, `Map`, `WeakSet`, or `WeakMap`. | ✅ ☑️ | 🔧 | |
| [no-useless-collection-argument](docs/rules/no-useless-collection-argument.md) | Disallow useless values or fallbacks in `Set`, `Map`, `WeakSet`, or `WeakMap`. | ✅ ☑️ | 🔧 | 💡 |
| [no-useless-error-capture-stack-trace](docs/rules/no-useless-error-capture-stack-trace.md) | Disallow unnecessary `Error.captureStackTrace(…)`. | ✅ ☑️ | 🔧 | |
| [no-useless-fallback-in-spread](docs/rules/no-useless-fallback-in-spread.md) | Disallow useless fallback when spreading in object literals. | ✅ ☑️ | 🔧 | |
| [no-useless-length-check](docs/rules/no-useless-length-check.md) | Disallow useless array length check. | ✅ ☑️ | 🔧 | |
Expand Down
52 changes: 41 additions & 11 deletions rules/no-useless-collection-argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import {
} from './ast/index.js';
import {removeParentheses, removeArgument} from './fix/index.js';

const MESSAGE_ID = 'no-useless-collection-argument';
/**
@import {TSESTree as ESTree} from '@typescript-eslint/types';
@import * as ESLint from 'eslint';
*/

const MESSAGE_ID_ERROR = 'no-useless-collection-argument/error';
const MESSAGE_ID_SUGGESTION = 'no-useless-collection-argument/suggestion';
const messages = {
[MESSAGE_ID]: 'The {{description}} is useless.',
[MESSAGE_ID_ERROR]: 'The {{description}} is useless.',
[MESSAGE_ID_SUGGESTION]: 'Remove the {{description}}',
};

const getDescription = node => {
Expand All @@ -33,7 +40,7 @@ const getDescription = node => {

const removeFallback = (node, context) =>
// Same code from rules/no-useless-fallback-in-spread.js
/** @param {import('eslint').Rule.RuleFixer} fixer */
/** @param {ESLint.Rule.RuleFixer} fixer */
function * fix(fixer) {
const {sourceCode} = context;
const logicalExpression = node.parent;
Expand All @@ -54,9 +61,9 @@ const removeFallback = (node, context) =>
}
};

/** @param {import('eslint').Rule.RuleContext} context */
/** @param {ESLint.Rule.RuleContext} context */
const create = context => {
context.on('NewExpression', newExpression => {
context.on('NewExpression', (/** @type {ESTree.NewExpression} */ newExpression) => {
if (!isNewExpression(newExpression, {
names: ['Set', 'Map', 'WeakSet', 'WeakMap'],
argumentsLength: 1,
Expand All @@ -73,18 +80,40 @@ const create = context => {
return;
}

return {
let fix;
let shouldUseSuggestion = false;
if (isCheckingFallback) {
fix = removeFallback(node, context);
} else {
if (context.sourceCode.getCommentsInside(node).length > 0) {
shouldUseSuggestion = true;
}

fix = fixer => removeArgument(fixer, node, context);
}

const problem = {
node,
messageId: MESSAGE_ID,
messageId: MESSAGE_ID_ERROR,
data: {description},
fix: isCheckingFallback
? removeFallback(node, context)
: fixer => removeArgument(fixer, node, context),
};

if (shouldUseSuggestion) {
problem.suggest = [
{
messageId: MESSAGE_ID_SUGGESTION,
fix,
},
];
} else {
problem.fix = fix;
}

return problem;
});
};

/** @type {import('eslint').Rule.RuleModule} */
/** @type {ESLint.Rule.RuleModule} */
const config = {
create,
meta: {
Expand All @@ -94,6 +123,7 @@ const config = {
recommended: 'unopinionated',
},
fixable: 'code',
hasSuggestions: true,
messages,
},
};
Expand Down
2 changes: 2 additions & 0 deletions test/no-useless-collection-argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@ test.snapshot({
'new Set([] ?? "")',
'new Set( (( (( "" )) ?? (( [] )) )) )',
'new Set(foo ?? bar ?? [])',
// Comments
'new Set([/**/])',
],
});
7 changes: 6 additions & 1 deletion test/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ test('Every rule file has the appropriate contents', t => {
const rulePath = path.join('rules', `${ruleName}.js`);
const ruleContents = fs.readFileSync(rulePath, 'utf8');

t.true(ruleContents.includes('/** @type {import(\'eslint\').Rule.RuleModule} */'), `${ruleName} includes jsdoc comment for rule type`);
t.true(
// TODO: Use `@import` instead of `import('eslint')`
ruleContents.includes('/** @type {import(\'eslint\').Rule.RuleModule} */')
|| ruleContents.includes('/** @type {ESLint.Rule.RuleModule} */'),
`${ruleName} includes jsdoc comment for rule type`,
);
}
});

Expand Down
19 changes: 19 additions & 0 deletions test/snapshots/no-useless-collection-argument.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,22 @@ Generated by [AVA](https://avajs.dev).
Output:␊
1 | new Set(foo ?? bar)␊
`

## invalid(18): new Set([/**/])

> Input

`␊
1 | new Set([/**/])␊
`

> Error 1/1

`␊
Message:␊
> 1 | new Set([/**/])␊
| ^^^^^^ The empty array is useless.␊
Suggestion 1/1: Remove the empty array:␊
1 | new Set()␊
`
Binary file modified test/snapshots/no-useless-collection-argument.js.snap
Binary file not shown.
Loading