Skip to content

Commit a00524a

Browse files
committed
Fix
1 parent 305a772 commit a00524a

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

docs/rules/no-useless-collection-argument.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
🔧💡 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).
66

77
<!-- end auto-generated rule header -->
88
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` -->

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export default [
120120
| [no-unreadable-array-destructuring](docs/rules/no-unreadable-array-destructuring.md) | Disallow unreadable array destructuring. | ✅ ☑️ | 🔧 | |
121121
| [no-unreadable-iife](docs/rules/no-unreadable-iife.md) | Disallow unreadable IIFEs. | ✅ ☑️ | | |
122122
| [no-unused-properties](docs/rules/no-unused-properties.md) | Disallow unused object properties. | | | |
123-
| [no-useless-collection-argument](docs/rules/no-useless-collection-argument.md) | Disallow useless values or fallbacks in `Set`, `Map`, `WeakSet`, or `WeakMap`. | ✅ ☑️ | 🔧 | |
123+
| [no-useless-collection-argument](docs/rules/no-useless-collection-argument.md) | Disallow useless values or fallbacks in `Set`, `Map`, `WeakSet`, or `WeakMap`. | ✅ ☑️ | 🔧 | 💡 |
124124
| [no-useless-error-capture-stack-trace](docs/rules/no-useless-error-capture-stack-trace.md) | Disallow unnecessary `Error.captureStackTrace(…)`. | ✅ ☑️ | 🔧 | |
125125
| [no-useless-fallback-in-spread](docs/rules/no-useless-fallback-in-spread.md) | Disallow useless fallback when spreading in object literals. | ✅ ☑️ | 🔧 | |
126126
| [no-useless-length-check](docs/rules/no-useless-length-check.md) | Disallow useless array length check. | ✅ ☑️ | 🔧 | |

rules/no-useless-collection-argument.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ import {
88
} from './ast/index.js';
99
import {removeParentheses, removeArgument} from './fix/index.js';
1010

11-
const MESSAGE_ID = 'no-useless-collection-argument';
11+
/**
12+
@import {TSESTree as ESTree} from '@typescript-eslint/types';
13+
@import * as ESLint from 'eslint';
14+
*/
15+
16+
const MESSAGE_ID_ERROR = 'no-useless-collection-argument/error';
17+
const MESSAGE_ID_SUGGESTION = 'no-useless-collection-argument/suggestion';
1218
const messages = {
13-
[MESSAGE_ID]: 'The {{description}} is useless.',
19+
[MESSAGE_ID_ERROR]: 'The {{description}} is useless.',
20+
[MESSAGE_ID_SUGGESTION]: 'Remove the {{description}}',
1421
};
1522

1623
const getDescription = node => {
@@ -33,7 +40,7 @@ const getDescription = node => {
3340

3441
const removeFallback = (node, context) =>
3542
// Same code from rules/no-useless-fallback-in-spread.js
36-
/** @param {import('eslint').Rule.RuleFixer} fixer */
43+
/** @param {ESLint.Rule.RuleFixer} fixer */
3744
function * fix(fixer) {
3845
const {sourceCode} = context;
3946
const logicalExpression = node.parent;
@@ -54,9 +61,9 @@ const removeFallback = (node, context) =>
5461
}
5562
};
5663

57-
/** @param {import('eslint').Rule.RuleContext} context */
64+
/** @param {ESLint.Rule.RuleContext} context */
5865
const create = context => {
59-
context.on('NewExpression', newExpression => {
66+
context.on('NewExpression', (/** @type {ESTree.NewExpression} */ newExpression) => {
6067
if (!isNewExpression(newExpression, {
6168
names: ['Set', 'Map', 'WeakSet', 'WeakMap'],
6269
argumentsLength: 1,
@@ -73,18 +80,40 @@ const create = context => {
7380
return;
7481
}
7582

76-
return {
83+
let fix;
84+
let shouldUseSuggestion = false;
85+
if (isCheckingFallback) {
86+
fix = removeFallback(node, context);
87+
} else {
88+
if (context.sourceCode.getCommentsInside(node).length > 0) {
89+
shouldUseSuggestion = true;
90+
}
91+
92+
fix = fixer => removeArgument(fixer, node, context);
93+
}
94+
95+
const problem = {
7796
node,
78-
messageId: MESSAGE_ID,
97+
messageId: MESSAGE_ID_ERROR,
7998
data: {description},
80-
fix: isCheckingFallback
81-
? removeFallback(node, context)
82-
: fixer => removeArgument(fixer, node, context),
8399
};
100+
101+
if (shouldUseSuggestion) {
102+
problem.suggest = [
103+
{
104+
messageId: MESSAGE_ID_SUGGESTION,
105+
fix,
106+
},
107+
];
108+
} else {
109+
problem.fix = fix;
110+
}
111+
112+
return problem;
84113
});
85114
};
86115

87-
/** @type {import('eslint').Rule.RuleModule} */
116+
/** @type {ESLint.Rule.RuleModule} */
88117
const config = {
89118
create,
90119
meta: {
@@ -94,6 +123,7 @@ const config = {
94123
recommended: 'unopinionated',
95124
},
96125
fixable: 'code',
126+
hasSuggestions: true,
97127
messages,
98128
},
99129
};

test/snapshots/no-useless-collection-argument.js.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,6 @@ Generated by [AVA](https://avajs.dev).
532532
> 1 | new Set([/**/])␊
533533
| ^^^^^^ The empty array is useless.␊
534534
535-
Output:␊
535+
Suggestion 1/1: Remove the empty array:␊
536536
1 | new Set()␊
537537
`
18 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)