@typescript-eslint/eslint-plugin 8.25.1-alpha.16 → 8.25.1-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rules/explicit-module-boundary-types.d.ts +1 -0
- package/dist/rules/explicit-module-boundary-types.d.ts.map +1 -1
- package/dist/rules/explicit-module-boundary-types.js +14 -0
- package/dist/rules/no-unnecessary-type-assertion.d.ts.map +1 -1
- package/dist/rules/no-unnecessary-type-assertion.js +4 -0
- package/dist/rules/unified-signatures.d.ts +1 -0
- package/dist/rules/unified-signatures.d.ts.map +1 -1
- package/dist/rules/unified-signatures.js +22 -1
- package/docs/rules/explicit-module-boundary-types.mdx +14 -0
- package/docs/rules/unified-signatures.mdx +44 -0
- package/package.json +7 -7
@@ -5,6 +5,7 @@ export type Options = [
|
|
5
5
|
allowedNames?: string[];
|
6
6
|
allowHigherOrderFunctions?: boolean;
|
7
7
|
allowTypedFunctionExpressions?: boolean;
|
8
|
+
allowOverloadFunctions?: boolean;
|
8
9
|
}
|
9
10
|
];
|
10
11
|
export type MessageIds = 'anyTypedArg' | 'anyTypedArgUnnamed' | 'missingArgType' | 'missingArgTypeUnnamed' | 'missingReturnType';
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"explicit-module-boundary-types.d.ts","sourceRoot":"","sources":["../../src/rules/explicit-module-boundary-types.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"explicit-module-boundary-types.d.ts","sourceRoot":"","sources":["../../src/rules/explicit-module-boundary-types.ts"],"names":[],"mappings":"AAyBA,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,kCAAkC,CAAC,EAAE,OAAO,CAAC;QAC7C,yCAAyC,CAAC,EAAE,OAAO,CAAC;QACpD,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;QACxB,yBAAyB,CAAC,EAAE,OAAO,CAAC;QACpC,6BAA6B,CAAC,EAAE,OAAO,CAAC;QACxC,sBAAsB,CAAC,EAAE,OAAO,CAAC;KAClC;CACF,CAAC;AACF,MAAM,MAAM,UAAU,GAClB,aAAa,GACb,oBAAoB,GACpB,gBAAgB,GAChB,uBAAuB,GACvB,mBAAmB,CAAC;;AAExB,wBAweG"}
|
@@ -48,6 +48,10 @@ exports.default = (0, util_1.createRule)({
|
|
48
48
|
'You must still type the parameters of the function.',
|
49
49
|
].join('\n'),
|
50
50
|
},
|
51
|
+
allowOverloadFunctions: {
|
52
|
+
type: 'boolean',
|
53
|
+
description: 'Whether to ignore return type annotations on functions with overload signatures.',
|
54
|
+
},
|
51
55
|
allowTypedFunctionExpressions: {
|
52
56
|
type: 'boolean',
|
53
57
|
description: 'Whether to ignore type annotations on the variable of a function expression.',
|
@@ -62,6 +66,7 @@ exports.default = (0, util_1.createRule)({
|
|
62
66
|
allowDirectConstAssertionInArrowFunctions: true,
|
63
67
|
allowedNames: [],
|
64
68
|
allowHigherOrderFunctions: true,
|
69
|
+
allowOverloadFunctions: false,
|
65
70
|
allowTypedFunctionExpressions: true,
|
66
71
|
},
|
67
72
|
],
|
@@ -342,6 +347,11 @@ exports.default = (0, util_1.createRule)({
|
|
342
347
|
(0, explicitReturnTypeUtils_1.ancestorHasReturnType)(node)) {
|
343
348
|
return;
|
344
349
|
}
|
350
|
+
if (options.allowOverloadFunctions &&
|
351
|
+
node.parent.type === utils_1.AST_NODE_TYPES.MethodDefinition &&
|
352
|
+
(0, util_1.hasOverloadSignatures)(node.parent, context)) {
|
353
|
+
return;
|
354
|
+
}
|
345
355
|
(0, explicitReturnTypeUtils_1.checkFunctionExpressionReturnType)({ node, returns }, options, context.sourceCode, loc => {
|
346
356
|
context.report({
|
347
357
|
loc,
|
@@ -359,6 +369,10 @@ exports.default = (0, util_1.createRule)({
|
|
359
369
|
if (isAllowedName(node) || (0, explicitReturnTypeUtils_1.ancestorHasReturnType)(node)) {
|
360
370
|
return;
|
361
371
|
}
|
372
|
+
if (options.allowOverloadFunctions &&
|
373
|
+
(0, util_1.hasOverloadSignatures)(node, context)) {
|
374
|
+
return;
|
375
|
+
}
|
362
376
|
(0, explicitReturnTypeUtils_1.checkFunctionReturnType)({ node, returns }, options, context.sourceCode, loc => {
|
363
377
|
context.report({
|
364
378
|
loc,
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"no-unnecessary-type-assertion.d.ts","sourceRoot":"","sources":["../../src/rules/no-unnecessary-type-assertion.ts"],"names":[],"mappings":"AAqBA,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;KAC1B;CACF,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,yBAAyB,GAAG,sBAAsB,CAAC;;AAE5E,
|
1
|
+
{"version":3,"file":"no-unnecessary-type-assertion.d.ts","sourceRoot":"","sources":["../../src/rules/no-unnecessary-type-assertion.ts"],"names":[],"mappings":"AAqBA,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;KAC1B;CACF,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,yBAAyB,GAAG,sBAAsB,CAAC;;AAE5E,wBAwXG"}
|
@@ -260,6 +260,10 @@ exports.default = (0, util_1.createRule)({
|
|
260
260
|
// so figure out if the variable is used in a place that accepts nullable types
|
261
261
|
const contextualType = (0, util_1.getContextualType)(checker, originalNode);
|
262
262
|
if (contextualType) {
|
263
|
+
if ((0, util_1.isTypeFlagSet)(type, ts.TypeFlags.Unknown) &&
|
264
|
+
!(0, util_1.isTypeFlagSet)(contextualType, ts.TypeFlags.Unknown)) {
|
265
|
+
return;
|
266
|
+
}
|
263
267
|
// in strict mode you can't assign null to undefined, so we have to make sure that
|
264
268
|
// the two types share a nullable type
|
265
269
|
const typeIncludesUndefined = (0, util_1.isTypeFlagSet)(type, ts.TypeFlags.Undefined);
|
@@ -2,6 +2,7 @@ export type MessageIds = 'omittingRestParameter' | 'omittingSingleParameter' | '
|
|
2
2
|
export type Options = [
|
3
3
|
{
|
4
4
|
ignoreDifferentlyNamedParameters?: boolean;
|
5
|
+
ignoreOverloadsWithDifferentJSDoc?: boolean;
|
5
6
|
}
|
6
7
|
];
|
7
8
|
declare const _default: import("@typescript-eslint/utils/ts-eslint").RuleModule<MessageIds, Options, import("../../rules").ESLintPluginDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"unified-signatures.d.ts","sourceRoot":"","sources":["../../src/rules/unified-signatures.ts"],"names":[],"mappings":"AAuDA,MAAM,MAAM,UAAU,GAClB,uBAAuB,GACvB,yBAAyB,GACzB,2BAA2B,CAAC;AAEhC,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,gCAAgC,CAAC,EAAE,OAAO,CAAC;
|
1
|
+
{"version":3,"file":"unified-signatures.d.ts","sourceRoot":"","sources":["../../src/rules/unified-signatures.ts"],"names":[],"mappings":"AAuDA,MAAM,MAAM,UAAU,GAClB,uBAAuB,GACvB,yBAAyB,GACzB,2BAA2B,CAAC;AAEhC,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,gCAAgC,CAAC,EAAE,OAAO,CAAC;QAC3C,iCAAiC,CAAC,EAAE,OAAO,CAAC;KAC7C;CACF,CAAC;;AAEF,wBAyiBG"}
|
@@ -25,6 +25,10 @@ exports.default = (0, util_1.createRule)({
|
|
25
25
|
type: 'boolean',
|
26
26
|
description: 'Whether two parameters with different names at the same index should be considered different even if their types are the same.',
|
27
27
|
},
|
28
|
+
ignoreOverloadsWithDifferentJSDoc: {
|
29
|
+
type: 'boolean',
|
30
|
+
description: 'Whether two overloads with different JSDoc comments should be considered different even if their parameter and return types are the same.',
|
31
|
+
},
|
28
32
|
},
|
29
33
|
},
|
30
34
|
],
|
@@ -32,9 +36,10 @@ exports.default = (0, util_1.createRule)({
|
|
32
36
|
defaultOptions: [
|
33
37
|
{
|
34
38
|
ignoreDifferentlyNamedParameters: false,
|
39
|
+
ignoreOverloadsWithDifferentJSDoc: false,
|
35
40
|
},
|
36
41
|
],
|
37
|
-
create(context, [{ ignoreDifferentlyNamedParameters }]) {
|
42
|
+
create(context, [{ ignoreDifferentlyNamedParameters, ignoreOverloadsWithDifferentJSDoc }]) {
|
38
43
|
//----------------------------------------------------------------------
|
39
44
|
// Helpers
|
40
45
|
//----------------------------------------------------------------------
|
@@ -126,6 +131,13 @@ exports.default = (0, util_1.createRule)({
|
|
126
131
|
}
|
127
132
|
}
|
128
133
|
}
|
134
|
+
if (ignoreOverloadsWithDifferentJSDoc) {
|
135
|
+
const aComment = getBlockCommentForNode(getExportingNode(a) ?? a);
|
136
|
+
const bComment = getBlockCommentForNode(getExportingNode(b) ?? b);
|
137
|
+
if (aComment?.value !== bComment?.value) {
|
138
|
+
return false;
|
139
|
+
}
|
140
|
+
}
|
129
141
|
return (typesAreEqual(a.returnType, b.returnType) &&
|
130
142
|
// Must take the same type parameters.
|
131
143
|
// If one uses a type parameter (from outside) and the other doesn't, they shouldn't be joined.
|
@@ -306,6 +318,15 @@ exports.default = (0, util_1.createRule)({
|
|
306
318
|
addFailures(failures);
|
307
319
|
currentScope = scopes.pop();
|
308
320
|
}
|
321
|
+
/**
|
322
|
+
* @returns the first valid JSDoc comment annotating `node`
|
323
|
+
*/
|
324
|
+
function getBlockCommentForNode(node) {
|
325
|
+
return context.sourceCode
|
326
|
+
.getCommentsBefore(node)
|
327
|
+
.reverse()
|
328
|
+
.find(comment => comment.type === utils_1.AST_TOKEN_TYPES.Block);
|
329
|
+
}
|
309
330
|
function addOverload(signature, key, containingNode) {
|
310
331
|
key ??= getOverloadKey(signature);
|
311
332
|
if (currentScope &&
|
@@ -260,6 +260,20 @@ export const foo: FooType = bar => {};
|
|
260
260
|
</TabItem>
|
261
261
|
</Tabs>
|
262
262
|
|
263
|
+
### `allowOverloadFunctions`
|
264
|
+
|
265
|
+
{/* insert option description */}
|
266
|
+
|
267
|
+
Examples of correct code when `allowOverloadFunctions` is set to `true`:
|
268
|
+
|
269
|
+
```ts option='{ "allowOverloadFunctions": true }' showPlaygroundButton
|
270
|
+
export function test(a: string): string;
|
271
|
+
export function test(a: number): number;
|
272
|
+
export function test(a: unknown) {
|
273
|
+
return a;
|
274
|
+
}
|
275
|
+
```
|
276
|
+
|
263
277
|
## When Not To Use It
|
264
278
|
|
265
279
|
If your project is not used by downstream consumers that are sensitive to API types, you can disable this rule.
|
@@ -78,6 +78,50 @@ function f(b: string): void;
|
|
78
78
|
</TabItem>
|
79
79
|
</Tabs>
|
80
80
|
|
81
|
+
### `ignoreOverloadsWithDifferentJSDoc`
|
82
|
+
|
83
|
+
{/* insert option description */}
|
84
|
+
|
85
|
+
Examples of code for this rule with `ignoreOverloadsWithDifferentJSDoc`:
|
86
|
+
|
87
|
+
<Tabs>
|
88
|
+
<TabItem value="❌ Incorrect">
|
89
|
+
|
90
|
+
```ts option='{ "ignoreOverloadsWithDifferentJSDoc": true }'
|
91
|
+
declare function f(x: string): void;
|
92
|
+
declare function f(x: boolean): void;
|
93
|
+
/**
|
94
|
+
* @deprecate
|
95
|
+
*/
|
96
|
+
declare function f(x: number): void;
|
97
|
+
/**
|
98
|
+
* @deprecate
|
99
|
+
*/
|
100
|
+
declare function f(x: null): void;
|
101
|
+
```
|
102
|
+
|
103
|
+
</TabItem>
|
104
|
+
<TabItem value="✅ Correct">
|
105
|
+
|
106
|
+
```ts option='{ "ignoreOverloadsWithDifferentJSDoc": true }'
|
107
|
+
declare function f(x: string): void;
|
108
|
+
/**
|
109
|
+
* This signature does something else.
|
110
|
+
*/
|
111
|
+
declare function f(x: boolean): void;
|
112
|
+
/**
|
113
|
+
* @async
|
114
|
+
*/
|
115
|
+
declare function f(x: number): void;
|
116
|
+
/**
|
117
|
+
* @deprecate
|
118
|
+
*/
|
119
|
+
declare function f(x: null): void;
|
120
|
+
```
|
121
|
+
|
122
|
+
</TabItem>
|
123
|
+
</Tabs>
|
124
|
+
|
81
125
|
## When Not To Use It
|
82
126
|
|
83
127
|
This is purely a stylistic rule to help with readability of function signature overloads.
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
3
|
-
"version": "8.25.1-alpha.
|
3
|
+
"version": "8.25.1-alpha.18",
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
5
5
|
"files": [
|
6
6
|
"dist",
|
@@ -62,10 +62,10 @@
|
|
62
62
|
},
|
63
63
|
"dependencies": {
|
64
64
|
"@eslint-community/regexpp": "^4.10.0",
|
65
|
-
"@typescript-eslint/scope-manager": "8.25.1-alpha.
|
66
|
-
"@typescript-eslint/type-utils": "8.25.1-alpha.
|
67
|
-
"@typescript-eslint/utils": "8.25.1-alpha.
|
68
|
-
"@typescript-eslint/visitor-keys": "8.25.1-alpha.
|
65
|
+
"@typescript-eslint/scope-manager": "8.25.1-alpha.18",
|
66
|
+
"@typescript-eslint/type-utils": "8.25.1-alpha.18",
|
67
|
+
"@typescript-eslint/utils": "8.25.1-alpha.18",
|
68
|
+
"@typescript-eslint/visitor-keys": "8.25.1-alpha.18",
|
69
69
|
"graphemer": "^1.4.0",
|
70
70
|
"ignore": "^5.3.1",
|
71
71
|
"natural-compare": "^1.4.0",
|
@@ -76,8 +76,8 @@
|
|
76
76
|
"@types/marked": "^5.0.2",
|
77
77
|
"@types/mdast": "^4.0.3",
|
78
78
|
"@types/natural-compare": "*",
|
79
|
-
"@typescript-eslint/rule-schema-to-typescript-types": "8.25.1-alpha.
|
80
|
-
"@typescript-eslint/rule-tester": "8.25.1-alpha.
|
79
|
+
"@typescript-eslint/rule-schema-to-typescript-types": "8.25.1-alpha.18",
|
80
|
+
"@typescript-eslint/rule-tester": "8.25.1-alpha.18",
|
81
81
|
"ajv": "^6.12.6",
|
82
82
|
"cross-env": "^7.0.3",
|
83
83
|
"cross-fetch": "*",
|