4 * Copyright 2017 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");
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);
29 return _super !== null && _super.apply(this, arguments) || this;
31 Rule.prototype.apply = function (sourceFile) {
33 checkSuperCall: this.ruleArguments.length !== 0 &&
34 this.ruleArguments[0]["check-super-calls"] === true,
36 return this.applyWithFunction(sourceFile, walk, options);
39 description: "Prevents blank constructors, as they are redundant.",
40 optionExamples: [true, [true, (_a = {}, _a[OPTION_CHECK_SUPER_CALL] = true, _a)]],
43 _b[OPTION_CHECK_SUPER_CALL] = { type: "boolean" },
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,
53 Rule.FAILURE_STRING = "Remove unnecessary empty constructor.";
55 }(Lint.Rules.AbstractRule));
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) {
65 if (superArguments.length === constructorParameters.length) {
66 if (constructorParameters.length === 0) {
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) {
83 var isEmptyOrContainsOnlySuper = function (node, options) {
84 if (node.body !== undefined) {
85 var checkSuperCall = options.checkSuperCall;
86 if (node.body.statements.length === 0) {
90 return (node.body.statements.length === 1 &&
91 containsSuper(node.body.statements[0], node.parameters));
96 var containsConstructorParameter = function (node) {
97 // If this has any parameter properties
98 return node.parameters.some(tsutils_1.isParameterProperty);
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; });
106 var containsDecorator = function (node) {
107 return node.parameters.some(function (p) { return p.decorators !== undefined; });
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));
124 context.addFailureAtNode(node, Rule.FAILURE_STRING, replacements);
127 ts.forEachChild(node, callback);
130 return ts.forEachChild(context.sourceFile, callback);
132 var templateObject_1, templateObject_2;