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-parameter-properties',
26 meta: {
27 type: 'problem',
28 docs: {
29 description: 'Disallow the use of parameter properties in class constructors',
30 // too opinionated to be recommended
31 recommended: false,
32 },
33 messages: {
34 noParamProp: 'Property {{parameter}} cannot be declared in the constructor.',
35 },
36 schema: [
37 {
38 type: 'object',
39 properties: {
40 allows: {
41 type: 'array',
42 items: {
43 enum: [
44 'readonly',
45 'private',
46 'protected',
47 'public',
48 'private readonly',
49 'protected readonly',
50 'public readonly',
51 ],
52 },
53 minItems: 1,
54 },
55 },
56 additionalProperties: false,
57 },
58 ],
59 },
60 defaultOptions: [
61 {
62 allows: [],
63 },
64 ],
65 create(context, [{ allows }]) {
66 /**
67 * Gets the modifiers of `node`.
68 * @param node the node to be inspected.
69 */
70 function getModifiers(node) {
71 const modifiers = [];
72 if (node.accessibility) {
73 modifiers.push(node.accessibility);
74 }
75 if (node.readonly) {
76 modifiers.push('readonly');
77 }
78 return modifiers.filter(Boolean).join(' ');
79 }
80 return {
81 TSParameterProperty(node) {
82 const modifiers = getModifiers(node);
83 if (!allows.includes(modifiers)) {
84 // HAS to be an identifier or assignment or TSC will throw
85 if (node.parameter.type !== utils_1.AST_NODE_TYPES.Identifier &&
86 node.parameter.type !== utils_1.AST_NODE_TYPES.AssignmentPattern) {
87 return;
88 }
89 const name = node.parameter.type === utils_1.AST_NODE_TYPES.Identifier
90 ? node.parameter.name
91 : // has to be an Identifier or TSC will throw an error
92 node.parameter.left.name;
93 context.report({
94 node,
95 messageId: 'noParamProp',
96 data: {
97 parameter: name,
98 },
99 });
100 }
101 },
102 };
103 },
104});
105//# sourceMappingURL=no-parameter-properties.js.map
\No newline at end of file