UNPKG

5.58 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 Lint = require("tslint");
14var tsutils = require("tsutils");
15var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
16var FAILURE_STRING = 'Unnecessary local variable: ';
17var Rule = (function (_super) {
18 __extends(Rule, _super);
19 function Rule() {
20 return _super !== null && _super.apply(this, arguments) || this;
21 }
22 Rule.prototype.apply = function (sourceFile) {
23 return this.applyWithWalker(new UnnecessaryLocalVariableRuleWalker(sourceFile, this.getOptions()));
24 };
25 Rule.metadata = {
26 ruleName: 'no-unnecessary-local-variable',
27 type: 'maintainability',
28 description: 'Do not declare a variable only to return it from the function on the next line.',
29 options: null,
30 optionsDescription: '',
31 typescriptOnly: true,
32 issueClass: 'Non-SDL',
33 issueType: 'Warning',
34 severity: 'Low',
35 level: 'Opportunity for Excellence',
36 group: 'Clarity',
37 commonWeaknessEnumeration: '563, 710'
38 };
39 return Rule;
40}(Lint.Rules.AbstractRule));
41exports.Rule = Rule;
42var UnnecessaryLocalVariableRuleWalker = (function (_super) {
43 __extends(UnnecessaryLocalVariableRuleWalker, _super);
44 function UnnecessaryLocalVariableRuleWalker() {
45 var _this = _super !== null && _super.apply(this, arguments) || this;
46 _this.variableUsages = tsutils.collectVariableUsage(_this.getSourceFile());
47 return _this;
48 }
49 UnnecessaryLocalVariableRuleWalker.prototype.visitBlock = function (node) {
50 this.validateStatementArray(node.statements);
51 _super.prototype.visitBlock.call(this, node);
52 };
53 UnnecessaryLocalVariableRuleWalker.prototype.visitSourceFile = function (node) {
54 this.validateStatementArray(node.statements);
55 _super.prototype.visitSourceFile.call(this, node);
56 };
57 UnnecessaryLocalVariableRuleWalker.prototype.visitCaseClause = function (node) {
58 this.validateStatementArray(node.statements);
59 _super.prototype.visitCaseClause.call(this, node);
60 };
61 UnnecessaryLocalVariableRuleWalker.prototype.visitDefaultClause = function (node) {
62 this.validateStatementArray(node.statements);
63 _super.prototype.visitDefaultClause.call(this, node);
64 };
65 UnnecessaryLocalVariableRuleWalker.prototype.visitModuleDeclaration = function (node) {
66 if (node.body != null && tsutils.isModuleBlock(node.body)) {
67 this.validateStatementArray(node.body.statements);
68 }
69 _super.prototype.visitModuleDeclaration.call(this, node);
70 };
71 UnnecessaryLocalVariableRuleWalker.prototype.validateStatementArray = function (statements) {
72 if (statements == null || statements.length < 2) {
73 return;
74 }
75 var lastStatement = statements[statements.length - 1];
76 var nextToLastStatement = statements[statements.length - 2];
77 var returnedVariableName = this.tryToGetReturnedVariableName(lastStatement);
78 var declaredVariableIdentifier = this.tryToGetDeclaredVariable(nextToLastStatement);
79 if (declaredVariableIdentifier == null) {
80 return;
81 }
82 var declaredVariableName = declaredVariableIdentifier.text;
83 if (returnedVariableName != null
84 && declaredVariableName != null
85 && returnedVariableName === declaredVariableName
86 && this.variableIsOnlyUsedOnce(declaredVariableIdentifier)) {
87 this.addFailureAt(nextToLastStatement.getStart(), nextToLastStatement.getWidth(), FAILURE_STRING + returnedVariableName);
88 }
89 };
90 UnnecessaryLocalVariableRuleWalker.prototype.tryToGetDeclaredVariable = function (statement) {
91 if (statement != null && tsutils.isVariableStatement(statement)) {
92 if (statement.declarationList.declarations.length === 1) {
93 var declaration = statement.declarationList.declarations[0];
94 if (declaration.name != null && tsutils.isIdentifier(declaration.name)) {
95 return declaration.name;
96 }
97 }
98 }
99 return null;
100 };
101 UnnecessaryLocalVariableRuleWalker.prototype.tryToGetReturnedVariableName = function (statement) {
102 if (statement != null && tsutils.isReturnStatement(statement)) {
103 if (statement.expression != null && tsutils.isIdentifier(statement.expression)) {
104 return statement.expression.text;
105 }
106 }
107 return null;
108 };
109 UnnecessaryLocalVariableRuleWalker.prototype.variableIsOnlyUsedOnce = function (declaredVariableIdentifier) {
110 var usage = this.variableUsages.get(declaredVariableIdentifier);
111 return usage !== undefined && usage.uses.length === 1;
112 };
113 return UnnecessaryLocalVariableRuleWalker;
114}(ErrorTolerantWalker_1.ErrorTolerantWalker));
115//# sourceMappingURL=noUnnecessaryLocalVariableRule.js.map
\No newline at end of file