UNPKG

3.74 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const utils_1 = require("@typescript-eslint/utils");
23const semver = __importStar(require("semver"));
24const ts = __importStar(require("typescript"));
25const util = __importStar(require("../util"));
26const is3dot5 = semver.satisfies(ts.version, `>= 3.5.0 || >= 3.5.1-rc || >= 3.5.0-beta`, {
27 includePrerelease: true,
28});
29const is3dot9 = is3dot5 &&
30 semver.satisfies(ts.version, `>= 3.9.0 || >= 3.9.1-rc || >= 3.9.0-beta`, {
31 includePrerelease: true,
32 });
33exports.default = util.createRule({
34 name: 'no-unnecessary-type-constraint',
35 meta: {
36 docs: {
37 description: 'Disallows unnecessary constraints on generic types',
38 recommended: 'error',
39 suggestion: true,
40 },
41 fixable: 'code',
42 messages: {
43 unnecessaryConstraint: 'Constraining the generic type `{{name}}` to `{{constraint}}` does nothing and is unnecessary.',
44 },
45 schema: [],
46 type: 'suggestion',
47 },
48 defaultOptions: [],
49 create(context) {
50 if (!is3dot5) {
51 return {};
52 }
53 // In theory, we could use the type checker for more advanced constraint types...
54 // ...but in practice, these types are rare, and likely not worth requiring type info.
55 // https://github.com/typescript-eslint/typescript-eslint/pull/2516#discussion_r495731858
56 const unnecessaryConstraints = is3dot9
57 ? new Map([
58 [utils_1.AST_NODE_TYPES.TSAnyKeyword, 'any'],
59 [utils_1.AST_NODE_TYPES.TSUnknownKeyword, 'unknown'],
60 ])
61 : new Map([[utils_1.AST_NODE_TYPES.TSUnknownKeyword, 'unknown']]);
62 const inJsx = context.getFilename().toLowerCase().endsWith('tsx');
63 const checkNode = (node, inArrowFunction) => {
64 const constraint = unnecessaryConstraints.get(node.constraint.type);
65 if (constraint) {
66 context.report({
67 data: {
68 constraint,
69 name: node.name.name,
70 },
71 fix(fixer) {
72 return fixer.replaceTextRange([node.name.range[1], node.constraint.range[1]], inArrowFunction && inJsx ? ',' : '');
73 },
74 messageId: 'unnecessaryConstraint',
75 node,
76 });
77 }
78 };
79 return {
80 ':not(ArrowFunctionExpression) > TSTypeParameterDeclaration > TSTypeParameter[constraint]'(node) {
81 checkNode(node, false);
82 },
83 'ArrowFunctionExpression > TSTypeParameterDeclaration > TSTypeParameter[constraint]'(node) {
84 checkNode(node, true);
85 },
86 };
87 },
88});
89//# sourceMappingURL=no-unnecessary-type-constraint.js.map
\No newline at end of file