Skip to content

feat(eslint-plugin): [typedef] add variable-declaration-ignore-function #1578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion packages/eslint-plugin/docs/rules/typedef.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ This rule has an object option that may receive any of the following as booleans
- `"objectDestructuring"`
- `"parameter"`: `true` by default
- `"propertyDeclaration"`: `true` by default
- `"variableDeclaration"`
- `"variableDeclaration"`,
- `"variableDeclarationIgnoreFunction"`

For example, with the following configuration:

Expand Down Expand Up @@ -254,6 +255,30 @@ let initialText: string = 'text';
let delayedText: string;
```

### `variableDeclarationIgnoreFunction`

Ignore variable declarations for non-arrow and arrow functions.

Examples of **incorrect** code with `{ "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }`:

```ts
const text = 'text';
```

Examples of **correct** code with `{ "variableDeclaration": true, "variableDeclarationIgnoreFunction": true }`:

```ts
const a = (): void => {};
const b = function (): void => {};
const c: () => void = (): void => {};

class Foo {
a = (): void => {};
b = function (): void => {};
c = () => void = (): void => {};
}
```

## When Not To Use It

If you are using stricter TypeScript compiler options, particularly `--noImplicitAny` and/or `--strictPropertyInitialization`, you likely don't need this rule.
Expand Down
17 changes: 16 additions & 1 deletion packages/eslint-plugin/src/rules/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const enum OptionKeys {
Parameter = 'parameter',
PropertyDeclaration = 'propertyDeclaration',
VariableDeclaration = 'variableDeclaration',
VariableDeclarationIgnoreFunction = 'variableDeclarationIgnoreFunction',
}

type Options = { [k in OptionKeys]?: boolean };
Expand Down Expand Up @@ -41,6 +42,7 @@ export default util.createRule<[Options], MessageIds>({
[OptionKeys.Parameter]: { type: 'boolean' },
[OptionKeys.PropertyDeclaration]: { type: 'boolean' },
[OptionKeys.VariableDeclaration]: { type: 'boolean' },
[OptionKeys.VariableDeclarationIgnoreFunction]: { type: 'boolean' },
},
},
],
Expand Down Expand Up @@ -125,6 +127,14 @@ export default util.createRule<[Options], MessageIds>({
}
}

function isVariableDeclarationIgnoreFunction(node: TSESTree.Node): boolean {
return (
!!options[OptionKeys.VariableDeclarationIgnoreFunction] &&
(node.type === AST_NODE_TYPES.FunctionExpression ||
node.type === AST_NODE_TYPES.ArrowFunctionExpression)
);
}

return {
ArrayPattern(node): void {
if (
Expand All @@ -141,6 +151,10 @@ export default util.createRule<[Options], MessageIds>({
}
},
ClassProperty(node): void {
if (node.value && isVariableDeclarationIgnoreFunction(node.value)) {
return;
}

if (
options[OptionKeys.MemberVariableDeclaration] &&
!node.typeAnnotation
Expand Down Expand Up @@ -188,7 +202,8 @@ export default util.createRule<[Options], MessageIds>({
(node.id.type === AST_NODE_TYPES.ArrayPattern &&
!options[OptionKeys.ArrayDestructuring]) ||
(node.id.type === AST_NODE_TYPES.ObjectPattern &&
!options[OptionKeys.ObjectDestructuring])
!options[OptionKeys.ObjectDestructuring]) ||
(node.init && isVariableDeclarationIgnoreFunction(node.init))
) {
return;
}
Expand Down
120 changes: 120 additions & 0 deletions packages/eslint-plugin/tests/rules/typedef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,57 @@ ruleTester.run('typedef', rule, {
},
],
},
// variable declaration ignore function
{
code: `const foo = function(): void {};`,
options: [
{
variableDeclaration: true,
variableDeclarationIgnoreFunction: true,
},
],
},
{
code: `const foo = (): void => {};`,
options: [
{
variableDeclaration: true,
variableDeclarationIgnoreFunction: true,
},
],
},
{
code: `const foo: () => void = (): void => {};`,
options: [
{
variableDeclaration: true,
variableDeclarationIgnoreFunction: true,
},
],
},
{
code: `const foo: () => void = function (): void {};`,
options: [
{
variableDeclaration: true,
variableDeclarationIgnoreFunction: true,
},
],
},
{
code: `
class Foo {
a = (): void => {};
b = function (): void {};
}
`,
options: [
{
variableDeclaration: true,
variableDeclarationIgnoreFunction: true,
},
],
},
],
invalid: [
// Array destructuring
Expand Down Expand Up @@ -652,5 +703,74 @@ ruleTester.run('typedef', rule, {
},
],
},
{
code: `const foo = 'foo'`,
errors: [
{
messageId: 'expectedTypedefNamed',
data: { name: 'foo' },
},
],
options: [
{
variableDeclaration: true,
variableDeclarationIgnoreFunction: true,
},
],
},
{
code: `const foo = function(): void {};`,
errors: [
{
messageId: 'expectedTypedefNamed',
data: { name: 'foo' },
},
],
options: [
{
variableDeclaration: true,
variableDeclarationIgnoreFunction: false,
},
],
},
{
code: `const foo = (): void => {};`,
errors: [
{
messageId: 'expectedTypedefNamed',
data: { name: 'foo' },
},
],
options: [
{
variableDeclaration: true,
variableDeclarationIgnoreFunction: false,
},
],
},
{
code: `
class Foo {
a = (): void => {};
b = function (): void {};
}
`,
errors: [
{
messageId: 'expectedTypedefNamed',
data: { name: 'a' },
},
{
messageId: 'expectedTypedefNamed',
data: { name: 'b' },
},
],
options: [
{
variableDeclaration: true,
variableDeclarationIgnoreFunction: false,
},
],
},
],
});