Skip to content

Commit

Permalink
fix function args nested object (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
pipopotamasu authored Mar 4, 2024
1 parent f487b49 commit 5b06d06
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
20 changes: 18 additions & 2 deletions rules/no-implicit-any/function-args/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ ruleTester.run('function-args', rule, {
{
code: 'function foo (arg1: any = null, arg2: any = undefined, arg3: any[] = []) {}',
},
{
code: 'function foo ({ a: { b: { c } } }: any) {}',
},
{
code: 'const arrayObjFunc = [{ key: function (arg1: any, arg2: any) {} }];',
},
Expand Down Expand Up @@ -47,6 +50,9 @@ ruleTester.run('function-args', rule, {
{
code: 'const foo = (arg: any = {}) => {};',
},
{
code: 'const foo = ({ a: { b: { c } } }: any) => {}',
},
{
code: 'const arrayObjArrowFunc = [{ key: (arg1: any, arg2: any ) => {} }];',
},
Expand Down Expand Up @@ -203,6 +209,11 @@ ruleTester.run('function-args', rule, {
{ messageId: 'missingAnyType' },
],
},
{
code: 'function foo ({ a: { b: { c } } }) {}',
output: 'function foo ({ a: { b: { c } } }: any) {}',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const arrayObjFunc = [{ key: function (arg1, arg2) {} }];',
output: 'const arrayObjFunc = [{ key: function (arg1: any, arg2: any) {} }];',
Expand All @@ -211,7 +222,7 @@ ruleTester.run('function-args', rule, {
{
code: 'function foo ({ arg1, arg2 }) {}',
output: 'function foo ({ arg1, arg2 }: any) {}',
errors: [{ messageId: 'missingAnyType' }, { messageId: 'missingAnyType' }],
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'function foo ({ arg1, ...rest }) {}',
Expand Down Expand Up @@ -243,6 +254,11 @@ ruleTester.run('function-args', rule, {
{ messageId: 'missingAnyType' },
],
},
{
code: 'const foo = ({ a: { b: { c } } }) => {}',
output: 'const foo = ({ a: { b: { c } } }: any) => {}',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const foo = arg => {}',
output: 'const foo = (arg: any) => {}',
Expand All @@ -261,7 +277,7 @@ ruleTester.run('function-args', rule, {
{
code: 'const foo = ({ arg1, arg2 }) => {}',
output: 'const foo = ({ arg1, arg2 }: any) => {}',
errors: [{ messageId: 'missingAnyType' }, { messageId: 'missingAnyType' }],
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const foo = ({ arg1, ...rest }) => {}',
Expand Down
26 changes: 10 additions & 16 deletions rules/no-implicit-any/function-args/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,16 @@ function lintArg(
},
});
} else if (type.flags === ts.TypeFlags.Object) {
if (arg.type === AST_NODE_TYPES.ObjectPattern) {
arg.properties.forEach((property) => {
if (property.type === AST_NODE_TYPES.Property) {
if (!property.key['typeAnnotation']) {
const type = parserServices.getTypeAtLocation(property);
if (type.flags === ts.TypeFlags.Any) {
context.report({
node: arg,
messageId: 'missingAnyType',
fix(fixer) {
return fixer.insertTextAfter(arg, ': any');
},
});
}
}
}
if (
arg.type === AST_NODE_TYPES.ObjectPattern &&
!(arg.properties.length === 1 && arg.properties[0].type === AST_NODE_TYPES.RestElement)
) {
context.report({
node: arg,
messageId: 'missingAnyType',
fix(fixer) {
return fixer.insertTextAfter(arg, ': any');
},
});
} else if (
arg.type === AST_NODE_TYPES.ArrayPattern ||
Expand Down

0 comments on commit 5b06d06

Please sign in to comment.