UNPKG

3.9 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: 'no-misused-new',
26 meta: {
27 type: 'problem',
28 docs: {
29 description: 'Enforce valid definition of `new` and `constructor`',
30 recommended: 'error',
31 },
32 schema: [],
33 messages: {
34 errorMessageInterface: 'Interfaces cannot be constructed, only classes.',
35 errorMessageClass: 'Class cannot have method named `new`.',
36 },
37 },
38 defaultOptions: [],
39 create(context) {
40 /**
41 * @param node type to be inspected.
42 * @returns name of simple type or null
43 */
44 function getTypeReferenceName(node) {
45 if (node) {
46 switch (node.type) {
47 case utils_1.AST_NODE_TYPES.TSTypeAnnotation:
48 return getTypeReferenceName(node.typeAnnotation);
49 case utils_1.AST_NODE_TYPES.TSTypeReference:
50 return getTypeReferenceName(node.typeName);
51 case utils_1.AST_NODE_TYPES.Identifier:
52 return node.name;
53 default:
54 break;
55 }
56 }
57 return null;
58 }
59 /**
60 * @param parent parent node.
61 * @param returnType type to be compared
62 */
63 function isMatchingParentType(parent, returnType) {
64 if (parent &&
65 'id' in parent &&
66 parent.id &&
67 parent.id.type === utils_1.AST_NODE_TYPES.Identifier) {
68 return getTypeReferenceName(returnType) === parent.id.name;
69 }
70 return false;
71 }
72 return {
73 'TSInterfaceBody > TSConstructSignatureDeclaration'(node) {
74 if (isMatchingParentType(node.parent.parent, node.returnType)) {
75 // constructor
76 context.report({
77 node,
78 messageId: 'errorMessageInterface',
79 });
80 }
81 },
82 "TSMethodSignature[key.name='constructor']"(node) {
83 context.report({
84 node,
85 messageId: 'errorMessageInterface',
86 });
87 },
88 "ClassBody > MethodDefinition[key.name='new']"(node) {
89 if (node.value.type === utils_1.AST_NODE_TYPES.TSEmptyBodyFunctionExpression) {
90 if (node.parent &&
91 isMatchingParentType(node.parent.parent, node.value.returnType)) {
92 context.report({
93 node,
94 messageId: 'errorMessageClass',
95 });
96 }
97 }
98 },
99 };
100 },
101});
102//# sourceMappingURL=no-misused-new.js.map
\No newline at end of file