Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit caaa859

Browse files
authored
feat(eslint-plugin): [explicit-module-boundary-types] improve accuracy and coverage (typescript-eslint#2135)
1 parent 7925823 commit caaa859

File tree

6 files changed

+826
-444
lines changed

6 files changed

+826
-444
lines changed

.vscode/launch.json

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@
1818
],
1919
"sourceMaps": true,
2020
"console": "integratedTerminal",
21-
"internalConsoleOptions": "neverOpen"
21+
"internalConsoleOptions": "neverOpen",
22+
"skipFiles": [
23+
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
24+
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
25+
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
26+
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
27+
"${workspaceFolder}/packages/parser/src/index.ts",
28+
"${workspaceFolder}/packages/parser/dist/index.js",
29+
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
30+
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
31+
],
2232
},
2333
{
2434
"type": "node",
@@ -34,7 +44,17 @@
3444
],
3545
"sourceMaps": true,
3646
"console": "integratedTerminal",
37-
"internalConsoleOptions": "neverOpen"
47+
"internalConsoleOptions": "neverOpen",
48+
"skipFiles": [
49+
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
50+
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
51+
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
52+
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
53+
"${workspaceFolder}/packages/parser/src/index.ts",
54+
"${workspaceFolder}/packages/parser/dist/index.js",
55+
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
56+
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
57+
],
3858
},
3959
{
4060
"type": "node",
@@ -50,7 +70,17 @@
5070
],
5171
"sourceMaps": true,
5272
"console": "integratedTerminal",
53-
"internalConsoleOptions": "neverOpen"
73+
"internalConsoleOptions": "neverOpen",
74+
"skipFiles": [
75+
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
76+
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
77+
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
78+
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
79+
"${workspaceFolder}/packages/parser/src/index.ts",
80+
"${workspaceFolder}/packages/parser/dist/index.js",
81+
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
82+
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
83+
],
5484
},
5585
{
5686
"type": "node",
@@ -66,7 +96,17 @@
6696
],
6797
"sourceMaps": true,
6898
"console": "integratedTerminal",
69-
"internalConsoleOptions": "neverOpen"
99+
"internalConsoleOptions": "neverOpen",
100+
"skipFiles": [
101+
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
102+
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
103+
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
104+
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
105+
"${workspaceFolder}/packages/parser/src/index.ts",
106+
"${workspaceFolder}/packages/parser/dist/index.js",
107+
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
108+
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
109+
],
70110
},
71111
{
72112
"type": "node",
@@ -82,7 +122,17 @@
82122
],
83123
"sourceMaps": true,
84124
"console": "integratedTerminal",
85-
"internalConsoleOptions": "neverOpen"
125+
"internalConsoleOptions": "neverOpen",
126+
"skipFiles": [
127+
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
128+
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
129+
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
130+
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
131+
"${workspaceFolder}/packages/parser/src/index.ts",
132+
"${workspaceFolder}/packages/parser/dist/index.js",
133+
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
134+
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
135+
],
86136
}
87137
]
88138
}

packages/eslint-plugin/docs/rules/explicit-module-boundary-types.md

Lines changed: 88 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,9 @@ The rule accepts an options object with the following properties:
7171
```ts
7272
type Options = {
7373
/**
74-
* If true, type annotations are also allowed on the variable of a function expression
75-
* rather than on the function arguments/return value directly.
76-
*/
77-
allowTypedFunctionExpressions?: boolean;
78-
/**
79-
* If true, functions immediately returning another function expression will not
80-
* require an explicit return value annotation.
81-
* You must still type the parameters of the function.
74+
* If true, the rule will not report for arguments that are explicitly typed as `any`
8275
*/
83-
allowHigherOrderFunctions?: boolean;
76+
allowArgumentsExplicitlyTypedAsAny?: boolean;
8477
/**
8578
* If true, body-less arrow functions that return an `as const` type assertion will not
8679
* require an explicit return value annotation.
@@ -92,16 +85,24 @@ type Options = {
9285
*/
9386
allowedNames?: string[];
9487
/**
95-
* If true, track references to exported variables as well as direct exports.
88+
* If true, functions immediately returning another function expression will not
89+
* require an explicit return value annotation.
90+
* You must still type the parameters of the function.
91+
*/
92+
allowHigherOrderFunctions?: boolean;
93+
/**
94+
* If true, type annotations are also allowed on the variable of a function expression
95+
* rather than on the function arguments/return value directly.
9696
*/
97-
shouldTrackReferences?: boolean;
97+
allowTypedFunctionExpressions?: boolean;
9898
};
9999

100100
const defaults = {
101-
allowTypedFunctionExpressions: true,
102-
allowHigherOrderFunctions: true,
101+
allowArgumentsExplicitlyTypedAsAny: false,
102+
allowDirectConstAssertionInArrowFunctions: true,
103103
allowedNames: [],
104-
shouldTrackReferences: true,
104+
allowHigherOrderFunctions: true,
105+
allowTypedFunctionExpressions: true,
105106
};
106107
```
107108

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

