diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-12-10 21:51:33 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-12-10 21:51:33 +0100 |
commit | 0469abd4a9c9270a1fdc962969e36e63699af8b4 (patch) | |
tree | f9864d4a4148621378958794cbbfdc2393733283 /node_modules/tslint/lib/rules/noUnnecessaryClassRule.js | |
parent | 6947e79bbc258f7bc96af424ddb71a511f0c15a3 (diff) |
upgrade dependencies
Diffstat (limited to 'node_modules/tslint/lib/rules/noUnnecessaryClassRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/noUnnecessaryClassRule.js | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/node_modules/tslint/lib/rules/noUnnecessaryClassRule.js b/node_modules/tslint/lib/rules/noUnnecessaryClassRule.js new file mode 100644 index 000000000..fff8b5ed1 --- /dev/null +++ b/node_modules/tslint/lib/rules/noUnnecessaryClassRule.js @@ -0,0 +1,113 @@ +"use strict"; +/** + * @license + * Copyright 2017 Palantir Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +Object.defineProperty(exports, "__esModule", { value: true }); +var tslib_1 = require("tslib"); +var ts = require("typescript"); +var Lint = require("../index"); +var tsutils_1 = require("tsutils"); +var OPTION__ALLOW_CONSTRUCTOR_ONLY = "allow-constructor-only"; +var OPTION__ALLOW_EMPTY_CLASS = "allow-empty-class"; +var OPTION__ALLOW_STATIC_ONLY = "allow-static-only"; +function parseOptions(options) { + return { + allowConstructorOnly: options.indexOf(OPTION__ALLOW_CONSTRUCTOR_ONLY) !== -1, + allowEmptyClass: options.indexOf(OPTION__ALLOW_EMPTY_CLASS) !== -1, + allowStaticOnly: options.indexOf(OPTION__ALLOW_STATIC_ONLY) !== -1, + }; +} +var Rule = /** @class */ (function (_super) { + tslib_1.__extends(Rule, _super); + function Rule() { + return _super !== null && _super.apply(this, arguments) || this; + } + Rule.prototype.apply = function (sourceFile) { + return this.applyWithWalker(new NoUnnecessaryClassWalker(sourceFile, this.ruleName, parseOptions(this.ruleArguments))); + }; + /* tslint:disable:object-literal-sort-keys */ + Rule.metadata = { + ruleName: "no-unnecessary-class", + description: (_a = ["\n Disallows classes that are not strictly necessary."], _a.raw = ["\n Disallows classes that are not strictly necessary."], Lint.Utils.dedent(_a)), + rationale: (_b = ["\n Users who come from a Java-style OO language may wrap\n their utility functions in an extra class, instead of\n putting them at the top level."], _b.raw = ["\n Users who come from a Java-style OO language may wrap\n their utility functions in an extra class, instead of\n putting them at the top level."], Lint.Utils.dedent(_b)), + optionsDescription: (_c = ["\n Three arguments may be optionally provided:\n\n * `\"allow-constructor-only\"` ignores classes whose members are constructors.\n * `\"allow-empty-class\"` ignores `class DemoClass {}`.\n * `\"allow-static-only\"` ignores classes whose members are static."], _c.raw = ["\n Three arguments may be optionally provided:\n\n * \\`\"allow-constructor-only\"\\` ignores classes whose members are constructors.\n * \\`\"allow-empty-class\"\\` ignores \\`class DemoClass {}\\`.\n * \\`\"allow-static-only\"\\` ignores classes whose members are static."], Lint.Utils.dedent(_c)), + options: { + type: "array", + items: { + type: "string", + }, + minLength: 0, + maxLength: 3, + }, + optionExamples: [true, ["allow-empty-class", "allow-constructor-only"]], + type: "functionality", + typescriptOnly: false, + }; + /* tslint:enable:object-literal-sort-keys */ + Rule.FAILURE_CONSTRUCTOR_ONLY = "Every member of this class is a constructor. Use functions instead."; + Rule.FAILURE_STATIC_ONLY = "Every member of this class is static. Use namespaces or plain objects instead."; + Rule.FAILURE_EMPTY_CLASS = "This class has no members."; + return Rule; +}(Lint.Rules.AbstractRule)); +exports.Rule = Rule; +var NoUnnecessaryClassWalker = /** @class */ (function (_super) { + tslib_1.__extends(NoUnnecessaryClassWalker, _super); + function NoUnnecessaryClassWalker() { + return _super !== null && _super.apply(this, arguments) || this; + } + NoUnnecessaryClassWalker.prototype.walk = function (sourceFile) { + var _this = this; + var checkIfUnnecessaryClass = function (node) { + if (tsutils_1.isClassDeclaration(node) && !hasExtendsClause(node)) { + _this.checkMembers(node); + } + return ts.forEachChild(node, checkIfUnnecessaryClass); + }; + ts.forEachChild(sourceFile, checkIfUnnecessaryClass); + }; + NoUnnecessaryClassWalker.prototype.checkMembers = function (node) { + if (node.members.length === 0) { + if (!this.options.allowEmptyClass) { + this.addFailureAtNode(tsutils_1.getChildOfKind(node, ts.SyntaxKind.ClassKeyword), Rule.FAILURE_EMPTY_CLASS); + } + return; + } + var allMembersAreConstructors = node.members.every(tsutils_1.isConstructorDeclaration); + if (allMembersAreConstructors && + !this.options.allowConstructorOnly && + !node.members.some(isConstructorWithShorthandProps)) { + this.addFailureAtNode(tsutils_1.getChildOfKind(node, ts.SyntaxKind.ClassKeyword, this.sourceFile), Rule.FAILURE_CONSTRUCTOR_ONLY); + } + if (!allMembersAreConstructors && + !this.options.allowStaticOnly && + !node.members.some(isNonStaticMember)) { + this.addFailureAtNode(tsutils_1.getChildOfKind(node, ts.SyntaxKind.ClassKeyword, this.sourceFile), Rule.FAILURE_STATIC_ONLY); + } + }; + return NoUnnecessaryClassWalker; +}(Lint.AbstractWalker)); +function isNonStaticMember(member) { + return (isConstructorWithShorthandProps(member) || + (!tsutils_1.isConstructorDeclaration(member) && !tsutils_1.hasModifier(member.modifiers, ts.SyntaxKind.StaticKeyword))); +} +function hasExtendsClause(declaration) { + return (declaration.heritageClauses !== undefined && + declaration.heritageClauses[0].token === ts.SyntaxKind.ExtendsKeyword); +} +function isConstructorWithShorthandProps(member) { + return tsutils_1.isConstructorDeclaration(member) && member.parameters.some(tsutils_1.isParameterProperty); +} +var _a, _b, _c; |