UNPKG

9.89 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"));
24const accessibilityLevel = { enum: ['explicit', 'no-public', 'off'] };
25exports.default = util.createRule({
26 name: 'explicit-member-accessibility',
27 meta: {
28 type: 'problem',
29 docs: {
30 description: 'Require explicit accessibility modifiers on class properties and methods',
31 // too opinionated to be recommended
32 recommended: false,
33 },
34 fixable: 'code',
35 messages: {
36 missingAccessibility: 'Missing accessibility modifier on {{type}} {{name}}.',
37 unwantedPublicAccessibility: 'Public accessibility modifier on {{type}} {{name}}.',
38 },
39 schema: [
40 {
41 type: 'object',
42 properties: {
43 accessibility: accessibilityLevel,
44 overrides: {
45 type: 'object',
46 properties: {
47 accessors: accessibilityLevel,
48 constructors: accessibilityLevel,
49 methods: accessibilityLevel,
50 properties: accessibilityLevel,
51 parameterProperties: accessibilityLevel,
52 },
53 additionalProperties: false,
54 },
55 ignoredMethodNames: {
56 type: 'array',
57 items: {
58 type: 'string',
59 },
60 },
61 },
62 additionalProperties: false,
63 },
64 ],
65 },
66 defaultOptions: [{ accessibility: 'explicit' }],
67 create(context, [option]) {
68 var _a, _b, _c, _d, _e, _f, _g, _h;
69 const sourceCode = context.getSourceCode();
70 const baseCheck = (_a = option.accessibility) !== null && _a !== void 0 ? _a : 'explicit';
71 const overrides = (_b = option.overrides) !== null && _b !== void 0 ? _b : {};
72 const ctorCheck = (_c = overrides.constructors) !== null && _c !== void 0 ? _c : baseCheck;
73 const accessorCheck = (_d = overrides.accessors) !== null && _d !== void 0 ? _d : baseCheck;
74 const methodCheck = (_e = overrides.methods) !== null && _e !== void 0 ? _e : baseCheck;
75 const propCheck = (_f = overrides.properties) !== null && _f !== void 0 ? _f : baseCheck;
76 const paramPropCheck = (_g = overrides.parameterProperties) !== null && _g !== void 0 ? _g : baseCheck;
77 const ignoredMethodNames = new Set((_h = option.ignoredMethodNames) !== null && _h !== void 0 ? _h : []);
78 /**
79 * Generates the report for rule violations
80 */
81 function reportIssue(messageId, nodeType, node, nodeName, fix = null) {
82 context.report({
83 node,
84 messageId,
85 data: {
86 type: nodeType,
87 name: nodeName,
88 },
89 fix,
90 });
91 }
92 /**
93 * Checks if a method declaration has an accessibility modifier.
94 * @param methodDefinition The node representing a MethodDefinition.
95 */
96 function checkMethodAccessibilityModifier(methodDefinition) {
97 if (methodDefinition.key.type === utils_1.AST_NODE_TYPES.PrivateIdentifier) {
98 return;
99 }
100 let nodeType = 'method definition';
101 let check = baseCheck;
102 switch (methodDefinition.kind) {
103 case 'method':
104 check = methodCheck;
105 break;
106 case 'constructor':
107 check = ctorCheck;
108 break;
109 case 'get':
110 case 'set':
111 check = accessorCheck;
112 nodeType = `${methodDefinition.kind} property accessor`;
113 break;
114 }
115 const { name: methodName } = util.getNameFromMember(methodDefinition, sourceCode);
116 if (check === 'off' || ignoredMethodNames.has(methodName)) {
117 return;
118 }
119 if (check === 'no-public' &&
120 methodDefinition.accessibility === 'public') {
121 reportIssue('unwantedPublicAccessibility', nodeType, methodDefinition, methodName, getUnwantedPublicAccessibilityFixer(methodDefinition));
122 }
123 else if (check === 'explicit' && !methodDefinition.accessibility) {
124 reportIssue('missingAccessibility', nodeType, methodDefinition, methodName);
125 }
126 }
127 /**
128 * Creates a fixer that removes a "public" keyword with following spaces
129 */
130 function getUnwantedPublicAccessibilityFixer(node) {
131 return function (fixer) {
132 const tokens = sourceCode.getTokens(node);
133 let rangeToRemove;
134 for (let i = 0; i < tokens.length; i++) {
135 const token = tokens[i];
136 if (token.type === utils_1.AST_TOKEN_TYPES.Keyword &&
137 token.value === 'public') {
138 const commensAfterPublicKeyword = sourceCode.getCommentsAfter(token);
139 if (commensAfterPublicKeyword.length) {
140 // public /* Hi there! */ static foo()
141 // ^^^^^^^
142 rangeToRemove = [
143 token.range[0],
144 commensAfterPublicKeyword[0].range[0],
145 ];
146 break;
147 }
148 else {
149 // public static foo()
150 // ^^^^^^^
151 rangeToRemove = [token.range[0], tokens[i + 1].range[0]];
152 break;
153 }
154 }
155 }
156 return fixer.removeRange(rangeToRemove);
157 };
158 }
159 /**
160 * Checks if property has an accessibility modifier.
161 * @param propertyDefinition The node representing a PropertyDefinition.
162 */
163 function checkPropertyAccessibilityModifier(propertyDefinition) {
164 if (propertyDefinition.key.type === utils_1.AST_NODE_TYPES.PrivateIdentifier) {
165 return;
166 }
167 const nodeType = 'class property';
168 const { name: propertyName } = util.getNameFromMember(propertyDefinition, sourceCode);
169 if (propCheck === 'no-public' &&
170 propertyDefinition.accessibility === 'public') {
171 reportIssue('unwantedPublicAccessibility', nodeType, propertyDefinition, propertyName, getUnwantedPublicAccessibilityFixer(propertyDefinition));
172 }
173 else if (propCheck === 'explicit' &&
174 !propertyDefinition.accessibility) {
175 reportIssue('missingAccessibility', nodeType, propertyDefinition, propertyName);
176 }
177 }
178 /**
179 * Checks that the parameter property has the desired accessibility modifiers set.
180 * @param node The node representing a Parameter Property
181 */
182 function checkParameterPropertyAccessibilityModifier(node) {
183 const nodeType = 'parameter property';
184 // HAS to be an identifier or assignment or TSC will throw
185 if (node.parameter.type !== utils_1.AST_NODE_TYPES.Identifier &&
186 node.parameter.type !== utils_1.AST_NODE_TYPES.AssignmentPattern) {
187 return;
188 }
189 const nodeName = node.parameter.type === utils_1.AST_NODE_TYPES.Identifier
190 ? node.parameter.name
191 : // has to be an Identifier or TSC will throw an error
192 node.parameter.left.name;
193 switch (paramPropCheck) {
194 case 'explicit': {
195 if (!node.accessibility) {
196 reportIssue('missingAccessibility', nodeType, node, nodeName);
197 }
198 break;
199 }
200 case 'no-public': {
201 if (node.accessibility === 'public' && node.readonly) {
202 reportIssue('unwantedPublicAccessibility', nodeType, node, nodeName, getUnwantedPublicAccessibilityFixer(node));
203 }
204 break;
205 }
206 }
207 }
208 return {
209 'MethodDefinition, TSAbstractMethodDefinition': checkMethodAccessibilityModifier,
210 'PropertyDefinition, TSAbstractPropertyDefinition': checkPropertyAccessibilityModifier,
211 TSParameterProperty: checkParameterPropertyAccessibilityModifier,
212 };
213 },
214});
215//# sourceMappingURL=explicit-member-accessibility.js.map
\No newline at end of file