UNPKG

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