1 | "use strict";
|
2 | var __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 | })();
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | var ts = require("typescript");
|
17 | var Lint = require("tslint");
|
18 | var tsutils = require("tsutils");
|
19 | var AstUtils_1 = require("./utils/AstUtils");
|
20 | var FAILURE_UNDEFINED_INIT = 'Unnecessary field initialization. Field explicitly initialized to undefined: ';
|
21 | var FAILURE_UNDEFINED_DUPE = 'Unnecessary field initialization. Field value already initialized in declaration: ';
|
22 | var 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 | return this.applyWithFunction(sourceFile, walk);
|
29 | };
|
30 | Rule.metadata = {
|
31 | ruleName: 'no-unnecessary-field-initialization',
|
32 | type: 'maintainability',
|
33 | description: 'Do not unnecessarily initialize the fields of a class to values they already have.',
|
34 | options: null,
|
35 | optionsDescription: '',
|
36 | typescriptOnly: true,
|
37 | issueClass: 'Non-SDL',
|
38 | issueType: 'Warning',
|
39 | severity: 'Moderate',
|
40 | level: 'Opportunity for Excellence',
|
41 | group: 'Clarity',
|
42 | commonWeaknessEnumeration: '398, 710'
|
43 | };
|
44 | return Rule;
|
45 | }(Lint.Rules.AbstractRule));
|
46 | exports.Rule = Rule;
|
47 | function walk(ctx) {
|
48 | var fieldInitializations = {};
|
49 | function visitConstructorDeclaration(node) {
|
50 | if (node.body !== undefined) {
|
51 | node.body.statements.forEach(function (statement) {
|
52 | if (tsutils.isExpressionStatement(statement)) {
|
53 | var expression = statement.expression;
|
54 | if (tsutils.isBinaryExpression(expression)) {
|
55 | var binaryExpression = expression;
|
56 | var property = binaryExpression.left;
|
57 | var propertyName = property.getText();
|
58 | if (Object.keys(fieldInitializations).indexOf(propertyName) > -1) {
|
59 | if (AstUtils_1.AstUtils.isUndefined(binaryExpression.right)) {
|
60 | if (Object.keys(fieldInitializations).indexOf(propertyName) > -1) {
|
61 | var fieldInitValue = fieldInitializations[propertyName];
|
62 | if (fieldInitValue === undefined) {
|
63 | var start = property.getStart();
|
64 | var width = property.getWidth();
|
65 | ctx.addFailureAt(start, width, FAILURE_UNDEFINED_INIT + property.getText());
|
66 | }
|
67 | }
|
68 | }
|
69 | else if (AstUtils_1.AstUtils.isConstant(binaryExpression.right)) {
|
70 | var fieldInitValue = fieldInitializations[propertyName];
|
71 | if (fieldInitValue === binaryExpression.right.getText()) {
|
72 | var start = binaryExpression.getStart();
|
73 | var width = binaryExpression.getWidth();
|
74 | var message = FAILURE_UNDEFINED_DUPE + binaryExpression.getText();
|
75 | ctx.addFailureAt(start, width, message);
|
76 | }
|
77 | }
|
78 | }
|
79 | }
|
80 | }
|
81 | });
|
82 | }
|
83 | }
|
84 | function visitPropertyDeclaration(node) {
|
85 | var initializer = node.initializer;
|
86 | if (tsutils.isIdentifier(node.name)) {
|
87 | var fieldName = 'this.' + node.name.getText();
|
88 | if (initializer === undefined) {
|
89 | fieldInitializations[fieldName] = undefined;
|
90 | }
|
91 | else if (AstUtils_1.AstUtils.isConstant(initializer)) {
|
92 | fieldInitializations[fieldName] = initializer.getText();
|
93 | }
|
94 | }
|
95 | if (initializer !== undefined && AstUtils_1.AstUtils.isUndefined(initializer)) {
|
96 | var start = initializer.getStart();
|
97 | var width = initializer.getWidth();
|
98 | ctx.addFailureAt(start, width, FAILURE_UNDEFINED_INIT + node.name.getText());
|
99 | }
|
100 | }
|
101 | function cb(node) {
|
102 | if (tsutils.isClassDeclaration(node)) {
|
103 | fieldInitializations = {};
|
104 | node.members.forEach(function (member) {
|
105 | if (tsutils.isPropertyDeclaration(member)) {
|
106 | visitPropertyDeclaration(member);
|
107 | }
|
108 | else if (tsutils.isConstructorDeclaration(member)) {
|
109 | visitConstructorDeclaration(member);
|
110 | }
|
111 | });
|
112 | fieldInitializations = {};
|
113 | }
|
114 | }
|
115 | return ts.forEachChild(ctx.sourceFile, cb);
|
116 | }
|
117 |
|
\ | No newline at end of file |