Skip to content

Commit

Permalink
feat(eslint-plugin): [explicit-module-boundary-types] improve accurac…
Browse files Browse the repository at this point in the history
…y and coverage (#2135)
  • Loading branch information
bradzacher committed May 30, 2020
1 parent 7925823 commit caaa859
Show file tree
Hide file tree
Showing 6 changed files with 826 additions and 444 deletions.
60 changes: 55 additions & 5 deletions .vscode/launch.json
Expand Up @@ -18,7 +18,17 @@
],
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/parser/src/index.ts",
"${workspaceFolder}/packages/parser/dist/index.js",
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
],
},
{
"type": "node",
Expand All @@ -34,7 +44,17 @@
],
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/parser/src/index.ts",
"${workspaceFolder}/packages/parser/dist/index.js",
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
],
},
{
"type": "node",
Expand All @@ -50,7 +70,17 @@
],
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/parser/src/index.ts",
"${workspaceFolder}/packages/parser/dist/index.js",
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
],
},
{
"type": "node",
Expand All @@ -66,7 +96,17 @@
],
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/parser/src/index.ts",
"${workspaceFolder}/packages/parser/dist/index.js",
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
],
},
{
"type": "node",
Expand All @@ -82,7 +122,17 @@
],
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/parser/src/index.ts",
"${workspaceFolder}/packages/parser/dist/index.js",
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
],
}
]
}
181 changes: 88 additions & 93 deletions packages/eslint-plugin/docs/rules/explicit-module-boundary-types.md
Expand Up @@ -71,16 +71,9 @@ The rule accepts an options object with the following properties:
```ts
type Options = {
/**
* If true, type annotations are also allowed on the variable of a function expression
* rather than on the function arguments/return value directly.
*/
allowTypedFunctionExpressions?: boolean;
/**
* If true, functions immediately returning another function expression will not
* require an explicit return value annotation.
* You must still type the parameters of the function.
* If true, the rule will not report for arguments that are explicitly typed as `any`
*/
allowHigherOrderFunctions?: boolean;
allowArgumentsExplicitlyTypedAsAny?: boolean;
/**
* If true, body-less arrow functions that return an `as const` type assertion will not
* require an explicit return value annotation.
Expand All @@ -92,16 +85,24 @@ type Options = {
*/
allowedNames?: string[];
/**
* If true, track references to exported variables as well as direct exports.
* If true, functions immediately returning another function expression will not
* require an explicit return value annotation.
* You must still type the parameters of the function.
*/
allowHigherOrderFunctions?: boolean;
/**
* If true, type annotations are also allowed on the variable of a function expression
* rather than on the function arguments/return value directly.
*/
shouldTrackReferences?: boolean;
allowTypedFunctionExpressions?: boolean;
};

const defaults = {
allowTypedFunctionExpressions: true,
allowHigherOrderFunctions: true,
allowArgumentsExplicitlyTypedAsAny: false,
allowDirectConstAssertionInArrowFunctions: true,
allowedNames: [],
shouldTrackReferences: true,
allowHigherOrderFunctions: true,
allowTypedFunctionExpressions: true,
};
```

Expand All @@ -127,83 +128,20 @@ If you are working on a codebase within which you lint non-TypeScript code (i.e.
}
```

### `allowTypedFunctionExpressions`

Examples of **incorrect** code for this rule with `{ allowTypedFunctionExpressions: true }`:

```ts
export let arrowFn = () => 'test';

export let funcExpr = function () {
return 'test';
};

export let objectProp = {
foo: () => 1,
};

export const foo = bar => {};
```
### `allowArgumentsExplicitlyTypedAsAny`

Examples of additional **correct** code for this rule with `{ allowTypedFunctionExpressions: true }`:
Examples of **incorrect** code for this rule with `{ allowArgumentsExplicitlyTypedAsAny: true }`:

```ts
type FuncType = () => string;

export let arrowFn: FuncType = () => 'test';

export let funcExpr: FuncType = function () {
return 'test';
};

export let asTyped = (() => '') as () => string;
export let castTyped = <() => string>(() => '');

interface ObjectType {
foo(): number;
}
export let objectProp: ObjectType = {
foo: () => 1,
};
export let objectPropAs = {
foo: () => 1,
} as ObjectType;
export let objectPropCast = <ObjectType>{
foo: () => 1,
};

type FooType = (bar: string) => void;
export const foo: FooType = bar => {};
```

### `allowHigherOrderFunctions`

Examples of **incorrect** code for this rule with `{ allowHigherOrderFunctions: true }`:

```ts
export var arrowFn = () => () => {};

export function fn() {
return function () {};
}

export function foo(outer) {
return function (inner): void {};
}
export const func = (value: any): void => ({ type: 'X', value });
export function foo(value: any): void {}
```

Examples of **correct** code for this rule with `{ allowHigherOrderFunctions: true }`:
Examples of **correct** code for this rule with `{ allowArgumentsExplicitlyTypedAsAny: true }`:

```ts
export var arrowFn = () => (): void => {};

export function fn() {
return function (): void {};
}

export function foo(outer: string) {
return function (inner: string): void {};
}
export const func = (value: number): void => ({ type: 'X', value });
export function foo(value: number): void {}
```

### `allowDirectConstAssertionInArrowFunctions`
Expand Down Expand Up @@ -248,26 +186,83 @@ You may pass function/method names you would like this rule to ignore, like so:
}
```

### `shouldTrackReferences`
### `allowHigherOrderFunctions`

Examples of **incorrect** code for this rule with `{ shouldTrackReferences: true }`:
Examples of **incorrect** code for this rule with `{ allowHigherOrderFunctions: true }`:

```ts
function foo(bar) {
return bar;
export var arrowFn = () => () => {};

export function fn() {
return function () {};
}

export default foo;
export function foo(outer) {
return function (inner): void {};
}
```

Examples of **correct** code for this rule with `{ shouldTrackReferences: true }`:
Examples of **correct** code for this rule with `{ allowHigherOrderFunctions: true }`:

```ts
function foo(bar: string): string {
return bar;
export var arrowFn = () => (): void => {};

export function fn() {
return function (): void {};
}

export function foo(outer: string) {
return function (inner: string): void {};
}
```

### `allowTypedFunctionExpressions`

Examples of **incorrect** code for this rule with `{ allowTypedFunctionExpressions: true }`:

export default foo;
```ts
export let arrowFn = () => 'test';

export let funcExpr = function () {
return 'test';
};

export let objectProp = {
foo: () => 1,
};

export const foo = bar => {};
```

Examples of additional **correct** code for this rule with `{ allowTypedFunctionExpressions: true }`:

```ts
type FuncType = () => string;

export let arrowFn: FuncType = () => 'test';

export let funcExpr: FuncType = function () {
return 'test';
};

export let asTyped = (() => '') as () => string;
export let castTyped = <() => string>(() => '');

interface ObjectType {
foo(): number;
}
export let objectProp: ObjectType = {
foo: () => 1,
};
export let objectPropAs = {
foo: () => 1,
} as ObjectType;
export let objectPropCast = <ObjectType>{
foo: () => 1,
};

type FooType = (bar: string) => void;
export const foo: FooType = bar => {};
```

## When Not To Use It
Expand Down

0 comments on commit caaa859

Please sign in to comment.