UNPKG

5.77 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 getESLintCoreRule_1 = require("../util/getESLintCoreRule");
24const util = __importStar(require("../util"));
25const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('no-empty-function');
26const schema = util.deepMerge(
27// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- https://github.com/microsoft/TypeScript/issues/17002
28Array.isArray(baseRule.meta.schema)
29 ? baseRule.meta.schema[0]
30 : baseRule.meta.schema, {
31 properties: {
32 allow: {
33 items: {
34 enum: [
35 'functions',
36 'arrowFunctions',
37 'generatorFunctions',
38 'methods',
39 'generatorMethods',
40 'getters',
41 'setters',
42 'constructors',
43 'private-constructors',
44 'protected-constructors',
45 'asyncFunctions',
46 'asyncMethods',
47 'decoratedFunctions',
48 ],
49 },
50 },
51 },
52});
53exports.default = util.createRule({
54 name: 'no-empty-function',
55 meta: {
56 type: 'suggestion',
57 docs: {
58 description: 'Disallow empty functions',
59 recommended: 'error',
60 extendsBaseRule: true,
61 },
62 hasSuggestions: baseRule.meta.hasSuggestions,
63 schema: [schema],
64 messages: baseRule.meta.messages,
65 },
66 defaultOptions: [
67 {
68 allow: [],
69 },
70 ],
71 create(context, [{ allow = [] }]) {
72 const rules = baseRule.create(context);
73 const isAllowedProtectedConstructors = allow.includes('protected-constructors');
74 const isAllowedPrivateConstructors = allow.includes('private-constructors');
75 const isAllowedDecoratedFunctions = allow.includes('decoratedFunctions');
76 /**
77 * Check if the method body is empty
78 * @param node the node to be validated
79 * @returns true if the body is empty
80 * @private
81 */
82 function isBodyEmpty(node) {
83 return !node.body || node.body.body.length === 0;
84 }
85 /**
86 * Check if method has parameter properties
87 * @param node the node to be validated
88 * @returns true if the body has parameter properties
89 * @private
90 */
91 function hasParameterProperties(node) {
92 var _a;
93 return (_a = node.params) === null || _a === void 0 ? void 0 : _a.some(param => param.type === utils_1.AST_NODE_TYPES.TSParameterProperty);
94 }
95 /**
96 * @param node the node to be validated
97 * @returns true if the constructor is allowed to be empty
98 * @private
99 */
100 function isAllowedEmptyConstructor(node) {
101 const parent = node.parent;
102 if (isBodyEmpty(node) &&
103 (parent === null || parent === void 0 ? void 0 : parent.type) === utils_1.AST_NODE_TYPES.MethodDefinition &&
104 parent.kind === 'constructor') {
105 const { accessibility } = parent;
106 return (
107 // allow protected constructors
108 (accessibility === 'protected' && isAllowedProtectedConstructors) ||
109 // allow private constructors
110 (accessibility === 'private' && isAllowedPrivateConstructors) ||
111 // allow constructors which have parameter properties
112 hasParameterProperties(node));
113 }
114 return false;
115 }
116 /**
117 * @param node the node to be validated
118 * @returns true if a function has decorators
119 * @private
120 */
121 function isAllowedEmptyDecoratedFunctions(node) {
122 var _a;
123 if (isAllowedDecoratedFunctions && isBodyEmpty(node)) {
124 const decorators = ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.MethodDefinition
125 ? node.parent.decorators
126 : undefined;
127 return !!decorators && !!decorators.length;
128 }
129 return false;
130 }
131 return Object.assign(Object.assign({}, rules), { FunctionExpression(node) {
132 if (isAllowedEmptyConstructor(node) ||
133 isAllowedEmptyDecoratedFunctions(node)) {
134 return;
135 }
136 rules.FunctionExpression(node);
137 },
138 FunctionDeclaration(node) {
139 if (isAllowedEmptyDecoratedFunctions(node)) {
140 return;
141 }
142 rules.FunctionDeclaration(node);
143 } });
144 },
145});
146//# sourceMappingURL=no-empty-function.js.map
\No newline at end of file