UNPKG

4.95 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var ts = require("typescript");
14var Lint = require("tslint");
15var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
16var AstUtils_1 = require("./utils/AstUtils");
17var Utils_1 = require("./utils/Utils");
18var FAILURE_STRING = 'A stateless class was found. This indicates a failure in the object model: ';
19var Rule = (function (_super) {
20 __extends(Rule, _super);
21 function Rule() {
22 return _super !== null && _super.apply(this, arguments) || this;
23 }
24 Rule.prototype.apply = function (sourceFile) {
25 if (Rule.isWarningShown === false) {
26 console.warn('Warning: no-stateless-class rule is deprecated. Replace your usage with the TSLint no-unnecessary-class rule.');
27 Rule.isWarningShown = true;
28 }
29 return this.applyWithWalker(new NoStatelessClassRuleWalker(sourceFile, this.getOptions()));
30 };
31 Rule.metadata = {
32 ruleName: 'no-stateless-class',
33 type: 'maintainability',
34 description: 'A stateless class represents a failure in the object oriented design of the system.',
35 options: null,
36 optionsDescription: '',
37 typescriptOnly: true,
38 issueClass: 'Non-SDL',
39 issueType: 'Warning',
40 severity: 'Important',
41 level: 'Opportunity for Excellence',
42 recommendation: 'false,',
43 group: 'Deprecated',
44 commonWeaknessEnumeration: '398, 710'
45 };
46 Rule.isWarningShown = false;
47 return Rule;
48}(Lint.Rules.AbstractRule));
49exports.Rule = Rule;
50var NoStatelessClassRuleWalker = (function (_super) {
51 __extends(NoStatelessClassRuleWalker, _super);
52 function NoStatelessClassRuleWalker() {
53 return _super !== null && _super.apply(this, arguments) || this;
54 }
55 NoStatelessClassRuleWalker.prototype.visitClassDeclaration = function (node) {
56 if (!this.isClassStateful(node)) {
57 var className = node.name == null ? '<unknown>' : node.name.text;
58 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING + className);
59 }
60 _super.prototype.visitClassDeclaration.call(this, node);
61 };
62 NoStatelessClassRuleWalker.prototype.isClassStateful = function (node) {
63 if (this.classExtendsSomething(node)) {
64 return true;
65 }
66 if (node.members.length === 0) {
67 return false;
68 }
69 if (this.classDeclaresConstructorProperties(node)) {
70 return true;
71 }
72 return this.classDeclaresInstanceData(node);
73 };
74 NoStatelessClassRuleWalker.prototype.classDeclaresInstanceData = function (node) {
75 return Utils_1.Utils.exists(node.members, function (classElement) {
76 if (classElement.kind === ts.SyntaxKind.Constructor) {
77 return false;
78 }
79 if (AstUtils_1.AstUtils.isStatic(classElement)) {
80 return false;
81 }
82 return true;
83 });
84 };
85 NoStatelessClassRuleWalker.prototype.classDeclaresConstructorProperties = function (node) {
86 var _this = this;
87 return Utils_1.Utils.exists(node.members, function (element) {
88 if (element.kind === ts.SyntaxKind.Constructor) {
89 return _this.constructorDeclaresProperty(element);
90 }
91 return false;
92 });
93 };
94 NoStatelessClassRuleWalker.prototype.constructorDeclaresProperty = function (ctor) {
95 return Utils_1.Utils.exists(ctor.parameters, function (param) {
96 return AstUtils_1.AstUtils.hasModifier(param.modifiers, ts.SyntaxKind.PublicKeyword)
97 || AstUtils_1.AstUtils.hasModifier(param.modifiers, ts.SyntaxKind.PrivateKeyword)
98 || AstUtils_1.AstUtils.hasModifier(param.modifiers, ts.SyntaxKind.ProtectedKeyword)
99 || AstUtils_1.AstUtils.hasModifier(param.modifiers, ts.SyntaxKind.ReadonlyKeyword);
100 });
101 };
102 NoStatelessClassRuleWalker.prototype.classExtendsSomething = function (node) {
103 return Utils_1.Utils.exists(node.heritageClauses, function (clause) {
104 return clause.token === ts.SyntaxKind.ExtendsKeyword;
105 });
106 };
107 return NoStatelessClassRuleWalker;
108}(ErrorTolerantWalker_1.ErrorTolerantWalker));
109//# sourceMappingURL=noStatelessClassRule.js.map
\No newline at end of file