Skip to content

Commit 27f7509

Browse files
authored
no-useless-undefined: Check one undefined at a time (#2792)
1 parent 8a132ac commit 27f7509

File tree

5 files changed

+20
-89
lines changed

5 files changed

+20
-89
lines changed

rules/ast/is-undefined.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default function isUndefined(node) {
2-
return node.type === 'Identifier' && node.name === 'undefined';
2+
return node?.type === 'Identifier' && node.name === 'undefined';
33
}

rules/no-useless-undefined.js

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import {isCommaToken} from '@eslint-community/eslint-utils';
2-
import {replaceNodeOrTokenAndSpacesBefore} from './fix/index.js';
1+
import {removeArgument, replaceNodeOrTokenAndSpacesBefore} from './fix/index.js';
32
import {isUndefined, isFunction} from './ast/index.js';
4-
import {getParenthesizedRange} from './utils/index.js';
53

64
const messageId = 'no-useless-undefined';
75
const messages = {
@@ -223,58 +221,22 @@ const create = context => {
223221
}
224222

225223
context.on('CallExpression', node => {
226-
if (shouldIgnore(node.callee)) {
227-
return;
228-
}
229-
230224
const argumentNodes = node.arguments;
225+
const lastArgument = argumentNodes.at(-1);
231226

232-
// Ignore arguments in `Function#bind()`, but not `this` argument
233-
if (isFunctionBindCall(node) && argumentNodes.length !== 1) {
227+
if (!isUndefined(lastArgument) || shouldIgnore(node.callee)) {
234228
return;
235229
}
236230

237-
const undefinedArguments = [];
238-
for (let index = argumentNodes.length - 1; index >= 0; index--) {
239-
const node = argumentNodes[index];
240-
if (isUndefined(node)) {
241-
undefinedArguments.unshift(node);
242-
} else {
243-
break;
244-
}
245-
}
246-
247-
if (undefinedArguments.length === 0) {
231+
// Ignore arguments in `Function#bind()`, but not `this` argument
232+
if (isFunctionBindCall(node) && argumentNodes.length !== 1) {
248233
return;
249234
}
250235

251-
const firstUndefined = undefinedArguments[0];
252-
const lastUndefined = undefinedArguments.at(-1);
253-
254236
return {
237+
node: lastArgument,
255238
messageId,
256-
loc: {
257-
start: sourceCode.getLoc(firstUndefined).start,
258-
end: sourceCode.getLoc(lastUndefined).end,
259-
},
260-
fix(fixer) {
261-
let [start] = getParenthesizedRange(firstUndefined, context);
262-
let [, end] = getParenthesizedRange(lastUndefined, context);
263-
264-
const previousArgument = argumentNodes[argumentNodes.length - undefinedArguments.length - 1];
265-
266-
if (previousArgument) {
267-
[, start] = getParenthesizedRange(previousArgument, context);
268-
} else {
269-
// If all arguments removed, and there is trailing comma, we need remove it.
270-
const tokenAfter = sourceCode.getTokenBefore(sourceCode.getLastToken(node));
271-
if (isCommaToken(tokenAfter)) {
272-
[, end] = sourceCode.getRange(tokenAfter);
273-
}
274-
}
275-
276-
return fixer.removeRange([start, end]);
277-
},
239+
fix: fixer => removeArgument(fixer, lastArgument, context),
278240
};
279241
});
280242
};

test/no-useless-undefined.js

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ test({
150150
},
151151
{
152152
code: 'foo(undefined, undefined);',
153-
output: 'foo();',
153+
output: 'foo(undefined);',
154154
errors,
155155
},
156156
{
@@ -160,7 +160,7 @@ test({
160160
},
161161
{
162162
code: 'foo(undefined, undefined,);',
163-
output: 'foo();',
163+
output: 'foo(undefined,);',
164164
errors,
165165
},
166166
{
@@ -170,7 +170,7 @@ test({
170170
},
171171
{
172172
code: 'foo(bar, undefined, undefined);',
173-
output: 'foo(bar);',
173+
output: 'foo(bar, undefined);',
174174
errors,
175175
},
176176
{
@@ -190,42 +190,14 @@ test({
190190
},
191191
{
192192
code: 'foo(bar, undefined, undefined,);',
193-
output: 'foo(bar,);',
193+
output: 'foo(bar, undefined,);',
194194
errors,
195195
},
196196
{
197197
code: 'foo(undefined, bar, undefined, undefined,);',
198-
output: 'foo(undefined, bar,);',
198+
output: 'foo(undefined, bar, undefined,);',
199199
errors,
200200
},
201-
// Test report range
202-
{
203-
code: outdent`
204-
foo(
205-
undefined,
206-
bar,
207-
undefined,
208-
undefined,
209-
undefined,
210-
undefined,
211-
)
212-
`,
213-
output: outdent`
214-
foo(
215-
undefined,
216-
bar,
217-
)
218-
`,
219-
errors: [
220-
{
221-
messageId,
222-
// The second `undefined`
223-
line: 4, column: 2,
224-
// The last `undefined`
225-
endLine: 7, endColumn: 11,
226-
},
227-
],
228-
},
229201
{
230202
code: 'const {foo = undefined} = {};',
231203
output: 'const {foo} = {};',

test/snapshots/no-useless-undefined.js.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,11 @@ Generated by [AVA](https://avajs.dev).
3434
1 | foo(␊
3535
2 | undefined,␊
3636
3 | bar,␊
37-
> 4 | undefined,␊
38-
| ^^^^^^^^^^␊
39-
> 5 | undefined,␊
40-
| ^^^^^^^^^^^␊
41-
> 6 | undefined,␊
42-
| ^^^^^^^^^^^␊
37+
4 | undefined,␊
38+
5 | undefined,␊
39+
6 | undefined,␊
4340
> 7 | undefined,␊
44-
| ^^^^^^^^^^^ Do not use useless \`undefined\`.␊
41+
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
4542
8 | )␊
4643
`
4744

@@ -84,7 +81,7 @@ Generated by [AVA](https://avajs.dev).
8481
8582
`␊
8683
> 1 | foo(bar, undefined, undefined);␊
87-
| ^^^^^^^^^^^^^^^^^^^^ Do not use useless \`undefined\`.␊
84+
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
8885
`
8986

9087
## invalid(4): let a = undefined, b = 2;
@@ -410,7 +407,7 @@ Generated by [AVA](https://avajs.dev).
410407
411408
`␊
412409
> 1 | foo( ((a)), ((undefined)), ((undefined)), )␊
413-
| ^^^^^^^^^^^^^^^^^^^^^^^^ Do not use useless \`undefined\`.␊
410+
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
414411
`
415412

416413
## invalid(15): foo( ((undefined)), ((undefined)), )
@@ -431,7 +428,7 @@ Generated by [AVA](https://avajs.dev).
431428
432429
`␊
433430
> 1 | foo( ((undefined)), ((undefined)), )␊
434-
| ^^^^^^^^^^^^^^^^^^^^^^^^ Do not use useless \`undefined\`.␊
431+
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
435432
`
436433

437434
## invalid(1): <script> import {nextTick} from 'vue'; const foo = nextTick(undefined); </script>
-29 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)