Skip to content

Commit 41fc9c2

Browse files
authored
no-useless-undefined: Flag return undefined for explicit undefined return types (#2876)
1 parent c88bf29 commit 41fc9c2

2 files changed

Lines changed: 155 additions & 56 deletions

File tree

rules/no-useless-undefined.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ const getFunction = scope => {
7777
}
7878
};
7979

80+
const isUndefinedReturnType = functionNode =>
81+
functionNode.returnType?.typeAnnotation?.type === 'TSUndefinedKeyword';
82+
8083
const isFunctionBindCall = node =>
8184
!node.optional
8285
&& node.callee.type === 'MemberExpression'
@@ -118,10 +121,14 @@ const create = context => {
118121
&& node.parent.type === 'ReturnStatement'
119122
&& node.parent.argument === node
120123
) {
124+
const functionNode = getFunction(sourceCode.getScope(node));
125+
if (functionNode?.returnType && !isUndefinedReturnType(functionNode)) {
126+
return;
127+
}
128+
121129
return getProblem(
122130
node,
123131
fixer => removeNodeAndLeadingSpace(node, fixer),
124-
/* CheckFunctionReturnType */ true,
125132
);
126133
}
127134
});

test/no-useless-undefined.js

Lines changed: 147 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -274,71 +274,163 @@ test.typescript({
274274
return undefined;
275275
}
276276
`,
277-
'const foo = (): undefined => {return undefined;}',
278277
'const foo = (): undefined => undefined;',
279278
'const foo = (): string => undefined;',
280-
'const foo = function (): undefined {return undefined}',
281-
'export function foo(): undefined {return undefined}',
282-
outdent`
283-
const object = {
284-
method(): undefined {
285-
return undefined;
279+
'createContext<T>(undefined);',
280+
'React.createContext<T>(undefined);',
281+
],
282+
invalid: [
283+
{
284+
code: 'function shouldBeFlagged(): undefined {return undefined;}',
285+
output: 'function shouldBeFlagged(): undefined {return;}',
286+
errors,
287+
},
288+
{
289+
code: 'const foo = function (): undefined {return undefined};',
290+
output: 'const foo = function (): undefined {return};',
291+
errors,
292+
},
293+
{
294+
code: outdent`
295+
class A {
296+
method(): undefined {
297+
return undefined;
298+
}
286299
}
287-
}
288-
`,
289-
outdent`
290-
class A {
291-
method(): undefined {
292-
return undefined;
300+
`,
301+
output: outdent`
302+
class A {
303+
method(): undefined {
304+
return;
305+
}
293306
}
294-
}
295-
`,
296-
outdent`
297-
const A = class A {
298-
method(): undefined {
299-
return undefined
307+
`,
308+
errors,
309+
},
310+
{
311+
code: 'export function foo(): undefined {return undefined}',
312+
output: 'export function foo(): undefined {return}',
313+
errors,
314+
},
315+
{
316+
code: outdent`
317+
const object = {
318+
method(): undefined {
319+
return undefined;
320+
}
300321
}
301-
};
302-
`,
303-
outdent`
304-
class A {
305-
static method(): undefined {
306-
return undefined
322+
`,
323+
output: outdent`
324+
const object = {
325+
method(): undefined {
326+
return;
327+
}
307328
}
308-
}
309-
`,
310-
outdent`
311-
class A {
312-
get method(): undefined {
313-
return undefined;
329+
`,
330+
errors,
331+
},
332+
{
333+
code: outdent`
334+
const A = class A {
335+
method(): undefined {
336+
return undefined
337+
}
338+
};
339+
`,
340+
output: outdent`
341+
const A = class A {
342+
method(): undefined {
343+
return
344+
}
345+
};
346+
`,
347+
errors,
348+
},
349+
{
350+
code: outdent`
351+
class A {
352+
static method(): undefined {
353+
return undefined
354+
}
314355
}
315-
}
316-
`,
317-
outdent`
318-
class A {
319-
static get method(): undefined {
320-
return undefined;
356+
`,
357+
output: outdent`
358+
class A {
359+
static method(): undefined {
360+
return
361+
}
321362
}
322-
}
323-
`,
324-
outdent`
325-
class A {
326-
#method(): undefined {
327-
return undefined;
363+
`,
364+
errors,
365+
},
366+
{
367+
code: outdent`
368+
class A {
369+
get method(): undefined {
370+
return undefined;
371+
}
328372
}
329-
}
330-
`,
331-
outdent`
332-
class A {
333-
private method(): undefined {
334-
return undefined;
373+
`,
374+
output: outdent`
375+
class A {
376+
get method(): undefined {
377+
return;
378+
}
335379
}
336-
}
337-
`,
338-
'createContext<T>(undefined);',
339-
'React.createContext<T>(undefined);',
340-
],
341-
invalid: [
380+
`,
381+
errors,
382+
},
383+
{
384+
code: outdent`
385+
class A {
386+
static get method(): undefined {
387+
return undefined;
388+
}
389+
}
390+
`,
391+
output: outdent`
392+
class A {
393+
static get method(): undefined {
394+
return;
395+
}
396+
}
397+
`,
398+
errors,
399+
},
400+
{
401+
code: outdent`
402+
class A {
403+
#method(): undefined {
404+
return undefined;
405+
}
406+
}
407+
`,
408+
output: outdent`
409+
class A {
410+
#method(): undefined {
411+
return;
412+
}
413+
}
414+
`,
415+
errors,
416+
},
417+
{
418+
code: outdent`
419+
class A {
420+
private method(): undefined {
421+
return undefined;
422+
}
423+
}
424+
`,
425+
output: outdent`
426+
class A {
427+
private method(): undefined {
428+
return;
429+
}
430+
}
431+
`,
432+
errors,
433+
},
342434
{
343435
code: outdent`
344436
function foo():undefined {

0 commit comments

Comments
 (0)