UNPKG

6.62 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 util = __importStar(require("../util"));
24exports.default = util.createRule({
25 name: 'adjacent-overload-signatures',
26 meta: {
27 type: 'suggestion',
28 docs: {
29 description: 'Require that member overloads be consecutive',
30 recommended: 'error',
31 },
32 schema: [],
33 messages: {
34 adjacentSignature: 'All {{name}} signatures should be adjacent.',
35 },
36 },
37 defaultOptions: [],
38 create(context) {
39 const sourceCode = context.getSourceCode();
40 /**
41 * Gets the name and attribute of the member being processed.
42 * @param member the member being processed.
43 * @returns the name and attribute of the member or null if it's a member not relevant to the rule.
44 */
45 function getMemberMethod(member) {
46 var _a, _b;
47 if (!member) {
48 return null;
49 }
50 const isStatic = 'static' in member && !!member.static;
51 switch (member.type) {
52 case utils_1.AST_NODE_TYPES.ExportDefaultDeclaration:
53 case utils_1.AST_NODE_TYPES.ExportNamedDeclaration: {
54 // export statements (e.g. export { a };)
55 // have no declarations, so ignore them
56 if (!member.declaration) {
57 return null;
58 }
59 return getMemberMethod(member.declaration);
60 }
61 case utils_1.AST_NODE_TYPES.TSDeclareFunction:
62 case utils_1.AST_NODE_TYPES.FunctionDeclaration: {
63 const name = (_b = (_a = member.id) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : null;
64 if (name === null) {
65 return null;
66 }
67 return {
68 name,
69 static: isStatic,
70 callSignature: false,
71 type: util.MemberNameType.Normal,
72 };
73 }
74 case utils_1.AST_NODE_TYPES.TSMethodSignature:
75 return Object.assign(Object.assign({}, util.getNameFromMember(member, sourceCode)), { static: isStatic, callSignature: false });
76 case utils_1.AST_NODE_TYPES.TSCallSignatureDeclaration:
77 return {
78 name: 'call',
79 static: isStatic,
80 callSignature: true,
81 type: util.MemberNameType.Normal,
82 };
83 case utils_1.AST_NODE_TYPES.TSConstructSignatureDeclaration:
84 return {
85 name: 'new',
86 static: isStatic,
87 callSignature: false,
88 type: util.MemberNameType.Normal,
89 };
90 case utils_1.AST_NODE_TYPES.MethodDefinition:
91 return Object.assign(Object.assign({}, util.getNameFromMember(member, sourceCode)), { static: isStatic, callSignature: false });
92 }
93 return null;
94 }
95 function isSameMethod(method1, method2) {
96 return (!!method2 &&
97 method1.name === method2.name &&
98 method1.static === method2.static &&
99 method1.callSignature === method2.callSignature &&
100 method1.type === method2.type);
101 }
102 function getMembers(node) {
103 switch (node.type) {
104 case utils_1.AST_NODE_TYPES.ClassBody:
105 case utils_1.AST_NODE_TYPES.Program:
106 case utils_1.AST_NODE_TYPES.TSModuleBlock:
107 case utils_1.AST_NODE_TYPES.TSInterfaceBody:
108 return node.body;
109 case utils_1.AST_NODE_TYPES.TSTypeLiteral:
110 return node.members;
111 }
112 }
113 /**
114 * Check the body for overload methods.
115 * @param {ASTNode} node the body to be inspected.
116 */
117 function checkBodyForOverloadMethods(node) {
118 const members = getMembers(node);
119 if (members) {
120 let lastMethod = null;
121 const seenMethods = [];
122 members.forEach(member => {
123 const method = getMemberMethod(member);
124 if (method === null) {
125 lastMethod = null;
126 return;
127 }
128 const index = seenMethods.findIndex(seenMethod => isSameMethod(method, seenMethod));
129 if (index > -1 && !isSameMethod(method, lastMethod)) {
130 context.report({
131 node: member,
132 messageId: 'adjacentSignature',
133 data: {
134 name: `${method.static ? 'static ' : ''}${method.name}`,
135 },
136 });
137 }
138 else if (index === -1) {
139 seenMethods.push(method);
140 }
141 lastMethod = method;
142 });
143 }
144 }
145 return {
146 ClassBody: checkBodyForOverloadMethods,
147 Program: checkBodyForOverloadMethods,
148 TSModuleBlock: checkBodyForOverloadMethods,
149 TSTypeLiteral: checkBodyForOverloadMethods,
150 TSInterfaceBody: checkBodyForOverloadMethods,
151 };
152 },
153});
154//# sourceMappingURL=adjacent-overload-signatures.js.map
\No newline at end of file