4 * Copyright 2013 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var error_1 = require("../error");
23 var Lint = require("../index");
24 var OPTION_NO_PUBLIC = "no-public";
25 var OPTION_CHECK_ACCESSOR = "check-accessor";
26 var OPTION_CHECK_CONSTRUCTOR = "check-constructor";
27 var OPTION_CHECK_PARAMETER_PROPERTY = "check-parameter-property";
28 var Rule = /** @class */ (function (_super) {
29 tslib_1.__extends(Rule, _super);
31 return _super !== null && _super.apply(this, arguments) || this;
33 Rule.FAILURE_STRING_FACTORY = function (memberType, memberName) {
34 memberName = memberName === undefined ? "" : " '" + memberName + "'";
35 return "The " + memberType + memberName + " must be marked either 'private', 'public', or 'protected'";
37 Rule.prototype.apply = function (sourceFile) {
38 var options = this.ruleArguments;
39 var noPublic = options.indexOf(OPTION_NO_PUBLIC) !== -1;
40 var checkAccessor = options.indexOf(OPTION_CHECK_ACCESSOR) !== -1;
41 var checkConstructor = options.indexOf(OPTION_CHECK_CONSTRUCTOR) !== -1;
42 var checkParameterProperty = options.indexOf(OPTION_CHECK_PARAMETER_PROPERTY) !== -1;
44 if (checkAccessor || checkConstructor || checkParameterProperty) {
45 error_1.showWarningOnce("Warning: " + this.ruleName + " - If 'no-public' is present, it should be the only option.");
48 checkAccessor = checkConstructor = checkParameterProperty = true;
50 return this.applyWithFunction(sourceFile, walk, {
51 checkAccessor: checkAccessor,
52 checkConstructor: checkConstructor,
53 checkParameterProperty: checkParameterProperty,
57 /* tslint:disable:object-literal-sort-keys */
59 ruleName: "member-access",
60 description: "Requires explicit visibility declarations for class members.",
61 rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Explicit visibility declarations can make code more readable and accessible for those new to TS.\n\n Other languages such as C# default to `private`, unlike TypeScript's default of `public`.\n Members lacking a visibility declaration may be an indication of an accidental leak of class internals.\n "], ["\n Explicit visibility declarations can make code more readable and accessible for those new to TS.\n\n Other languages such as C# default to \\`private\\`, unlike TypeScript's default of \\`public\\`.\n Members lacking a visibility declaration may be an indication of an accidental leak of class internals.\n "]))),
62 optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n These arguments may be optionally provided:\n\n * `\"no-public\"` forbids public accessibility to be specified, because this is the default.\n * `\"check-accessor\"` enforces explicit visibility on get/set accessors\n * `\"check-constructor\"` enforces explicit visibility on constructors\n * `\"check-parameter-property\"` enforces explicit visibility on parameter properties"], ["\n These arguments may be optionally provided:\n\n * \\`\"no-public\"\\` forbids public accessibility to be specified, because this is the default.\n * \\`\"check-accessor\"\\` enforces explicit visibility on get/set accessors\n * \\`\"check-constructor\"\\` enforces explicit visibility on constructors\n * \\`\"check-parameter-property\"\\` enforces explicit visibility on parameter properties"]))),
69 OPTION_CHECK_ACCESSOR,
70 OPTION_CHECK_CONSTRUCTOR,
71 OPTION_CHECK_PARAMETER_PROPERTY,
77 optionExamples: [true, [true, OPTION_NO_PUBLIC], [true, OPTION_CHECK_ACCESSOR]],
82 /* tslint:enable:object-literal-sort-keys */
83 Rule.FAILURE_STRING_NO_PUBLIC = "'public' is implicit.";
85 }(Lint.Rules.AbstractRule));
88 var _a = ctx.options, noPublic = _a.noPublic, checkAccessor = _a.checkAccessor, checkConstructor = _a.checkConstructor, checkParameterProperty = _a.checkParameterProperty;
89 return ts.forEachChild(ctx.sourceFile, function recur(node) {
90 if (tsutils_1.isClassLikeDeclaration(node)) {
91 for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
93 if (shouldCheck(child)) {
96 if (checkParameterProperty &&
97 tsutils_1.isConstructorDeclaration(child) &&
98 child.body !== undefined) {
99 for (var _b = 0, _c = child.parameters; _b < _c.length; _b++) {
101 if (tsutils_1.isParameterProperty(param)) {
108 return ts.forEachChild(node, recur);
110 function shouldCheck(node) {
112 case ts.SyntaxKind.Constructor:
113 return checkConstructor;
114 case ts.SyntaxKind.GetAccessor:
115 case ts.SyntaxKind.SetAccessor:
116 return checkAccessor;
117 case ts.SyntaxKind.MethodDeclaration:
118 case ts.SyntaxKind.PropertyDeclaration:
124 function check(node) {
125 if (tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.PrivateKeyword)) {
128 var publicKeyword = tsutils_1.getModifier(node, ts.SyntaxKind.PublicKeyword);
129 if (noPublic && publicKeyword !== undefined) {
130 // public is not optional for parameter property without the readonly modifier
131 if (node.kind !== ts.SyntaxKind.Parameter ||
132 tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.ReadonlyKeyword)) {
133 var start = publicKeyword.end - "public".length;
134 ctx.addFailure(start, publicKeyword.end, Rule.FAILURE_STRING_NO_PUBLIC, Lint.Replacement.deleteFromTo(start, tsutils_1.getNextToken(publicKeyword, ctx.sourceFile).getStart(ctx.sourceFile)));
137 if (!noPublic && publicKeyword === undefined) {
138 var nameNode = node.kind === ts.SyntaxKind.Constructor
139 ? tsutils_1.getChildOfKind(node, ts.SyntaxKind.ConstructorKeyword, ctx.sourceFile)
140 : node.name !== undefined
143 var memberName = node.name !== undefined && node.name.kind === ts.SyntaxKind.Identifier
146 ctx.addFailureAtNode(nameNode, Rule.FAILURE_STRING_FACTORY(typeToString(node), memberName), Lint.Replacement.appendText(getInsertionPosition(node, ctx.sourceFile), "public "));
150 function getInsertionPosition(member, sourceFile) {
151 var node = member.decorators === undefined
153 : tsutils_1.getTokenAtPosition(member, member.decorators.end, sourceFile);
154 return node.getStart(sourceFile);
156 function typeToString(node) {
158 case ts.SyntaxKind.MethodDeclaration:
159 return "class method";
160 case ts.SyntaxKind.PropertyDeclaration:
161 return "class property";
162 case ts.SyntaxKind.Constructor:
163 return "class constructor";
164 case ts.SyntaxKind.GetAccessor:
165 return "get property accessor";
166 case ts.SyntaxKind.SetAccessor:
167 return "set property accessor";
168 case ts.SyntaxKind.Parameter:
169 return "parameter property";
171 throw new Error("unhandled node type " + ts.SyntaxKind[node.kind]);
174 var templateObject_1, templateObject_2;