UNPKG

4.72 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-extraneous-class',
26 meta: {
27 type: 'suggestion',
28 docs: {
29 description: 'Forbids the use of classes as namespaces',
30 recommended: false,
31 },
32 schema: [
33 {
34 type: 'object',
35 additionalProperties: false,
36 properties: {
37 allowConstructorOnly: {
38 type: 'boolean',
39 },
40 allowEmpty: {
41 type: 'boolean',
42 },
43 allowStaticOnly: {
44 type: 'boolean',
45 },
46 allowWithDecorator: {
47 type: 'boolean',
48 },
49 },
50 },
51 ],
52 messages: {
53 empty: 'Unexpected empty class.',
54 onlyStatic: 'Unexpected class with only static properties.',
55 onlyConstructor: 'Unexpected class with only a constructor.',
56 },
57 },
58 defaultOptions: [
59 {
60 allowConstructorOnly: false,
61 allowEmpty: false,
62 allowStaticOnly: false,
63 allowWithDecorator: false,
64 },
65 ],
66 create(context, [{ allowConstructorOnly, allowEmpty, allowStaticOnly, allowWithDecorator }]) {
67 const isAllowWithDecorator = (node) => {
68 return !!(allowWithDecorator &&
69 node &&
70 node.decorators &&
71 node.decorators.length);
72 };
73 return {
74 ClassBody(node) {
75 const parent = node.parent;
76 if (!parent || parent.superClass || isAllowWithDecorator(parent)) {
77 return;
78 }
79 const reportNode = 'id' in parent && parent.id ? parent.id : parent;
80 if (node.body.length === 0) {
81 if (allowEmpty) {
82 return;
83 }
84 context.report({
85 node: reportNode,
86 messageId: 'empty',
87 });
88 return;
89 }
90 let onlyStatic = true;
91 let onlyConstructor = true;
92 for (const prop of node.body) {
93 if ('kind' in prop && prop.kind === 'constructor') {
94 if (prop.value.params.some(param => param.type === utils_1.AST_NODE_TYPES.TSParameterProperty)) {
95 onlyConstructor = false;
96 onlyStatic = false;
97 }
98 }
99 else {
100 onlyConstructor = false;
101 if ('static' in prop && !prop.static) {
102 onlyStatic = false;
103 }
104 }
105 if (!(onlyStatic || onlyConstructor)) {
106 break;
107 }
108 }
109 if (onlyConstructor) {
110 if (!allowConstructorOnly) {
111 context.report({
112 node: reportNode,
113 messageId: 'onlyConstructor',
114 });
115 }
116 return;
117 }
118 if (onlyStatic && !allowStaticOnly) {
119 context.report({
120 node: reportNode,
121 messageId: 'onlyStatic',
122 });
123 }
124 },
125 };
126 },
127});
128//# sourceMappingURL=no-extraneous-class.js.map
\No newline at end of file