UNPKG

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