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 getESLintCoreRule_1 = require("../util/getESLintCoreRule");
|
24 | const util = __importStar(require("../util"));
|
25 | const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('no-empty-function');
|
26 | const schema = util.deepMerge(
|
27 |
|
28 | Array.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 | });
|
53 | exports.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 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function isBodyEmpty(node) {
|
83 | return !node.body || node.body.body.length === 0;
|
84 | }
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
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 |
|
97 |
|
98 |
|
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 |
|
108 | (accessibility === 'protected' && isAllowedProtectedConstructors) ||
|
109 |
|
110 | (accessibility === 'private' && isAllowedPrivateConstructors) ||
|
111 |
|
112 | hasParameterProperties(node));
|
113 | }
|
114 | return false;
|
115 | }
|
116 | |
117 |
|
118 |
|
119 |
|
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 |
|
\ | No newline at end of file |