1 | "use strict";
|
2 | var __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 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var ts = require("typescript");
|
14 | var Lint = require("tslint");
|
15 | var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
|
16 | var AstUtils_1 = require("./utils/AstUtils");
|
17 | var Utils_1 = require("./utils/Utils");
|
18 | var FAILURE_STRING = 'A stateless class was found. This indicates a failure in the object model: ';
|
19 | var 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 | group: 'Deprecated',
|
43 | commonWeaknessEnumeration: '398, 710'
|
44 | };
|
45 | Rule.isWarningShown = false;
|
46 | return Rule;
|
47 | }(Lint.Rules.AbstractRule));
|
48 | exports.Rule = Rule;
|
49 | var NoStatelessClassRuleWalker = (function (_super) {
|
50 | __extends(NoStatelessClassRuleWalker, _super);
|
51 | function NoStatelessClassRuleWalker() {
|
52 | return _super !== null && _super.apply(this, arguments) || this;
|
53 | }
|
54 | NoStatelessClassRuleWalker.prototype.visitClassDeclaration = function (node) {
|
55 | if (!this.isClassStateful(node)) {
|
56 | var className = node.name == null ? '<unknown>' : node.name.text;
|
57 | this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING + className);
|
58 | }
|
59 | _super.prototype.visitClassDeclaration.call(this, node);
|
60 | };
|
61 | NoStatelessClassRuleWalker.prototype.isClassStateful = function (node) {
|
62 | if (this.classExtendsSomething(node)) {
|
63 | return true;
|
64 | }
|
65 | if (node.members.length === 0) {
|
66 | return false;
|
67 | }
|
68 | if (this.classDeclaresConstructorProperties(node)) {
|
69 | return true;
|
70 | }
|
71 | return this.classDeclaresInstanceData(node);
|
72 | };
|
73 | NoStatelessClassRuleWalker.prototype.classDeclaresInstanceData = function (node) {
|
74 | return Utils_1.Utils.exists(node.members, function (classElement) {
|
75 | if (classElement.kind === ts.SyntaxKind.Constructor) {
|
76 | return false;
|
77 | }
|
78 | if (AstUtils_1.AstUtils.isStatic(classElement)) {
|
79 | return false;
|
80 | }
|
81 | return true;
|
82 | });
|
83 | };
|
84 | NoStatelessClassRuleWalker.prototype.classDeclaresConstructorProperties = function (node) {
|
85 | var _this = this;
|
86 | return Utils_1.Utils.exists(node.members, function (element) {
|
87 | if (element.kind === ts.SyntaxKind.Constructor) {
|
88 | return _this.constructorDeclaresProperty(element);
|
89 | }
|
90 | return false;
|
91 | });
|
92 | };
|
93 | NoStatelessClassRuleWalker.prototype.constructorDeclaresProperty = function (ctor) {
|
94 | return Utils_1.Utils.exists(ctor.parameters, function (param) {
|
95 | return AstUtils_1.AstUtils.hasModifier(param.modifiers, ts.SyntaxKind.PublicKeyword)
|
96 | || AstUtils_1.AstUtils.hasModifier(param.modifiers, ts.SyntaxKind.PrivateKeyword)
|
97 | || AstUtils_1.AstUtils.hasModifier(param.modifiers, ts.SyntaxKind.ProtectedKeyword)
|
98 | || AstUtils_1.AstUtils.hasModifier(param.modifiers, ts.SyntaxKind.ReadonlyKeyword);
|
99 | });
|
100 | };
|
101 | NoStatelessClassRuleWalker.prototype.classExtendsSomething = function (node) {
|
102 | return Utils_1.Utils.exists(node.heritageClauses, function (clause) {
|
103 | return clause.token === ts.SyntaxKind.ExtendsKeyword;
|
104 | });
|
105 | };
|
106 | return NoStatelessClassRuleWalker;
|
107 | }(ErrorTolerantWalker_1.ErrorTolerantWalker));
|
108 |
|
\ | No newline at end of file |