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 FAILURE_UNDEFINED_INIT = 'Unnecessary field initialization. Field explicitly initialized to undefined: ';
|
18 | var FAILURE_UNDEFINED_DUPE = 'Unnecessary field initialization. Field value already initialized in declaration: ';
|
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 | return this.applyWithWalker(new UnnecessaryFieldInitializationRuleWalker(sourceFile, this.getOptions()));
|
26 | };
|
27 | Rule.metadata = {
|
28 | ruleName: 'no-unnecessary-field-initialization',
|
29 | type: 'maintainability',
|
30 | description: 'Do not unnecessarily initialize the fields of a class to values they already have.',
|
31 | options: null,
|
32 | optionsDescription: '',
|
33 | typescriptOnly: true,
|
34 | issueClass: 'Non-SDL',
|
35 | issueType: 'Warning',
|
36 | severity: 'Moderate',
|
37 | level: 'Opportunity for Excellence',
|
38 | group: 'Clarity',
|
39 | commonWeaknessEnumeration: '398, 710'
|
40 | };
|
41 | return Rule;
|
42 | }(Lint.Rules.AbstractRule));
|
43 | exports.Rule = Rule;
|
44 | var UnnecessaryFieldInitializationRuleWalker = (function (_super) {
|
45 | __extends(UnnecessaryFieldInitializationRuleWalker, _super);
|
46 | function UnnecessaryFieldInitializationRuleWalker() {
|
47 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
48 | _this.fieldInitializations = {};
|
49 | return _this;
|
50 | }
|
51 | UnnecessaryFieldInitializationRuleWalker.prototype.visitClassDeclaration = function (node) {
|
52 | var _this = this;
|
53 | this.fieldInitializations = {};
|
54 | node.members.forEach(function (member) {
|
55 | if (member.kind === ts.SyntaxKind.PropertyDeclaration) {
|
56 | _this.visitPropertyDeclaration(member);
|
57 | }
|
58 | else if (member.kind === ts.SyntaxKind.Constructor) {
|
59 | _this.visitConstructorDeclaration(member);
|
60 | }
|
61 | });
|
62 | this.fieldInitializations = {};
|
63 | };
|
64 | UnnecessaryFieldInitializationRuleWalker.prototype.visitPropertyDeclaration = function (node) {
|
65 | var initializer = node.initializer;
|
66 | if (node.name.kind === ts.SyntaxKind.Identifier) {
|
67 | var fieldName = 'this.' + node.name.getText();
|
68 | if (initializer == null) {
|
69 | this.fieldInitializations[fieldName] = undefined;
|
70 | }
|
71 | else if (AstUtils_1.AstUtils.isConstant(initializer)) {
|
72 | this.fieldInitializations[fieldName] = initializer.getText();
|
73 | }
|
74 | }
|
75 | if (AstUtils_1.AstUtils.isUndefined(initializer)) {
|
76 | var start = initializer.getStart();
|
77 | var width = initializer.getWidth();
|
78 | this.addFailureAt(start, width, FAILURE_UNDEFINED_INIT + node.name.getText());
|
79 | }
|
80 | };
|
81 | UnnecessaryFieldInitializationRuleWalker.prototype.visitConstructorDeclaration = function (node) {
|
82 | var _this = this;
|
83 | if (node.body != null) {
|
84 | node.body.statements.forEach(function (statement) {
|
85 | if (statement.kind === ts.SyntaxKind.ExpressionStatement) {
|
86 | var expression = statement.expression;
|
87 | if (expression.kind === ts.SyntaxKind.BinaryExpression) {
|
88 | var binaryExpression = expression;
|
89 | var property = binaryExpression.left;
|
90 | var propertyName = property.getText();
|
91 | if (Object.keys(_this.fieldInitializations).indexOf(propertyName) > -1) {
|
92 | if (AstUtils_1.AstUtils.isUndefined(binaryExpression.right)) {
|
93 | if (Object.keys(_this.fieldInitializations).indexOf(propertyName) > -1) {
|
94 | var fieldInitValue = _this.fieldInitializations[propertyName];
|
95 | if (fieldInitValue == null) {
|
96 | var start = property.getStart();
|
97 | var width = property.getWidth();
|
98 | _this.addFailureAt(start, width, FAILURE_UNDEFINED_INIT + property.getText());
|
99 | }
|
100 | }
|
101 | }
|
102 | else if (AstUtils_1.AstUtils.isConstant(binaryExpression.right)) {
|
103 | var fieldInitValue = _this.fieldInitializations[propertyName];
|
104 | if (fieldInitValue === binaryExpression.right.getText()) {
|
105 | var start = binaryExpression.getStart();
|
106 | var width = binaryExpression.getWidth();
|
107 | var message = FAILURE_UNDEFINED_DUPE + binaryExpression.getText();
|
108 | _this.addFailureAt(start, width, message);
|
109 | }
|
110 | }
|
111 | }
|
112 | }
|
113 | }
|
114 | });
|
115 | }
|
116 | };
|
117 | return UnnecessaryFieldInitializationRuleWalker;
|
118 | }(ErrorTolerantWalker_1.ErrorTolerantWalker));
|
119 |
|
\ | No newline at end of file |