Skip to content

Commit

Permalink
feat(eslint-plugin): [typedef] add variable-declaration-ignore-functi…
Browse files Browse the repository at this point in the history
…on (#1578)
  • Loading branch information
a-tarasyuk committed Feb 29, 2020
1 parent 8333d41 commit fc0a55e
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 2 deletions.
27 changes: 26 additions & 1 deletion packages/eslint-plugin/docs/rules/typedef.md
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
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
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,
},
],
},
],
});

0 comments on commit fc0a55e

Please sign in to comment.