1 | "use strict";
|
2 | var __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 | }));
|
9 | var __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 | });
|
14 | var __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 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | const utils_1 = require("@typescript-eslint/utils");
|
23 | const semver = __importStar(require("semver"));
|
24 | const ts = __importStar(require("typescript"));
|
25 | const util = __importStar(require("../util"));
|
26 | const is3dot5 = semver.satisfies(ts.version, `>= 3.5.0 || >= 3.5.1-rc || >= 3.5.0-beta`, {
|
27 | includePrerelease: true,
|
28 | });
|
29 | const is3dot9 = is3dot5 &&
|
30 | semver.satisfies(ts.version, `>= 3.9.0 || >= 3.9.1-rc || >= 3.9.0-beta`, {
|
31 | includePrerelease: true,
|
32 | });
|
33 | exports.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 |
|
54 |
|
55 |
|
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 |
|
\ | No newline at end of file |