-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor function args functions * add implicit return lint
- Loading branch information
1 parent
99c7f81
commit 6a34cfd
Showing
4 changed files
with
104 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ruleTester } from '../../testUtil'; | ||
import { rule } from '../../no-implicit-any'; | ||
|
||
ruleTester.run('implicit-return', rule, { | ||
valid: [ | ||
{ code: 'const foo = () => { return 1 }' }, | ||
{ code: 'const foo = () => 1' }, | ||
{ code: 'const foo = () => null as null' }, | ||
{ code: 'const foo = () => (null as null)' }, | ||
{ code: 'const foo = () => (null) as null' }, | ||
{ code: 'const foo = (): null => null' }, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'const foo = () => null', | ||
output: 'const foo = () => null as null', | ||
errors: [{ messageId: 'missingAnyType' }], | ||
}, | ||
{ | ||
code: 'const foo = () => (null)', | ||
output: 'const foo = () => (null as null)', | ||
errors: [{ messageId: 'missingAnyType' }], | ||
}, | ||
{ | ||
code: 'const foo = () => undefined', | ||
output: 'const foo = () => undefined as undefined', | ||
errors: [{ messageId: 'missingAnyType' }], | ||
}, | ||
{ | ||
code: 'const foo = () => (undefined)', | ||
output: 'const foo = () => (undefined as undefined)', | ||
errors: [{ messageId: 'missingAnyType' }], | ||
}, | ||
{ | ||
code: 'const foo = () => []', | ||
output: 'const foo = () => [] as any[]', | ||
errors: [{ messageId: 'missingAnyType' }], | ||
}, | ||
{ | ||
code: 'const foo = () => ([])', | ||
output: 'const foo = () => ([] as any[])', | ||
errors: [{ messageId: 'missingAnyType' }], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ESLintUtils, type TSESLint } from '@typescript-eslint/utils'; | ||
import { type TSESTree, AST_NODE_TYPES } from '@typescript-eslint/types'; | ||
import { isNull, isUndefined, enabledStrictNullChecks } from '../../helper'; | ||
|
||
export const lintImplicitReturn = ( | ||
context: Readonly<TSESLint.RuleContext<'missingAnyType', any[]>>, | ||
node: TSESTree.ArrowFunctionExpression | ||
) => { | ||
const bodyNode = node.body; | ||
const bodyType = bodyNode.type; | ||
const parserServices = ESLintUtils.getParserServices(context); | ||
if ( | ||
bodyType === AST_NODE_TYPES.BlockStatement || | ||
bodyType === AST_NODE_TYPES.TSAsExpression || | ||
node.returnType || | ||
enabledStrictNullChecks(parserServices.program.getCompilerOptions()) | ||
) | ||
return; | ||
|
||
if (isNull(bodyNode)) { | ||
context.report({ | ||
node: bodyNode, | ||
messageId: 'missingAnyType', | ||
fix(fixer) { | ||
return fixer.insertTextAfter(bodyNode, ' as null'); | ||
}, | ||
}); | ||
} else if (isUndefined(bodyNode)) { | ||
context.report({ | ||
node: bodyNode, | ||
messageId: 'missingAnyType', | ||
fix(fixer) { | ||
return fixer.insertTextAfter(bodyNode, ' as undefined'); | ||
}, | ||
}); | ||
} else if (bodyNode.type === AST_NODE_TYPES.ArrayExpression && bodyNode.elements.length === 0) { | ||
context.report({ | ||
node: bodyNode, | ||
messageId: 'missingAnyType', | ||
fix(fixer) { | ||
return fixer.insertTextAfter(bodyNode, ' as any[]'); | ||
}, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters