Skip to content

Commit

Permalink
lint ts function args
Browse files Browse the repository at this point in the history
  • Loading branch information
pipopotamasu committed Feb 26, 2024
1 parent 058814b commit 86373be
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
50 changes: 50 additions & 0 deletions rules/no-implicit-any/function-args/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ ruleTester.run('function-args', rule, {
foo(function (bar) {});
`,
},
// TSFunctionType
{
code: 'type Foo = (arg: any) => void;',
},
{
code: `
type Foo = {
fn: (arg: any) => void;
}
`,
},
{
code: `
interface Foo {
fn: (arg: any) => void;
}
`,
},
],
invalid: [
// FunctionDeclaration or FunctionExpression
Expand Down Expand Up @@ -254,5 +272,37 @@ ruleTester.run('function-args', rule, {
`,
errors: [{ messageId: 'missingAnyType' }],
},
// TSFunctionType
{
code: 'type Foo = (arg) => void;',
output: 'type Foo = (arg: any) => void;',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: `
type Foo = {
fn: (arg) => void;
}
`,
output: `
type Foo = {
fn: (arg: any) => void;
}
`,
errors: [{ messageId: 'missingAnyType' }],
},
{
code: `
interface Foo {
fn: (arg) => void;
}
`,
output: `
interface Foo {
fn: (arg: any) => void;
}
`,
errors: [{ messageId: 'missingAnyType' }],
},
],
});
9 changes: 9 additions & 0 deletions rules/no-implicit-any/function-args/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ export const lintFunctionDeclaration = (
});
};

export const lintTSFunctionType = (
context: Readonly<TSESLint.RuleContext<'missingAnyType', any[]>>,
node: TSESTree.TSFunctionType
) => {
node.params.forEach((arg) => {
lintArg(context, arg);
});
};

export const lintFunctionExpression = (
context: Readonly<TSESLint.RuleContext<'missingAnyType', any[]>>,
node: TSESTree.FunctionExpression
Expand Down
4 changes: 4 additions & 0 deletions rules/no-implicit-any/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
lintFunctionDeclaration,
lintFunctionExpression,
lintArrowFunctionExpression,
lintTSFunctionType,
} from './function-args';
import { lintMemberExpression } from './member-expression';
import { lintVariableDeclarator } from './variable-declarator';
Expand Down Expand Up @@ -42,6 +43,9 @@ export const rule = createRule({
ArrowFunctionExpression(node) {
lintArrowFunctionExpression(context, node);
},
TSFunctionType(node) {
lintTSFunctionType(context, node);
},
VariableDeclarator(node) {
lintVariableDeclarator(context, node);
},
Expand Down

0 comments on commit 86373be

Please sign in to comment.