@typescript-eslint/eslint-plugin 8.6.1-alpha.6 → 8.6.1-alpha.8
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/consistent-type-exports.js +86 -10
- package/dist/rules/consistent-type-exports.js.map +1 -1
- package/dist/rules/no-confusing-non-null-assertion.js +96 -29
- package/dist/rules/no-confusing-non-null-assertion.js.map +1 -1
- package/dist/rules/no-deprecated.js +83 -37
- package/dist/rules/no-deprecated.js.map +1 -1
- package/dist/rules/no-unsafe-call.js +39 -4
- package/dist/rules/no-unsafe-call.js.map +1 -1
- package/dist/util/index.js +0 -1
- package/dist/util/index.js.map +1 -1
- package/docs/rules/no-confusing-non-null-assertion.mdx +16 -3
- package/docs/rules/no-floating-promises.mdx +13 -6
- package/docs/rules/no-unnecessary-type-parameters.mdx +122 -1
- package/docs/rules/no-unsafe-call.mdx +28 -0
- package/docs/rules/no-unsafe-function-type.mdx +1 -0
- package/package.json +7 -7
@@ -1,7 +1,31 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
26
|
const utils_1 = require("@typescript-eslint/utils");
|
4
|
-
const
|
27
|
+
const tsutils = __importStar(require("ts-api-utils"));
|
28
|
+
const ts = __importStar(require("typescript"));
|
5
29
|
const util_1 = require("../util");
|
6
30
|
exports.default = (0, util_1.createRule)({
|
7
31
|
name: 'consistent-type-exports',
|
@@ -38,27 +62,79 @@ exports.default = (0, util_1.createRule)({
|
|
38
62
|
create(context, [{ fixMixedExportsWithInlineTypeSpecifier }]) {
|
39
63
|
const sourceExportsMap = {};
|
40
64
|
const services = (0, util_1.getParserServices)(context);
|
65
|
+
const checker = services.program.getTypeChecker();
|
41
66
|
/**
|
42
|
-
* Helper for identifying if
|
67
|
+
* Helper for identifying if a symbol resolves to a
|
43
68
|
* JavaScript value or a TypeScript type.
|
44
69
|
*
|
45
70
|
* @returns True/false if is a type or not, or undefined if the specifier
|
46
71
|
* can't be resolved.
|
47
72
|
*/
|
48
|
-
function
|
49
|
-
const checker = services.program.getTypeChecker();
|
50
|
-
const symbol = services.getSymbolAtLocation(specifier.exported);
|
73
|
+
function isSymbolTypeBased(symbol) {
|
51
74
|
if (!symbol) {
|
52
75
|
return undefined;
|
53
76
|
}
|
54
|
-
const aliasedSymbol =
|
55
|
-
|
56
|
-
|
77
|
+
const aliasedSymbol = tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)
|
78
|
+
? checker.getAliasedSymbol(symbol)
|
79
|
+
: symbol;
|
80
|
+
if (checker.isUnknownSymbol(aliasedSymbol)) {
|
57
81
|
return undefined;
|
58
82
|
}
|
59
|
-
return !(aliasedSymbol.flags &
|
83
|
+
return !(aliasedSymbol.flags & ts.SymbolFlags.Value);
|
60
84
|
}
|
61
85
|
return {
|
86
|
+
ExportAllDeclaration(node) {
|
87
|
+
if (node.exportKind === 'type') {
|
88
|
+
return;
|
89
|
+
}
|
90
|
+
const sourceModule = ts.resolveModuleName(node.source.value, context.filename, services.program.getCompilerOptions(), ts.sys);
|
91
|
+
if (sourceModule.resolvedModule == null) {
|
92
|
+
return;
|
93
|
+
}
|
94
|
+
const sourceFile = services.program.getSourceFile(sourceModule.resolvedModule.resolvedFileName);
|
95
|
+
if (sourceFile == null) {
|
96
|
+
return;
|
97
|
+
}
|
98
|
+
const sourceFileSymbol = checker.getSymbolAtLocation(sourceFile);
|
99
|
+
if (sourceFileSymbol == null) {
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
const sourceFileType = checker.getTypeOfSymbol(sourceFileSymbol);
|
103
|
+
// Module can explicitly export types or values, and it's not difficult
|
104
|
+
// to distinguish one from the other, since we can get the flags of
|
105
|
+
// the exported symbols or check if symbol export declaration has
|
106
|
+
// the "type" keyword in it.
|
107
|
+
//
|
108
|
+
// Things get a lot more complicated when we're dealing with
|
109
|
+
// export * from './module-with-type-only-exports'
|
110
|
+
// export type * from './module-with-type-and-value-exports'
|
111
|
+
//
|
112
|
+
// TS checker has an internal function getExportsOfModuleWorker that
|
113
|
+
// recursively visits all module exports, including "export *". It then
|
114
|
+
// puts type-only-star-exported symbols into the typeOnlyExportStarMap
|
115
|
+
// property of sourceFile's SymbolLinks. Since symbol links aren't
|
116
|
+
// exposed outside the checker, we cannot access it directly.
|
117
|
+
//
|
118
|
+
// Therefore, to filter out value properties, we use the following hack:
|
119
|
+
// checker.getPropertiesOfType returns all exports that were originally
|
120
|
+
// values, but checker.getPropertyOfType returns undefined for
|
121
|
+
// properties that are mentioned in the typeOnlyExportStarMap.
|
122
|
+
const isThereAnyExportedValue = checker
|
123
|
+
.getPropertiesOfType(sourceFileType)
|
124
|
+
.some(propertyTypeSymbol => checker.getPropertyOfType(sourceFileType, propertyTypeSymbol.escapedName.toString()) != null);
|
125
|
+
if (isThereAnyExportedValue) {
|
126
|
+
return;
|
127
|
+
}
|
128
|
+
context.report({
|
129
|
+
node,
|
130
|
+
messageId: 'typeOverValue',
|
131
|
+
fix(fixer) {
|
132
|
+
const asteriskToken = (0, util_1.nullThrows)(context.sourceCode.getFirstToken(node, token => token.type === utils_1.AST_TOKEN_TYPES.Punctuator &&
|
133
|
+
token.value === '*'), util_1.NullThrowsReasons.MissingToken('asterisk', 'export all declaration'));
|
134
|
+
return fixer.insertTextBefore(asteriskToken, 'type ');
|
135
|
+
},
|
136
|
+
});
|
137
|
+
},
|
62
138
|
ExportNamedDeclaration(node) {
|
63
139
|
// Coerce the source into a string for use as a lookup entry.
|
64
140
|
const source = getSourceFromExport(node) ?? 'undefined';
|
@@ -93,7 +169,7 @@ exports.default = (0, util_1.createRule)({
|
|
93
169
|
inlineTypeSpecifiers.push(specifier);
|
94
170
|
continue;
|
95
171
|
}
|
96
|
-
const isTypeBased =
|
172
|
+
const isTypeBased = isSymbolTypeBased(services.getSymbolAtLocation(specifier.exported));
|
97
173
|
if (isTypeBased === true) {
|
98
174
|
typeBasedSpecifiers.push(specifier);
|
99
175
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"consistent-type-exports.js","sourceRoot":"","sources":["../../src/rules/consistent-type-exports.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"consistent-type-exports.js","sourceRoot":"","sources":["../../src/rules/consistent-type-exports.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,oDAA2E;AAC3E,sDAAwC;AACxC,+CAAiC;AAEjC,kCAQiB;AA2BjB,kBAAe,IAAA,iBAAU,EAAsB;IAC7C,IAAI,EAAE,yBAAyB;IAC/B,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EAAE,0CAA0C;YACvD,oBAAoB,EAAE,IAAI;SAC3B;QACD,QAAQ,EAAE;YACR,aAAa,EAC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value: true });
|
3
3
|
const utils_1 = require("@typescript-eslint/utils");
|
4
4
|
const util_1 = require("../util");
|
5
|
+
const confusingOperators = new Set([
|
6
|
+
'=',
|
7
|
+
'==',
|
8
|
+
'===',
|
9
|
+
'in',
|
10
|
+
'instanceof',
|
11
|
+
]);
|
12
|
+
function isConfusingOperator(operator) {
|
13
|
+
return confusingOperators.has(operator);
|
14
|
+
}
|
5
15
|
exports.default = (0, util_1.createRule)({
|
6
16
|
name: 'no-confusing-non-null-assertion',
|
7
17
|
meta: {
|
@@ -12,57 +22,108 @@ exports.default = (0, util_1.createRule)({
|
|
12
22
|
},
|
13
23
|
hasSuggestions: true,
|
14
24
|
messages: {
|
15
|
-
confusingEqual: 'Confusing
|
16
|
-
confusingAssign: 'Confusing
|
17
|
-
|
18
|
-
|
19
|
-
|
25
|
+
confusingEqual: 'Confusing combination of non-null assertion and equality test like `a! == b`, which looks very similar to `a !== b`.',
|
26
|
+
confusingAssign: 'Confusing combination of non-null assertion and assignment like `a! = b`, which looks very similar to `a != b`.',
|
27
|
+
confusingOperator: 'Confusing combination of non-null assertion and `{{operator}}` operator like `a! {{operator}} b`, which might be misinterpreted as `!(a {{operator}} b)`.',
|
28
|
+
notNeedInEqualTest: 'Remove unnecessary non-null assertion (!) in equality test.',
|
29
|
+
notNeedInAssign: 'Remove unnecessary non-null assertion (!) in assignment left-hand side.',
|
30
|
+
notNeedInOperator: 'Remove possibly unnecessary non-null assertion (!) in the left operand of the `{{operator}}` operator.',
|
31
|
+
wrapUpLeft: 'Wrap the left-hand side in parentheses to avoid confusion with "{{operator}}" operator.',
|
20
32
|
},
|
21
33
|
schema: [],
|
22
34
|
},
|
23
35
|
defaultOptions: [],
|
24
36
|
create(context) {
|
37
|
+
function confusingOperatorToMessageData(operator) {
|
38
|
+
switch (operator) {
|
39
|
+
case '=':
|
40
|
+
return {
|
41
|
+
messageId: 'confusingAssign',
|
42
|
+
};
|
43
|
+
case '==':
|
44
|
+
case '===':
|
45
|
+
return {
|
46
|
+
messageId: 'confusingEqual',
|
47
|
+
};
|
48
|
+
case 'in':
|
49
|
+
case 'instanceof':
|
50
|
+
return {
|
51
|
+
messageId: 'confusingOperator',
|
52
|
+
data: { operator },
|
53
|
+
};
|
54
|
+
// istanbul ignore next
|
55
|
+
default:
|
56
|
+
operator;
|
57
|
+
throw new Error(`Unexpected operator ${operator}`);
|
58
|
+
}
|
59
|
+
}
|
25
60
|
return {
|
26
61
|
'BinaryExpression, AssignmentExpression'(node) {
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
node.operator === '=') {
|
33
|
-
const isAssign = node.operator === '=';
|
62
|
+
const operator = node.operator;
|
63
|
+
if (isConfusingOperator(operator)) {
|
64
|
+
// Look for a non-null assertion as the last token on the left hand side.
|
65
|
+
// That way, we catch things like `1 + two! === 3`, even though the left
|
66
|
+
// hand side isn't a non-null assertion AST node.
|
34
67
|
const leftHandFinalToken = context.sourceCode.getLastToken(node.left);
|
35
68
|
const tokenAfterLeft = context.sourceCode.getTokenAfter(node.left);
|
36
69
|
if (leftHandFinalToken?.type === utils_1.AST_TOKEN_TYPES.Punctuator &&
|
37
70
|
leftHandFinalToken.value === '!' &&
|
38
71
|
tokenAfterLeft?.value !== ')') {
|
39
|
-
if (
|
72
|
+
if (node.left.type === utils_1.AST_NODE_TYPES.TSNonNullExpression) {
|
73
|
+
let suggestions;
|
74
|
+
switch (operator) {
|
75
|
+
case '=':
|
76
|
+
suggestions = [
|
77
|
+
{
|
78
|
+
messageId: 'notNeedInAssign',
|
79
|
+
fix: (fixer) => fixer.remove(leftHandFinalToken),
|
80
|
+
},
|
81
|
+
];
|
82
|
+
break;
|
83
|
+
case '==':
|
84
|
+
case '===':
|
85
|
+
suggestions = [
|
86
|
+
{
|
87
|
+
messageId: 'notNeedInEqualTest',
|
88
|
+
fix: (fixer) => fixer.remove(leftHandFinalToken),
|
89
|
+
},
|
90
|
+
];
|
91
|
+
break;
|
92
|
+
case 'in':
|
93
|
+
case 'instanceof':
|
94
|
+
suggestions = [
|
95
|
+
{
|
96
|
+
messageId: 'notNeedInOperator',
|
97
|
+
data: { operator },
|
98
|
+
fix: (fixer) => fixer.remove(leftHandFinalToken),
|
99
|
+
},
|
100
|
+
{
|
101
|
+
messageId: 'wrapUpLeft',
|
102
|
+
data: { operator },
|
103
|
+
fix: wrapUpLeftFixer(node),
|
104
|
+
},
|
105
|
+
];
|
106
|
+
break;
|
107
|
+
// istanbul ignore next
|
108
|
+
default:
|
109
|
+
operator;
|
110
|
+
return;
|
111
|
+
}
|
40
112
|
context.report({
|
41
113
|
node,
|
42
|
-
|
43
|
-
suggest:
|
44
|
-
{
|
45
|
-
messageId: isAssign
|
46
|
-
? 'notNeedInAssign'
|
47
|
-
: 'notNeedInEqualTest',
|
48
|
-
fix: (fixer) => [
|
49
|
-
fixer.remove(leftHandFinalToken),
|
50
|
-
],
|
51
|
-
},
|
52
|
-
],
|
114
|
+
...confusingOperatorToMessageData(operator),
|
115
|
+
suggest: suggestions,
|
53
116
|
});
|
54
117
|
}
|
55
118
|
else {
|
56
119
|
context.report({
|
57
120
|
node,
|
58
|
-
|
121
|
+
...confusingOperatorToMessageData(operator),
|
59
122
|
suggest: [
|
60
123
|
{
|
61
124
|
messageId: 'wrapUpLeft',
|
62
|
-
|
63
|
-
|
64
|
-
fixer.insertTextAfter(node.left, ')'),
|
65
|
-
],
|
125
|
+
data: { operator },
|
126
|
+
fix: wrapUpLeftFixer(node),
|
66
127
|
},
|
67
128
|
],
|
68
129
|
});
|
@@ -73,4 +134,10 @@ exports.default = (0, util_1.createRule)({
|
|
73
134
|
};
|
74
135
|
},
|
75
136
|
});
|
137
|
+
function wrapUpLeftFixer(node) {
|
138
|
+
return (fixer) => [
|
139
|
+
fixer.insertTextBefore(node.left, '('),
|
140
|
+
fixer.insertTextAfter(node.left, ')'),
|
141
|
+
];
|
142
|
+
}
|
76
143
|
//# sourceMappingURL=no-confusing-non-null-assertion.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"no-confusing-non-null-assertion.js","sourceRoot":"","sources":["../../src/rules/no-confusing-non-null-assertion.ts"],"names":[],"mappings":";;AACA,oDAA2E;
|
1
|
+
{"version":3,"file":"no-confusing-non-null-assertion.js","sourceRoot":"","sources":["../../src/rules/no-confusing-non-null-assertion.ts"],"names":[],"mappings":";;AACA,oDAA2E;AAM3E,kCAAqC;AAWrC,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC;IACjC,GAAG;IACH,IAAI;IACJ,KAAK;IACL,IAAI;IACJ,YAAY;CACJ,CAAC,CAAC;AAIZ,SAAS,mBAAmB,CAAC,QAAgB;IAC3C,OAAO,kBAAkB,CAAC,GAAG,CAAC,QAA6B,CAAC,CAAC;AAC/D,CAAC;AAED,kBAAe,IAAA,iBAAU,EAAgB;IACvC,IAAI,EAAE,iCAAiC;IACvC,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EACT,gEAAgE;YAClE,WAAW,EAAE,WAAW;SACzB;QACD,cAAc,EAAE,IAAI;QACpB,QAAQ,EAAE;YACR,cAAc,EACZ,sHAAsH;YACxH,eAAe,EACb,iHAAiH;YACnH,iBAAiB,EACf,2JAA2J;YAE7J,kBAAkB,EAChB,6DAA6D;YAC/D,eAAe,EACb,yEAAyE;YAE3E,iBAAiB,EACf,wGAAwG;YAE1G,UAAU,EACR,yFAAyF;SAC5F;QACD,MAAM,EAAE,EAAE;KACX;IACD,cAAc,EAAE,EAAE;IAClB,MAAM,CAAC,OAAO;QACZ,SAAS,8BAA8B,CACrC,QAA2B;YAE3B,QAAQ,QAAQ,EAAE,CAAC;gBACjB,KAAK,GAAG;oBACN,OAAO;wBACL,SAAS,EAAE,iBAAiB;qBAC7B,CAAC;gBACJ,KAAK,IAAI,CAAC;gBACV,KAAK,KAAK;oBACR,OAAO;wBACL,SAAS,EAAE,gBAAgB;qBAC5B,CAAC;gBACJ,KAAK,IAAI,CAAC;gBAC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ACJ,CAAC"}
|
@@ -50,6 +50,37 @@ exports.default = (0, util_1.createRule)({
|
|
50
50
|
}
|
51
51
|
const services = (0, util_1.getParserServices)(context);
|
52
52
|
const checker = services.program.getTypeChecker();
|
53
|
+
// Deprecated jsdoc tags can be added on some symbol alias, e.g.
|
54
|
+
//
|
55
|
+
// export { /** @deprecated */ foo }
|
56
|
+
//
|
57
|
+
// When we import foo, its symbol is an alias of the exported foo (the one
|
58
|
+
// with the deprecated tag), which is itself an alias of the original foo.
|
59
|
+
// Therefore, we carefully go through the chain of aliases and check each
|
60
|
+
// immediate alias for deprecated tags
|
61
|
+
function searchForDeprecationInAliasesChain(symbol, checkDeprecationsOfAliasedSymbol) {
|
62
|
+
if (!symbol || !tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) {
|
63
|
+
return checkDeprecationsOfAliasedSymbol
|
64
|
+
? getJsDocDeprecation(symbol)
|
65
|
+
: undefined;
|
66
|
+
}
|
67
|
+
const targetSymbol = checker.getAliasedSymbol(symbol);
|
68
|
+
while (tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) {
|
69
|
+
const reason = getJsDocDeprecation(symbol);
|
70
|
+
if (reason !== undefined) {
|
71
|
+
return reason;
|
72
|
+
}
|
73
|
+
const immediateAliasedSymbol = symbol.getDeclarations() && checker.getImmediateAliasedSymbol(symbol);
|
74
|
+
if (!immediateAliasedSymbol) {
|
75
|
+
break;
|
76
|
+
}
|
77
|
+
symbol = immediateAliasedSymbol;
|
78
|
+
if (checkDeprecationsOfAliasedSymbol && symbol === targetSymbol) {
|
79
|
+
return getJsDocDeprecation(symbol);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
return undefined;
|
83
|
+
}
|
53
84
|
function isDeclaration(node) {
|
54
85
|
const { parent } = node;
|
55
86
|
switch (parent.type) {
|
@@ -149,48 +180,63 @@ exports.default = (0, util_1.createRule)({
|
|
149
180
|
function getCallLikeDeprecation(node) {
|
150
181
|
const tsNode = services.esTreeNodeToTSNodeMap.get(node.parent);
|
151
182
|
// If the node is a direct function call, we look for its signature.
|
152
|
-
const signature = checker.getResolvedSignature(tsNode);
|
183
|
+
const signature = (0, util_1.nullThrows)(checker.getResolvedSignature(tsNode), 'Expected call like node to have signature');
|
153
184
|
const symbol = services.getSymbolAtLocation(node);
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
}
|
174
|
-
// Or it could be a ClassDeclaration or a variable set to a ClassExpression.
|
175
|
-
const symbolAtLocation = symbol && checker.getTypeOfSymbolAtLocation(symbol, tsNode).getSymbol();
|
176
|
-
return symbolAtLocation &&
|
177
|
-
tsutils.isSymbolFlagSet(symbolAtLocation, ts.SymbolFlags.Class)
|
178
|
-
? getJsDocDeprecation(symbolAtLocation)
|
179
|
-
: undefined;
|
180
|
-
}
|
181
|
-
function getSymbol(node) {
|
182
|
-
if (node.parent.type === utils_1.AST_NODE_TYPES.Property) {
|
183
|
-
return services
|
184
|
-
.getTypeAtLocation(node.parent.parent)
|
185
|
-
.getProperty(node.name);
|
185
|
+
const aliasedSymbol = symbol !== undefined &&
|
186
|
+
tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)
|
187
|
+
? checker.getAliasedSymbol(symbol)
|
188
|
+
: symbol;
|
189
|
+
const symbolDeclarationKind = aliasedSymbol?.declarations?.[0].kind;
|
190
|
+
// Properties with function-like types have "deprecated" jsdoc
|
191
|
+
// on their symbols, not on their signatures:
|
192
|
+
//
|
193
|
+
// interface Props {
|
194
|
+
// /** @deprecated */
|
195
|
+
// property: () => 'foo'
|
196
|
+
// ^symbol^ ^signature^
|
197
|
+
// }
|
198
|
+
if (symbolDeclarationKind !== ts.SyntaxKind.MethodDeclaration &&
|
199
|
+
symbolDeclarationKind !== ts.SyntaxKind.FunctionDeclaration &&
|
200
|
+
symbolDeclarationKind !== ts.SyntaxKind.MethodSignature) {
|
201
|
+
return (searchForDeprecationInAliasesChain(symbol, true) ??
|
202
|
+
getJsDocDeprecation(signature) ??
|
203
|
+
getJsDocDeprecation(aliasedSymbol));
|
186
204
|
}
|
187
|
-
return
|
205
|
+
return (searchForDeprecationInAliasesChain(symbol,
|
206
|
+
// Here we're working with a function declaration or method.
|
207
|
+
// Both can have 1 or more overloads, each overload creates one
|
208
|
+
// ts.Declaration which is placed in symbol.declarations.
|
209
|
+
//
|
210
|
+
// Imagine the following code:
|
211
|
+
//
|
212
|
+
// function foo(): void
|
213
|
+
// /** @deprecated Some Reason */
|
214
|
+
// function foo(arg: string): void
|
215
|
+
// function foo(arg?: string): void {}
|
216
|
+
//
|
217
|
+
// foo() // <- foo is our symbol
|
218
|
+
//
|
219
|
+
// If we call getJsDocDeprecation(checker.getAliasedSymbol(symbol)),
|
220
|
+
// we get 'Some Reason', but after all, we are calling foo with
|
221
|
+
// a signature that is not deprecated!
|
222
|
+
// It works this way because symbol.getJsDocTags returns tags from
|
223
|
+
// all symbol declarations combined into one array. And AFAIK there is
|
224
|
+
// no publicly exported TS function that can tell us if a particular
|
225
|
+
// declaration is deprecated or not.
|
226
|
+
//
|
227
|
+
// So, in case of function and method declarations, we don't check original
|
228
|
+
// aliased symbol, but rely on the getJsDocDeprecation(signature) call below.
|
229
|
+
false) ?? getJsDocDeprecation(signature));
|
188
230
|
}
|
189
231
|
function getDeprecationReason(node) {
|
190
232
|
const callLikeNode = getCallLikeNode(node);
|
191
|
-
|
192
|
-
|
193
|
-
|
233
|
+
if (callLikeNode) {
|
234
|
+
return getCallLikeDeprecation(callLikeNode);
|
235
|
+
}
|
236
|
+
if (node.parent.type === utils_1.AST_NODE_TYPES.Property) {
|
237
|
+
return getJsDocDeprecation(services.getTypeAtLocation(node.parent.parent).getProperty(node.name));
|
238
|
+
}
|
239
|
+
return searchForDeprecationInAliasesChain(services.getSymbolAtLocation(node), true);
|
194
240
|
}
|
195
241
|
function checkIdentifier(node) {
|
196
242
|
if (isDeclaration(node) || isInsideExportOrImport(node)) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"no-deprecated.js","sourceRoot":"","sources":["../../src/rules/no-deprecated.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,oDAA0D;AAC1D,sDAAwC;AACxC,+CAAiC;AAEjC,
|
1
|
+
{"version":3,"file":"no-deprecated.js","sourceRoot":"","sources":["../../src/rules/no-deprecated.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,oDAA0D;AAC1D,sDAAwC;AACxC,+CAAiC;AAEjC,kCAAoE;AAIpE,kBAAe,IAAA,iBAAU,EAAC;IACxB,IAAI,EAAE,eAAe;IACrB,IAAI,EAAE;QACJ,IAAI,EAAE;YACJ,WAAW,EAAE,6CAA6C;YAC1D,WAAW,EAAE,QAAQ;YACrB,oBAAoB,EAAE,IAAI;SAC3B;QACD,QAAQ,EAAE;YACR,UAAU,EAAE,6BAA6B;YACzC,oBAAoB,EAAE,wCAAwC;SAC/D;QACD,MAAM,EAAE,EAAE;QACV,IAAI,EAAE,SAAS;KAChB;IACD,cAAc,EAAE,EAAE;IAClB,MAAM,CAAC,OAAO;QACZ,MAAM,EAAE,gBAAgB,EAAE,GAAG,OAAO,CAAC,aAAa,CAAC;QACnD,IAAI,gBAAgB,KAAK,MAAM,IAAI,gBAAgB,KAAK,WAAW,EAAE,CAAC;YACpE,MAAM,IAAI,KAAK,CACb,0CAA0C,gBAAgB,IAAI,CAC/D,CAAC;QACJ,CAAC;QAED,MAAM,QAAQ,GAAG,IAAA,wBAAiB,EAAC,OAAO,CAAC,CAAC;QAC5C,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;QAElD,gEAAgE;QAChE,EAAE;QACF,oCAAoC;QACpC,EAAE;QACF,0EAA0E;QAC1E,0EAA0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;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
@@ -35,13 +35,13 @@ exports.default = (0, util_1.createRule)({
|
|
35
35
|
requiresTypeChecking: true,
|
36
36
|
},
|
37
37
|
messages: {
|
38
|
-
unsafeCall: 'Unsafe call of
|
38
|
+
unsafeCall: 'Unsafe call of a(n) {{type}} typed value.',
|
39
39
|
unsafeCallThis: [
|
40
|
-
'Unsafe call of
|
40
|
+
'Unsafe call of a(n) {{type}} typed value. `this` is typed as {{type}}.',
|
41
41
|
'You can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function.',
|
42
42
|
].join('\n'),
|
43
|
-
unsafeNew: 'Unsafe construction of
|
44
|
-
unsafeTemplateTag: 'Unsafe
|
43
|
+
unsafeNew: 'Unsafe construction of a(n) {{type}} typed value.',
|
44
|
+
unsafeTemplateTag: 'Unsafe use of a(n) {{type}} typed template tag.',
|
45
45
|
},
|
46
46
|
schema: [],
|
47
47
|
},
|
@@ -69,6 +69,41 @@ exports.default = (0, util_1.createRule)({
|
|
69
69
|
type: isErrorType ? '`error` type' : '`any`',
|
70
70
|
},
|
71
71
|
});
|
72
|
+
return;
|
73
|
+
}
|
74
|
+
if ((0, util_1.isBuiltinSymbolLike)(services.program, type, 'Function')) {
|
75
|
+
// this also matches subtypes of `Function`, like `interface Foo extends Function {}`.
|
76
|
+
//
|
77
|
+
// For weird TS reasons that I don't understand, these are
|
78
|
+
//
|
79
|
+
// safe to construct if:
|
80
|
+
// - they have at least one call signature _that is not void-returning_,
|
81
|
+
// - OR they have at least one construct signature.
|
82
|
+
//
|
83
|
+
// safe to call (including as template) if:
|
84
|
+
// - they have at least one call signature
|
85
|
+
// - OR they have at least one construct signature.
|
86
|
+
const constructSignatures = type.getConstructSignatures();
|
87
|
+
if (constructSignatures.length > 0) {
|
88
|
+
return;
|
89
|
+
}
|
90
|
+
const callSignatures = type.getCallSignatures();
|
91
|
+
if (messageId === 'unsafeNew') {
|
92
|
+
if (callSignatures.some(signature => !tsutils.isIntrinsicVoidType(signature.getReturnType()))) {
|
93
|
+
return;
|
94
|
+
}
|
95
|
+
}
|
96
|
+
else if (callSignatures.length > 0) {
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
context.report({
|
100
|
+
node: reportingNode,
|
101
|
+
messageId,
|
102
|
+
data: {
|
103
|
+
type: '`Function`',
|
104
|
+
},
|
105
|
+
});
|
106
|
+
return;
|
72
107
|
}
|
73
108
|
}
|
74
109
|
return {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"no-unsafe-call.js","sourceRoot":"","sources":["../../src/rules/no-unsafe-call.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,sDAAwC;AAExC,
|
1
|
+
{"version":3,"file":"no-unsafe-call.js","sourceRoot":"","sources":["../../src/rules/no-unsafe-call.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,sDAAwC;AAExC,kCAOiB;AAQjB,kBAAe,IAAA,iBAAU,EAAiB;IACxC,IAAI,EAAE,gBAAgB;IACtB,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,0CAA0C;YACvD,WAAW,EAAE,aAAa;YAC1B,oBAAoB,EAAE,IAAI;SAC3B;QACD,QAAQ,EAAE;YACR,UAAU,EAAE,2CAA2C;YACvD,cAAc,EAAE;gBACd,wEAAwE;gBACxE,2HAA2H;aAC5H,CAAC,IAAI,CAAC,IAAI,CAAC;YACZ,SAAS,EAAE,mDAAmD;YAC9D,iBAAiB,EAAE,iDAAiD;SACrE;QACD,MAAM,EAAE,EAAE;KACX;IACD,cAAc,EAAE,EAAE;IAClB,MAAM,CAAC,OAAO;QACZ,MAAM,QAAQ,GAAG,IAAA,wBAAiB,EAAC,OAAO,CAAC,CAAC;QAC5C,MAAM,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC;QAC9D,MAAM,gBAAgB,GAAG,OA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,GAAG,IAAI,CAAC,iBAAiB,EAAE,CAAC;gBAChD,IAAI,SAAS,KAAK,WAAW,EAAE,CAAC;oBAC9B,IACE,cAAc,CAAC,IAAI,CACjB,SAAS,CAAC,EAAE,CACV,CAAC,OAAO,CAAC,mBAAmB,CAAC,SAAS,CAAC,aAAa,EAAE,CAAC,CAC1D,EACD,CAAC;wBACD,OAAO;oBACT,CAAC;gBACH,CAAC;qBAAM,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACrC,OAAO;gBACT,CAAC;gBAED,OAAO,CAAC,MAAM,CAAC;oBACb,IAAI,EAAE,aAAa;oBACnB,SAAS;oBACT,IAAI,EAAE;wBACJ,IAAI,EAAE,YAAY;qBACnB;iBACF,CAAC,CAAC;gBACH,OAAO;YACT,CAAC;QACH,CAAC;QAED,OAAO;YACL,2BAA2B,CACzB,IAAuC;gBAEvC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC;YACtC,CAAC;YACD,aAAa,CAAC,IAAI;gBAChB,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC;YAC5C,CAAC;YACD,kCAAkC,CAAC,IAAmB;gBACpD,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,mBAAmB,CAAC,CAAC;YAC7C,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/dist/util/index.js
CHANGED
@@ -34,7 +34,6 @@ __exportStar(require("./misc"), exports);
|
|
34
34
|
__exportStar(require("./needsPrecedingSemiColon"), exports);
|
35
35
|
__exportStar(require("./objectIterators"), exports);
|
36
36
|
__exportStar(require("./scopeUtils"), exports);
|
37
|
-
__exportStar(require("./types"), exports);
|
38
37
|
__exportStar(require("./isAssignee"), exports);
|
39
38
|
__exportStar(require("./getFixOrSuggest"), exports);
|
40
39
|
__exportStar(require("./isArrayMethodCallWithPredicate"), exports);
|
package/dist/util/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,oDAAuD;AAEvD,6CAA2B;AAC3B,2DAAyC;AACzC,+CAA6B;AAC7B,uDAAqC;AACrC,0DAAwC;AACxC,yDAAuC;AACvC,oDAAkC;AAClC,2DAAyC;AACzC,sDAAoC;AACpC,qDAAmC;AACnC,gDAA8B;AAC9B,kDAAgC;AAChC,iEAA+C;AAC/C,0DAAwC;AACxC,yCAAuB;AACvB,4DAA0C;AAC1C,oDAAkC;AAClC,+CAA6B;
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,oDAAuD;AAEvD,6CAA2B;AAC3B,2DAAyC;AACzC,+CAA6B;AAC7B,uDAAqC;AACrC,0DAAwC;AACxC,yDAAuC;AACvC,oDAAkC;AAClC,2DAAyC;AACzC,sDAAoC;AACpC,qDAAmC;AACnC,gDAA8B;AAC9B,kDAAgC;AAChC,iEAA+C;AAC/C,0DAAwC;AACxC,yCAAuB;AACvB,4DAA0C;AAC1C,oDAAkC;AAClC,+CAA6B;AAE7B,+CAA6B;AAC7B,oDAAkC;AAClC,mEAAiD;AAEjD,sEAAsE;AACtE,gEAA8C;AAC9C,MAAM,EACJ,YAAY,EACZ,SAAS,EACT,gBAAgB,EAChB,iBAAiB,EACjB,UAAU,EACV,iBAAiB,GAClB,GAAG,mBAAW,CAAC;AAMd,oCAAY;AACZ,8BAAS;AACT,4CAAgB;AAChB,8CAAiB;AACjB,gCAAU;AAGV,8CAAiB"}
|
@@ -9,14 +9,27 @@ import TabItem from '@theme/TabItem';
|
|
9
9
|
>
|
10
10
|
> See **https://typescript-eslint.io/rules/no-confusing-non-null-assertion** for documentation.
|
11
11
|
|
12
|
-
Using a non-null assertion (`!`) next to an
|
12
|
+
Using a non-null assertion (`!`) next to an assignment or equality check (`=` or `==` or `===`) creates code that is confusing as it looks similar to an inequality check (`!=` `!==`).
|
13
13
|
|
14
14
|
```typescript
|
15
|
-
a! == b; // a non-null
|
15
|
+
a! == b; // a non-null assertion(`!`) and an equals test(`==`)
|
16
16
|
a !== b; // not equals test(`!==`)
|
17
|
-
a! === b; // a non-null
|
17
|
+
a! === b; // a non-null assertion(`!`) and a triple equals test(`===`)
|
18
18
|
```
|
19
19
|
|
20
|
+
Using a non-null assertion (`!`) next to an in test (`in`) or an instanceof test (`instanceof`) creates code that is confusing since it may look like the operator is negated, but it is actually not.
|
21
|
+
|
22
|
+
{/* prettier-ignore */}
|
23
|
+
```typescript
|
24
|
+
a! in b; // a non-null assertion(`!`) and an in test(`in`)
|
25
|
+
a !in b; // also a non-null assertion(`!`) and an in test(`in`)
|
26
|
+
!(a in b); // a negated in test
|
27
|
+
|
28
|
+
a! instanceof b; // a non-null assertion(`!`) and an instanceof test(`instanceof`)
|
29
|
+
a !instanceof b; // also a non-null assertion(`!`) and an instanceof test(`instanceof`)
|
30
|
+
!(a instanceof b); // a negated instanceof test
|
31
|
+
````
|
32
|
+
|
20
33
|
This rule flags confusing `!` assertions and suggests either removing them or wrapping the asserted expression in `()` parenthesis.
|
21
34
|
|
22
35
|
## Examples
|
@@ -12,14 +12,13 @@ import TabItem from '@theme/TabItem';
|
|
12
12
|
A "floating" Promise is one that is created without any code set up to handle any errors it might throw.
|
13
13
|
Floating Promises can cause several issues, such as improperly sequenced operations, ignored Promise rejections, and more.
|
14
14
|
|
15
|
-
This rule
|
16
|
-
Valid ways of handling a Promise-valued statement include:
|
15
|
+
This rule will report Promise-valued statements that are not treated in one of the following ways:
|
17
16
|
|
18
|
-
- `await`ing it
|
19
|
-
- `return`ing it
|
20
|
-
- `void`ing it
|
21
17
|
- Calling its `.then()` with two arguments
|
22
18
|
- Calling its `.catch()` with one argument
|
19
|
+
- `await`ing it
|
20
|
+
- `return`ing it
|
21
|
+
- [`void`ing it](#ignorevoid)
|
23
22
|
|
24
23
|
This rule also reports when an Array containing Promises is created and not properly handled. The main way to resolve this is by using one of the Promise concurrency methods to create a single Promise, then handling that according to the procedure above. These methods include:
|
25
24
|
|
@@ -29,8 +28,10 @@ This rule also reports when an Array containing Promises is created and not prop
|
|
29
28
|
- `Promise.race()`
|
30
29
|
|
31
30
|
:::tip
|
32
|
-
`no-floating-promises` only detects unhandled Promise _statements_.
|
31
|
+
`no-floating-promises` only detects apparently unhandled Promise _statements_.
|
33
32
|
See [`no-misused-promises`](./no-misused-promises.mdx) for detecting code that provides Promises to _logical_ locations such as if statements.
|
33
|
+
|
34
|
+
See [_Using promises (error handling) on MDN_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#error_handling) for a detailed writeup on Promise error-handling.
|
34
35
|
:::
|
35
36
|
|
36
37
|
## Examples
|
@@ -134,6 +135,12 @@ await createMyThenable();
|
|
134
135
|
This option, which is `true` by default, allows you to stop the rule reporting promises consumed with the [`void` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void).
|
135
136
|
This can be a good way to explicitly mark a promise as intentionally not awaited.
|
136
137
|
|
138
|
+
:::warning
|
139
|
+
Voiding a Promise doesn't handle it or change the runtime behavior.
|
140
|
+
The outcome is just ignored, like disabling the rule with an [ESLint disable comment](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1).
|
141
|
+
Such Promise rejections will still be unhandled.
|
142
|
+
:::
|
143
|
+
|
137
144
|
Examples of **correct** code for this rule with `{ ignoreVoid: true }`:
|
138
145
|
|
139
146
|
```ts option='{ "ignoreVoid": true }' showPlaygroundButton
|
@@ -19,7 +19,7 @@ At best unnecessary type parameters make code harder to read.
|
|
19
19
|
At worst they can be used to disguise unsafe type assertions.
|
20
20
|
|
21
21
|
:::warning
|
22
|
-
This rule was recently added, and has a surprising amount of hidden complexity compared to most of our rules. If you encounter unexpected behavior with it, please check closely the [Limitations](#limitations)
|
22
|
+
This rule was recently added, and has a surprising amount of hidden complexity compared to most of our rules. If you encounter unexpected behavior with it, please check closely the [Limitations](#limitations) and [FAQ](#faq) sections below and our [issue tracker](https://github.com/typescript-eslint/typescript-eslint/issues?q=is%3Aissue+no-unnecessary-type-parameters).
|
23
23
|
If you don't see your case covered, please [reach out to us](https://typescript-eslint.io/contributing/issues)!
|
24
24
|
:::
|
25
25
|
|
@@ -87,6 +87,127 @@ This is because the type parameter `T` relates multiple methods in the `T[]` tog
|
|
87
87
|
Therefore, this rule won't report on type parameters used as a type argument.
|
88
88
|
That includes type arguments given to global types such as `Array` (including the `T[]` shorthand and in tuples), `Map`, and `Set`.
|
89
89
|
|
90
|
+
## FAQ
|
91
|
+
|
92
|
+
### The return type is only used as an input, so why isn't the rule reporting?
|
93
|
+
|
94
|
+
One common reason that this might be the case is when the return type is not specified explicitly.
|
95
|
+
The rule uses uses type information to count implicit usages of the type parameter in the function signature, including in the inferred return type.
|
96
|
+
For example, the following function...
|
97
|
+
|
98
|
+
```ts
|
99
|
+
function identity<T>(arg: T) {
|
100
|
+
return arg;
|
101
|
+
}
|
102
|
+
```
|
103
|
+
|
104
|
+
...implicitly has a return type of `T`. Therefore, the type parameter `T` is used twice, and the rule will not report this function.
|
105
|
+
|
106
|
+
For other reasons the rule might not be reporting, be sure to check the [Limitations section](#limitations) and other FAQs.
|
107
|
+
|
108
|
+
### I'm using the type parameter inside the function, so why is the rule reporting?
|
109
|
+
|
110
|
+
You might be surprised to that the rule reports on a function like this:
|
111
|
+
|
112
|
+
```ts
|
113
|
+
function log<T extends string>(string1: T): void {
|
114
|
+
const string2: T = string1;
|
115
|
+
console.log(string2);
|
116
|
+
}
|
117
|
+
```
|
118
|
+
|
119
|
+
After all, the type parameter `T` relates the input `string1` and the local variable `string2`, right?
|
120
|
+
However, this usage is unnecessary, since we can achieve the same results by replacing all usages of the type parameter with its constraint.
|
121
|
+
That is to say, the function can always be rewritten as:
|
122
|
+
|
123
|
+
```ts
|
124
|
+
function log(string1: string): void {
|
125
|
+
const string2: string = string1;
|
126
|
+
console.log(string2);
|
127
|
+
}
|
128
|
+
```
|
129
|
+
|
130
|
+
Therefore, this rule only counts usages of a type parameter in the _signature_ of a function, method, or class, but not in the implementation. See also [#9735](https://github.com/typescript-eslint/typescript-eslint/issues/9735)
|
131
|
+
|
132
|
+
### Why am I getting TypeScript errors saying "Object literal may only specify known properties" after removing an unnecessary type parameter?
|
133
|
+
|
134
|
+
Suppose you have a situation like the following, which will trigger the rule to report.
|
135
|
+
|
136
|
+
```ts
|
137
|
+
interface SomeProperties {
|
138
|
+
foo: string;
|
139
|
+
}
|
140
|
+
|
141
|
+
// T is only used once, so the rule will report.
|
142
|
+
function serialize<T extends SomeProperties>(x: T): string {
|
143
|
+
return JSON.stringify(x);
|
144
|
+
}
|
145
|
+
|
146
|
+
serialize({ foo: 'bar', anotherProperty: 'baz' });
|
147
|
+
```
|
148
|
+
|
149
|
+
If we remove the unnecessary type parameter, we'll get an error:
|
150
|
+
|
151
|
+
```ts
|
152
|
+
function serialize(x: SomeProperties): string {
|
153
|
+
return JSON.stringify(x);
|
154
|
+
}
|
155
|
+
|
156
|
+
// TS Error: Object literal may only specify known properties, and 'anotherProperty' does not exist in type 'SomeProperties'.
|
157
|
+
serialize({ foo: 'bar', anotherProperty: 'baz' });
|
158
|
+
```
|
159
|
+
|
160
|
+
This is because TypeScript figures it's _usually_ an error to explicitly provide excess properties in a location that expects a specific type.
|
161
|
+
See [the TypeScript handbook's section on excess property checks](https://www.typescriptlang.org/docs/handbook/2/objects.html#excess-property-checks) for further discussion.
|
162
|
+
|
163
|
+
To resolve this, you have two approaches to choose from.
|
164
|
+
|
165
|
+
1. If it doesn't make sense to accept excess properties in your function, you'll want to fix the errors at the call sites. Usually, you can simply remove any excess properties where the function is called.
|
166
|
+
2. Otherwise, if you do want your function to accept excess properties, you can modify the parameter type in order to allow excess properties explicitly by using an [index signature](https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures):
|
167
|
+
|
168
|
+
```ts
|
169
|
+
interface SomeProperties {
|
170
|
+
foo: string;
|
171
|
+
|
172
|
+
// This allows any other properties.
|
173
|
+
// You may wish to make these types more specific according to your use case.
|
174
|
+
[key: PropertKey]: unknown;
|
175
|
+
}
|
176
|
+
|
177
|
+
function serialize(x: SomeProperties): string {
|
178
|
+
return JSON.stringify(x);
|
179
|
+
}
|
180
|
+
|
181
|
+
// No error!
|
182
|
+
serialize({ foo: 'bar', anotherProperty: 'baz' });
|
183
|
+
```
|
184
|
+
|
185
|
+
Which solution is appropriate is a case-by-case decision, depending on the intended use case of your function.
|
186
|
+
|
187
|
+
### I have a complex scenario that is reported by the rule, but I can't see how to remove the type parameter. What should I do?
|
188
|
+
|
189
|
+
Sometimes, you may be able to rewrite the code by reaching for some niche TypeScript features, such as [the `NoInfer<T>` utility type](https://www.typescriptlang.org/docs/handbook/utility-types.html#noinfertype) (see [#9751](https://github.com/typescript-eslint/typescript-eslint/issues/9751)).
|
190
|
+
|
191
|
+
But, quite possibly, you've hit an edge case where the type is being used in a subtle way that the rule doesn't account for.
|
192
|
+
For example, the following arcane code is a way of testing whether two types are equal, and will be reported by the rule (see [#9709](https://github.com/typescript-eslint/typescript-eslint/issues/9709)):
|
193
|
+
|
194
|
+
{/* prettier-ignore */}
|
195
|
+
```ts
|
196
|
+
type Compute<A> = A extends Function ? A : { [K in keyof A]: Compute<A[K]> };
|
197
|
+
type Equal<X, Y> =
|
198
|
+
(<T1>() => T1 extends Compute<X> ? 1 : 2) extends
|
199
|
+
(<T2>() => T2 extends Compute<Y> ? 1 : 2)
|
200
|
+
? true
|
201
|
+
: false;
|
202
|
+
```
|
203
|
+
|
204
|
+
In this case, the function types created within the `Equal` type are never expected to be assigned to; they're just created for the purpose of type system manipulations.
|
205
|
+
This usage is not what the rule is intended to analyze.
|
206
|
+
|
207
|
+
Use eslint-disable comments as appropriate to suppress the rule in these kinds of cases.
|
208
|
+
|
209
|
+
{/* TODO - include an FAQ entry regarding instantiation expressions once the conversation in https://github.com/typescript-eslint/typescript-eslint/pull/9536#discussion_r1705850744 is done */}
|
210
|
+
|
90
211
|
## When Not To Use It
|
91
212
|
|
92
213
|
This rule will report on functions that use type parameters solely to test types, for example:
|
@@ -59,6 +59,34 @@ String.raw`foo`;
|
|
59
59
|
</TabItem>
|
60
60
|
</Tabs>
|
61
61
|
|
62
|
+
## The Unsafe `Function` Type
|
63
|
+
|
64
|
+
The `Function` type is behaves almost identically to `any` when called, so this rule also disallows calling values of type `Function`.
|
65
|
+
|
66
|
+
<Tabs>
|
67
|
+
<TabItem value="❌ Incorrect">
|
68
|
+
|
69
|
+
```ts
|
70
|
+
const f: Function = () => {};
|
71
|
+
f();
|
72
|
+
```
|
73
|
+
|
74
|
+
</TabItem>
|
75
|
+
</Tabs>
|
76
|
+
|
77
|
+
Note that whereas [no-unsafe-function-type](./no-unsafe-function-type.mdx) helps prevent the _creation_ of `Function` types, this rule helps prevent the unsafe _use_ of `Function` types, which may creep into your codebase without explicitly referencing the `Function` type at all.
|
78
|
+
See, for example, the following code:
|
79
|
+
|
80
|
+
```ts
|
81
|
+
function unsafe(maybeFunction: unknown): string {
|
82
|
+
if (typeof maybeFunction === 'function') {
|
83
|
+
// TypeScript allows this, but it's completely unsound.
|
84
|
+
return maybeFunction('call', 'with', 'any', 'args');
|
85
|
+
}
|
86
|
+
// etc
|
87
|
+
}
|
88
|
+
```
|
89
|
+
|
62
90
|
## When Not To Use It
|
63
91
|
|
64
92
|
If your codebase has many existing `any`s or areas of unsafe code, it may be difficult to enable this rule.
|
@@ -60,4 +60,5 @@ You might consider using [ESLint disable comments](https://eslint.org/docs/lates
|
|
60
60
|
|
61
61
|
- [`no-empty-object-type`](./no-empty-object-type.mdx)
|
62
62
|
- [`no-restricted-types`](./no-restricted-types.mdx)
|
63
|
+
- [`no-unsafe-call`](./no-unsafe-call.mdx)
|
63
64
|
- [`no-wrapper-object-types`](./no-wrapper-object-types.mdx)
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
3
|
-
"version": "8.6.1-alpha.
|
3
|
+
"version": "8.6.1-alpha.8",
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
5
5
|
"files": [
|
6
6
|
"dist",
|
@@ -60,10 +60,10 @@
|
|
60
60
|
},
|
61
61
|
"dependencies": {
|
62
62
|
"@eslint-community/regexpp": "^4.10.0",
|
63
|
-
"@typescript-eslint/scope-manager": "8.6.1-alpha.
|
64
|
-
"@typescript-eslint/type-utils": "8.6.1-alpha.
|
65
|
-
"@typescript-eslint/utils": "8.6.1-alpha.
|
66
|
-
"@typescript-eslint/visitor-keys": "8.6.1-alpha.
|
63
|
+
"@typescript-eslint/scope-manager": "8.6.1-alpha.8",
|
64
|
+
"@typescript-eslint/type-utils": "8.6.1-alpha.8",
|
65
|
+
"@typescript-eslint/utils": "8.6.1-alpha.8",
|
66
|
+
"@typescript-eslint/visitor-keys": "8.6.1-alpha.8",
|
67
67
|
"graphemer": "^1.4.0",
|
68
68
|
"ignore": "^5.3.1",
|
69
69
|
"natural-compare": "^1.4.0",
|
@@ -74,8 +74,8 @@
|
|
74
74
|
"@types/marked": "^5.0.2",
|
75
75
|
"@types/mdast": "^4.0.3",
|
76
76
|
"@types/natural-compare": "*",
|
77
|
-
"@typescript-eslint/rule-schema-to-typescript-types": "8.6.1-alpha.
|
78
|
-
"@typescript-eslint/rule-tester": "8.6.1-alpha.
|
77
|
+
"@typescript-eslint/rule-schema-to-typescript-types": "8.6.1-alpha.8",
|
78
|
+
"@typescript-eslint/rule-tester": "8.6.1-alpha.8",
|
79
79
|
"ajv": "^6.12.6",
|
80
80
|
"cross-env": "^7.0.3",
|
81
81
|
"cross-fetch": "*",
|