130-
### `allowTypedFunctionExpressions`
131-
132-
Examples of **incorrect** code for this rule with `{ allowTypedFunctionExpressions: true }`:
133-
134-
```ts
135-
export let arrowFn = () => 'test';
136-
137-
export let funcExpr = function () {
138-
return 'test';
139-
};
140-
141-
export let objectProp = {
142-
foo: () => 1,
143-
};
144-
145-
export const foo = bar => {};
146-
```
131+
### `allowArgumentsExplicitlyTypedAsAny`
147132

148-
Examples of additional **correct** code for this rule with `{ allowTypedFunctionExpressions: true }`:
133+
Examples of **incorrect** code for this rule with `{ allowArgumentsExplicitlyTypedAsAny: true }`:
149134

150135
```ts
151-
type FuncType = () => string;
152-
153-
export let arrowFn: FuncType = () => 'test';
154-
155-
export let funcExpr: FuncType = function () {
156-
return 'test';
157-
};
158-
159-
export let asTyped = (() => '') as () => string;
160-
export let castTyped = <() => string>(() => '');
161-
162-
interface ObjectType {
163-
foo(): number;
164-
}
165-
export let objectProp: ObjectType = {
166-
foo: () => 1,
167-
};
168-
export let objectPropAs = {
169-
foo: () => 1,
170-
} as ObjectType;
171-
export let objectPropCast = <ObjectType>{
172-
foo: () => 1,
173-
};
174-
175-
type FooType = (bar: string) => void;
176-
export const foo: FooType = bar => {};
177-
```
178-
179-
### `allowHigherOrderFunctions`
180-
181-
Examples of **incorrect** code for this rule with `{ allowHigherOrderFunctions: true }`:
182-
183-
```ts
184-
export var arrowFn = () => () => {};
185-
186-
export function fn() {
187-
return function () {};
188-
}
189-
190-
export function foo(outer) {
191-
return function (inner): void {};
192-
}
136+
export const func = (value: any): void => ({ type: 'X', value });
137+
export function foo(value: any): void {}
193138
```
194139

195-
Examples of **correct** code for this rule with `{ allowHigherOrderFunctions: true }`:
140+
Examples of **correct** code for this rule with `{ allowArgumentsExplicitlyTypedAsAny: true }`:
196141

197142
```ts
198-
export var arrowFn = () => (): void => {};
199-
200-
export function fn() {
201-
return function (): void {};
202-
}
203-
204-
export function foo(outer: string) {
205-
return function (inner: string): void {};
206-
}
143+
export const func = (value: number): void => ({ type: 'X', value });
144+
export function foo(value: number): void {}
207145
```
208146

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

251-
### `shouldTrackReferences`
189+
### `allowHigherOrderFunctions`
252190

253-
Examples of **incorrect** code for this rule with `{ shouldTrackReferences: true }`:
191+
Examples of **incorrect** code for this rule with `{ allowHigherOrderFunctions: true }`:
254192

255193
```ts
256-
function foo(bar) {
257-
return bar;
194+
export var arrowFn = () => () => {};
195+
196+
export function fn() {
197+
return function () {};
258198
}
259199

260-
export default foo;
200+
export function foo(outer) {
201+
return function (inner): void {};
202+
}
261203
```
262204

263-
Examples of **correct** code for this rule with `{ shouldTrackReferences: true }`:
205+
Examples of **correct** code for this rule with `{ allowHigherOrderFunctions: true }`:
264206

265207
```ts
266-
function foo(bar: string): string {
267-
return bar;
208+
export var arrowFn = () => (): void => {};
209+
210+
export function fn() {
211+
return function (): void {};
212+
}
213+
214+
export function foo(outer: string) {
215+
return function (inner: string): void {};
268216
}
217+
```
218+
219+
### `allowTypedFunctionExpressions`
220+
221+
Examples of **incorrect** code for this rule with `{ allowTypedFunctionExpressions: true }`:
269222

270-
export default foo;
223+
```ts
224+
export let arrowFn = () => 'test';
225+
226+
export let funcExpr = function () {
227+
return 'test';
228+
};
229+
230+
export let objectProp = {
231+
foo: () => 1,
232+
};
233+
234+
export const foo = bar => {};
235+
```
236+
237+
Examples of additional **correct** code for this rule with `{ allowTypedFunctionExpressions: true }`:
238+
239+
```ts
240+
type FuncType = () => string;
241+
242+
export let arrowFn: FuncType = () => 'test';
243+
244+
export let funcExpr: FuncType = function () {
245+
return 'test';
246+
};
247+
248+
export let asTyped = (() => '') as () => string;
249+
export let castTyped = <() => string>(() => '');
250+
251+
interface ObjectType {
252+
foo(): number;
253+
}
254+
export let objectProp: ObjectType = {
255+
foo: () => 1,
256+
};
257+
export let objectPropAs = {
258+
foo: () => 1,
259+
} as ObjectType;
260+
export let objectPropCast = <ObjectType>{
261+
foo: () => 1,
262+
};
263+
264+
type FooType = (bar: string) => void;
265+
export const foo: FooType = bar => {};
271266
```
272267

273268
## When Not To Use It

0 commit comments

Comments
 (0)