Skip to content

Commit c88882e

Browse files
authored
no-useless-undefined: Handle parenthesized undefined (#1178)
1 parent 92bebe1 commit c88882e

File tree

4 files changed

+175
-10
lines changed

4 files changed

+175
-10
lines changed

rules/no-useless-undefined.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const {isCommaToken} = require('eslint-utils');
33
const getDocumentationUrl = require('./utils/get-documentation-url');
4+
const replaceNodeOrTokenAndSpacesBefore = require('./utils/replace-node-or-token-and-spaces-before');
45

56
const messageId = 'no-useless-undefined';
67
const messages = {
@@ -97,19 +98,14 @@ const create = context => {
9798
});
9899
};
99100

100-
const code = context.getSourceCode().text;
101+
const sourceCode = context.getSourceCode();
101102
const options = {
102103
checkArguments: true,
103104
...context.options[0]
104105
};
105106

106-
const removeNodeAndLeadingSpace = (node, fixer) => {
107-
const textBefore = code.slice(0, node.range[0]);
108-
return fixer.removeRange([
109-
node.range[0] - (textBefore.length - textBefore.trim().length),
110-
node.range[1]
111-
]);
112-
};
107+
const removeNodeAndLeadingSpace = (node, fixer) =>
108+
replaceNodeOrTokenAndSpacesBefore(node, '', fixer, sourceCode);
113109

114110
const listeners = {
115111
[returnSelector]: listener(
@@ -118,7 +114,7 @@ const create = context => {
118114
),
119115
[yieldSelector]: listener(removeNodeAndLeadingSpace),
120116
[arrowFunctionSelector]: listener(
121-
(node, fixer) => fixer.replaceText(node, '{}'),
117+
(node, fixer) => replaceNodeOrTokenAndSpacesBefore(node, ' {}', fixer, sourceCode),
122118
/* CheckFunctionReturnType */ true
123119
),
124120
[variableInitSelector]: listener(

test/no-useless-undefined.mjs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,43 @@ test.snapshot({
367367
`,
368368
'function foo([bar = undefined] = []) {}',
369369
'foo(bar, undefined, undefined);',
370-
'let a = undefined, b = 2;'
370+
'let a = undefined, b = 2;',
371+
outdent`
372+
function foo() {
373+
return /* */ (
374+
/* */
375+
(
376+
/* */
377+
undefined
378+
/* */
379+
)
380+
/* */
381+
) /* */ ;
382+
}
383+
`,
384+
outdent`
385+
function * foo() {
386+
yield /* */ (
387+
/* */
388+
(
389+
/* */
390+
undefined
391+
/* */
392+
)
393+
/* */
394+
) /* */ ;
395+
}
396+
`,
397+
outdent`
398+
const foo = () => /* */ (
399+
/* */
400+
(
401+
/* */
402+
undefined
403+
/* */
404+
)
405+
/* */
406+
);
407+
`
371408
]
372409
});

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,135 @@ Generated by [AVA](https://avajs.dev).
8787
> 1 | let a = undefined, b = 2;␊
8888
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
8989
`
90+
91+
## Invalid #5
92+
1 | function foo() {
93+
2 | return /* */ (
94+
3 | /* */
95+
4 | (
96+
5 | /* */
97+
6 | undefined
98+
7 | /* */
99+
8 | )
100+
9 | /* */
101+
10 | ) /* */ ;
102+
11 | }
103+
104+
> Output
105+
106+
`␊
107+
1 | function foo() {␊
108+
2 | return /* */␊
109+
3 | /* */␊
110+
4 |␊
111+
5 | /* */␊
112+
6 |␊
113+
7 | /* */␊
114+
8 |␊
115+
9 | /* */␊
116+
10 | /* */ ;␊
117+
11 | }␊
118+
`
119+
120+
> Error 1/1
121+
122+
`␊
123+
1 | function foo() {␊
124+
2 | return /* */ (␊
125+
3 | /* */␊
126+
4 | (␊
127+
5 | /* */␊
128+
> 6 | undefined␊
129+
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
130+
7 | /* */␊
131+
8 | )␊
132+
9 | /* */␊
133+
10 | ) /* */ ;␊
134+
11 | }␊
135+
`
136+
137+
## Invalid #6
138+
1 | function * foo() {
139+
2 | yield /* */ (
140+
3 | /* */
141+
4 | (
142+
5 | /* */
143+
6 | undefined
144+
7 | /* */
145+
8 | )
146+
9 | /* */
147+
10 | ) /* */ ;
148+
11 | }
149+
150+
> Output
151+
152+
`␊
153+
1 | function * foo() {␊
154+
2 | yield /* */␊
155+
3 | /* */␊
156+
4 |␊
157+
5 | /* */␊
158+
6 |␊
159+
7 | /* */␊
160+
8 |␊
161+
9 | /* */␊
162+
10 | /* */ ;␊
163+
11 | }␊
164+
`
165+
166+
> Error 1/1
167+
168+
`␊
169+
1 | function * foo() {␊
170+
2 | yield /* */ (␊
171+
3 | /* */␊
172+
4 | (␊
173+
5 | /* */␊
174+
> 6 | undefined␊
175+
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
176+
7 | /* */␊
177+
8 | )␊
178+
9 | /* */␊
179+
10 | ) /* */ ;␊
180+
11 | }␊
181+
`
182+
183+
## Invalid #7
184+
1 | const foo = () => /* */ (
185+
2 | /* */
186+
3 | (
187+
4 | /* */
188+
5 | undefined
189+
6 | /* */
190+
7 | )
191+
8 | /* */
192+
9 | );
193+
194+
> Output
195+
196+
`␊
197+
1 | const foo = () => /* */␊
198+
2 | /* */␊
199+
3 |␊
200+
4 | /* */␊
201+
5 | {}␊
202+
6 | /* */␊
203+
7 |␊
204+
8 | /* */␊
205+
9 | ;␊
206+
`
207+
208+
> Error 1/1
209+
210+
`␊
211+
1 | const foo = () => /* */ (␊
212+
2 | /* */␊
213+
3 | (␊
214+
4 | /* */␊
215+
> 5 | undefined␊
216+
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
217+
6 | /* */␊
218+
7 | )␊
219+
8 | /* */␊
220+
9 | );␊
221+
`
294 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)