.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / unnecessaryConstructorRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 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 _a, _b;
21 var tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var rule_1 = require("../language/rule/rule");
25 var OPTION_CHECK_SUPER_CALL = "check-super-calls";
26 var Rule = /** @class */ (function (_super) {
27     tslib_1.__extends(Rule, _super);
28     function Rule() {
29         return _super !== null && _super.apply(this, arguments) || this;
30     }
31     Rule.prototype.apply = function (sourceFile) {
32         var options = {
33             checkSuperCall: this.ruleArguments.length !== 0 &&
34                 this.ruleArguments[0]["check-super-calls"] === true,
35         };
36         return this.applyWithFunction(sourceFile, walk, options);
37     };
38     Rule.metadata = {
39         description: "Prevents blank constructors, as they are redundant.",
40         optionExamples: [true, [true, (_a = {}, _a[OPTION_CHECK_SUPER_CALL] = true, _a)]],
41         options: {
42             properties: (_b = {},
43                 _b[OPTION_CHECK_SUPER_CALL] = { type: "boolean" },
44                 _b),
45             type: "object",
46         },
47         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            An optional object with the property '", "'.\n            This is to check for unnecessary constructor parameters for super call"], ["\n            An optional object with the property '", "'.\n            This is to check for unnecessary constructor parameters for super call"])), OPTION_CHECK_SUPER_CALL),
48         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            JavaScript implicitly adds a blank constructor when there isn't one.\n            It's not necessary to manually add one in.\n        "], ["\n            JavaScript implicitly adds a blank constructor when there isn't one.\n            It's not necessary to manually add one in.\n        "]))),
49         ruleName: "unnecessary-constructor",
50         type: "functionality",
51         typescriptOnly: false,
52     };
53     Rule.FAILURE_STRING = "Remove unnecessary empty constructor.";
54     return Rule;
55 }(Lint.Rules.AbstractRule));
56 exports.Rule = Rule;
57 var containsSuper = function (statement, constructorParameters) {
58     if (tsutils_1.isExpressionStatement(statement) &&
59         tsutils_1.isCallExpression(statement.expression) &&
60         ts.SyntaxKind.SuperKeyword === statement.expression.expression.kind) {
61         var superArguments = statement.expression.arguments;
62         if (superArguments.length < constructorParameters.length) {
63             return true;
64         }
65         if (superArguments.length === constructorParameters.length) {
66             if (constructorParameters.length === 0) {
67                 return true;
68             }
69             for (var _i = 0, constructorParameters_1 = constructorParameters; _i < constructorParameters_1.length; _i++) {
70                 var constructorParameter = constructorParameters_1[_i];
71                 for (var _a = 0, superArguments_1 = superArguments; _a < superArguments_1.length; _a++) {
72                     var superArgument = superArguments_1[_a];
73                     if (constructorParameter.name.kind !== superArgument.kind) {
74                         return false;
75                     }
76                 }
77             }
78             return true;
79         }
80     }
81     return false;
82 };
83 var isEmptyOrContainsOnlySuper = function (node, options) {
84     if (node.body !== undefined) {
85         var checkSuperCall = options.checkSuperCall;
86         if (node.body.statements.length === 0) {
87             return true;
88         }
89         if (checkSuperCall) {
90             return (node.body.statements.length === 1 &&
91                 containsSuper(node.body.statements[0], node.parameters));
92         }
93     }
94     return false;
95 };
96 var containsConstructorParameter = function (node) {
97     // If this has any parameter properties
98     return node.parameters.some(tsutils_1.isParameterProperty);
99 };
100 var isAccessRestrictingConstructor = function (node) {
101     // No modifiers implies public
102     return node.modifiers != undefined &&
103         // If this has any modifier that isn't public, it's doing something
104         node.modifiers.some(function (modifier) { return modifier.kind !== ts.SyntaxKind.PublicKeyword; });
105 };
106 var containsDecorator = function (node) {
107     return node.parameters.some(function (p) { return p.decorators !== undefined; });
108 };
109 function walk(context) {
110     var callback = function (node) {
111         if (tsutils_1.isConstructorDeclaration(node) &&
112             isEmptyOrContainsOnlySuper(node, context.options) &&
113             !containsConstructorParameter(node) &&
114             !containsDecorator(node) &&
115             !isAccessRestrictingConstructor(node)) {
116             var replacements = [];
117             // Since only one constructor implementation is allowed and the one found above is empty, all other overloads can be safely removed.
118             for (var _i = 0, _a = node.parent.members; _i < _a.length; _i++) {
119                 var maybeConstructor = _a[_i];
120                 if (tsutils_1.isConstructorDeclaration(maybeConstructor)) {
121                     replacements.push(rule_1.Replacement.deleteFromTo(maybeConstructor.pos, maybeConstructor.end));
122                 }
123             }
124             context.addFailureAtNode(node, Rule.FAILURE_STRING, replacements);
125         }
126         else {
127             ts.forEachChild(node, callback);
128         }
129     };
130     return ts.forEachChild(context.sourceFile, callback);
131 }
132 var templateObject_1, templateObject_2;