.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / memberAccessRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
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);
30     function Rule() {
31         return _super !== null && _super.apply(this, arguments) || this;
32     }
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'";
36     };
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;
43         if (noPublic) {
44             if (checkAccessor || checkConstructor || checkParameterProperty) {
45                 error_1.showWarningOnce("Warning: " + this.ruleName + " - If 'no-public' is present, it should be the only option.");
46                 return [];
47             }
48             checkAccessor = checkConstructor = checkParameterProperty = true;
49         }
50         return this.applyWithFunction(sourceFile, walk, {
51             checkAccessor: checkAccessor,
52             checkConstructor: checkConstructor,
53             checkParameterProperty: checkParameterProperty,
54             noPublic: noPublic,
55         });
56     };
57     /* tslint:disable:object-literal-sort-keys */
58     Rule.metadata = {
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"]))),
63         options: {
64             type: "array",
65             items: {
66                 type: "string",
67                 enum: [
68                     OPTION_NO_PUBLIC,
69                     OPTION_CHECK_ACCESSOR,
70                     OPTION_CHECK_CONSTRUCTOR,
71                     OPTION_CHECK_PARAMETER_PROPERTY,
72                 ],
73             },
74             minLength: 0,
75             maxLength: 4,
76         },
77         optionExamples: [true, [true, OPTION_NO_PUBLIC], [true, OPTION_CHECK_ACCESSOR]],
78         type: "typescript",
79         typescriptOnly: true,
80         hasFix: true,
81     };
82     /* tslint:enable:object-literal-sort-keys */
83     Rule.FAILURE_STRING_NO_PUBLIC = "'public' is implicit.";
84     return Rule;
85 }(Lint.Rules.AbstractRule));
86 exports.Rule = Rule;
87 function walk(ctx) {
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++) {
92                 var child = _a[_i];
93                 if (shouldCheck(child)) {
94                     check(child);
95                 }
96                 if (checkParameterProperty &&
97                     tsutils_1.isConstructorDeclaration(child) &&
98                     child.body !== undefined) {
99                     for (var _b = 0, _c = child.parameters; _b < _c.length; _b++) {
100                         var param = _c[_b];
101                         if (tsutils_1.isParameterProperty(param)) {
102                             check(param);
103                         }
104                     }
105                 }
106             }
107         }
108         return ts.forEachChild(node, recur);
109     });
110     function shouldCheck(node) {
111         switch (node.kind) {
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:
119                 return true;
120             default:
121                 return false;
122         }
123     }
124     function check(node) {
125         if (tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.PrivateKeyword)) {
126             return;
127         }
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)));
135             }
136         }
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
141                     ? node.name
142                     : node;
143             var memberName = node.name !== undefined && node.name.kind === ts.SyntaxKind.Identifier
144                 ? node.name.text
145                 : undefined;
146             ctx.addFailureAtNode(nameNode, Rule.FAILURE_STRING_FACTORY(typeToString(node), memberName), Lint.Replacement.appendText(getInsertionPosition(node, ctx.sourceFile), "public "));
147         }
148     }
149 }
150 function getInsertionPosition(member, sourceFile) {
151     var node = member.decorators === undefined
152         ? member
153         : tsutils_1.getTokenAtPosition(member, member.decorators.end, sourceFile);
154     return node.getStart(sourceFile);
155 }
156 function typeToString(node) {
157     switch (node.kind) {
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";
170         default:
171             throw new Error("unhandled node type " + ts.SyntaxKind[node.kind]);
172     }
173 }
174 var templateObject_1, templateObject_2;