Skip to content

Commit

Permalink
no-useless-undefined: Handle parenthesized undefined (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Apr 15, 2021
1 parent 92bebe1 commit c88882e
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 10 deletions.
14 changes: 5 additions & 9 deletions rules/no-useless-undefined.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const {isCommaToken} = require('eslint-utils');
const getDocumentationUrl = require('./utils/get-documentation-url');
const replaceNodeOrTokenAndSpacesBefore = require('./utils/replace-node-or-token-and-spaces-before');

const messageId = 'no-useless-undefined';
const messages = {
Expand Down Expand Up @@ -97,19 +98,14 @@ const create = context => {
});
};

const code = context.getSourceCode().text;
const sourceCode = context.getSourceCode();
const options = {
checkArguments: true,
...context.options[0]
};

const removeNodeAndLeadingSpace = (node, fixer) => {
const textBefore = code.slice(0, node.range[0]);
return fixer.removeRange([
node.range[0] - (textBefore.length - textBefore.trim().length),
node.range[1]
]);
};
const removeNodeAndLeadingSpace = (node, fixer) =>
replaceNodeOrTokenAndSpacesBefore(node, '', fixer, sourceCode);

const listeners = {
[returnSelector]: listener(
Expand All @@ -118,7 +114,7 @@ const create = context => {
),
[yieldSelector]: listener(removeNodeAndLeadingSpace),
[arrowFunctionSelector]: listener(
(node, fixer) => fixer.replaceText(node, '{}'),
(node, fixer) => replaceNodeOrTokenAndSpacesBefore(node, ' {}', fixer, sourceCode),
/* CheckFunctionReturnType */ true
),
[variableInitSelector]: listener(
Expand Down
39 changes: 38 additions & 1 deletion test/no-useless-undefined.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,43 @@ test.snapshot({
`,
'function foo([bar = undefined] = []) {}',
'foo(bar, undefined, undefined);',
'let a = undefined, b = 2;'
'let a = undefined, b = 2;',
outdent`
function foo() {
return /* */ (
/* */
(
/* */
undefined
/* */
)
/* */
) /* */ ;
}
`,
outdent`
function * foo() {
yield /* */ (
/* */
(
/* */
undefined
/* */
)
/* */
) /* */ ;
}
`,
outdent`
const foo = () => /* */ (
/* */
(
/* */
undefined
/* */
)
/* */
);
`
]
});
132 changes: 132 additions & 0 deletions test/snapshots/no-useless-undefined.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,135 @@ Generated by [AVA](https://avajs.dev).
> 1 | let a = undefined, b = 2;␊
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
`

## Invalid #5
1 | function foo() {
2 | return /* */ (
3 | /* */
4 | (
5 | /* */
6 | undefined
7 | /* */
8 | )
9 | /* */
10 | ) /* */ ;
11 | }

> Output
`␊
1 | function foo() {␊
2 | return /* */␊
3 | /* */␊
4 |␊
5 | /* */␊
6 |␊
7 | /* */␊
8 |␊
9 | /* */␊
10 | /* */ ;␊
11 | }␊
`

> Error 1/1
`␊
1 | function foo() {␊
2 | return /* */ (␊
3 | /* */␊
4 | (␊
5 | /* */␊
> 6 | undefined␊
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
7 | /* */␊
8 | )␊
9 | /* */␊
10 | ) /* */ ;␊
11 | }␊
`

## Invalid #6
1 | function * foo() {
2 | yield /* */ (
3 | /* */
4 | (
5 | /* */
6 | undefined
7 | /* */
8 | )
9 | /* */
10 | ) /* */ ;
11 | }

> Output
`␊
1 | function * foo() {␊
2 | yield /* */␊
3 | /* */␊
4 |␊
5 | /* */␊
6 |␊
7 | /* */␊
8 |␊
9 | /* */␊
10 | /* */ ;␊
11 | }␊
`

> Error 1/1
`␊
1 | function * foo() {␊
2 | yield /* */ (␊
3 | /* */␊
4 | (␊
5 | /* */␊
> 6 | undefined␊
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
7 | /* */␊
8 | )␊
9 | /* */␊
10 | ) /* */ ;␊
11 | }␊
`

## Invalid #7
1 | const foo = () => /* */ (
2 | /* */
3 | (
4 | /* */
5 | undefined
6 | /* */
7 | )
8 | /* */
9 | );

> Output
`␊
1 | const foo = () => /* */␊
2 | /* */␊
3 |␊
4 | /* */␊
5 | {}␊
6 | /* */␊
7 |␊
8 | /* */␊
9 | ;␊
`

> Error 1/1
`␊
1 | const foo = () => /* */ (␊
2 | /* */␊
3 | (␊
4 | /* */␊
> 5 | undefined␊
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
6 | /* */␊
7 | )␊
8 | /* */␊
9 | );␊
`
Binary file modified test/snapshots/no-useless-undefined.mjs.snap
Binary file not shown.

0 comments on commit c88882e

Please sign in to comment.