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 AstUtils_1 = require("./utils/AstUtils");
|
19 | var Utils_1 = require("./utils/Utils");
|
20 | var 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.applyWithWalker(new PromiseAnalyzer(sourceFile, this.getOptions()));
|
27 | };
|
28 | Rule.metadata = {
|
29 | ruleName: 'promise-must-complete',
|
30 | type: 'maintainability',
|
31 | description: 'When a Promise instance is created, then either the reject() or resolve() parameter must be ' +
|
32 | 'called on it within all code branches in the scope.',
|
33 | options: null,
|
34 | optionsDescription: '',
|
35 | typescriptOnly: true,
|
36 | issueClass: 'Non-SDL',
|
37 | issueType: 'Error',
|
38 | severity: 'Critical',
|
39 | level: 'Opportunity for Excellence',
|
40 | group: 'Correctness'
|
41 | };
|
42 | Rule.FAILURE_STRING = 'A Promise was found that appears to not have resolve or reject invoked on all code paths';
|
43 | return Rule;
|
44 | }(Lint.Rules.AbstractRule));
|
45 | exports.Rule = Rule;
|
46 | var PromiseAnalyzer = (function (_super) {
|
47 | __extends(PromiseAnalyzer, _super);
|
48 | function PromiseAnalyzer() {
|
49 | return _super !== null && _super.apply(this, arguments) || this;
|
50 | }
|
51 | PromiseAnalyzer.prototype.isPromiseDeclaration = function (node) {
|
52 | if (node.expression.kind === ts.SyntaxKind.Identifier &&
|
53 | node.expression.getText() === 'Promise' &&
|
54 | node.arguments !== undefined &&
|
55 | node.arguments.length > 0) {
|
56 | var firstArg = node.arguments[0];
|
57 | if (firstArg.kind === ts.SyntaxKind.ArrowFunction || firstArg.kind === ts.SyntaxKind.FunctionExpression) {
|
58 | return true;
|
59 | }
|
60 | }
|
61 | return false;
|
62 | };
|
63 | PromiseAnalyzer.prototype.getCompletionIdentifiers = function (declaration) {
|
64 | var result = [];
|
65 | if (declaration.parameters === undefined || declaration.parameters.length === 0) {
|
66 | return result;
|
67 | }
|
68 | var arg1 = declaration.parameters[0];
|
69 | var arg2 = declaration.parameters[1];
|
70 | if (arg1 !== undefined && arg1.name.kind === ts.SyntaxKind.Identifier) {
|
71 | result.push(declaration.parameters[0].name);
|
72 | }
|
73 | if (arg2 !== undefined && arg2.name.kind === ts.SyntaxKind.Identifier) {
|
74 | result.push(declaration.parameters[1].name);
|
75 | }
|
76 | return result;
|
77 | };
|
78 | PromiseAnalyzer.prototype.visitNewExpression = function (node) {
|
79 | if (this.isPromiseDeclaration(node) && node.arguments !== undefined) {
|
80 | var functionArgument = node.arguments[0];
|
81 | var functionBody = functionArgument.body;
|
82 | if (functionBody !== undefined) {
|
83 | var competionIdentifiers = this.getCompletionIdentifiers(functionArgument);
|
84 | this.validatePromiseUsage(node, functionBody, competionIdentifiers);
|
85 | }
|
86 | }
|
87 | _super.prototype.visitNewExpression.call(this, node);
|
88 | };
|
89 | PromiseAnalyzer.prototype.validatePromiseUsage = function (promiseInstantiation, block, completionIdentifiers) {
|
90 | var blockAnalyzer = new PromiseCompletionWalker(this.getSourceFile(), this.getOptions(), completionIdentifiers);
|
91 | blockAnalyzer.visitNode(block);
|
92 | if (!blockAnalyzer.isAlwaysCompleted()) {
|
93 | this.addFailureAt(promiseInstantiation.getStart(), promiseInstantiation.getWidth(), Rule.FAILURE_STRING);
|
94 | }
|
95 | };
|
96 | return PromiseAnalyzer;
|
97 | }(Lint.RuleWalker));
|
98 | var PromiseCompletionWalker = (function (_super) {
|
99 | __extends(PromiseCompletionWalker, _super);
|
100 | function PromiseCompletionWalker(sourceFile, options, completionIdentifiers) {
|
101 | var _this = _super.call(this, sourceFile, options) || this;
|
102 | _this.wasCompleted = false;
|
103 | _this.allBranchesCompleted = true;
|
104 | _this.hasBranches = false;
|
105 | _this.walkerOptions = options;
|
106 | _this.completionIdentifiers = completionIdentifiers;
|
107 | return _this;
|
108 | }
|
109 | PromiseCompletionWalker.prototype.visitNode = function (node) {
|
110 | _super.prototype.visitNode.call(this, node);
|
111 | };
|
112 | PromiseCompletionWalker.prototype.isAlwaysCompleted = function () {
|
113 | if (this.wasCompleted) {
|
114 | return true;
|
115 | }
|
116 | if (!this.hasBranches) {
|
117 | return false;
|
118 | }
|
119 | return this.allBranchesCompleted;
|
120 | };
|
121 | PromiseCompletionWalker.prototype.visitIfStatement = function (node) {
|
122 | this.hasBranches = true;
|
123 | var ifAnalyzer = new PromiseCompletionWalker(this.getSourceFile(), this.walkerOptions, this.completionIdentifiers);
|
124 | var elseAnalyzer = new PromiseCompletionWalker(this.getSourceFile(), this.walkerOptions, this.completionIdentifiers);
|
125 | ifAnalyzer.visitNode(node.thenStatement);
|
126 | if (!ifAnalyzer.isAlwaysCompleted()) {
|
127 | this.allBranchesCompleted = false;
|
128 | }
|
129 | else if (node.elseStatement !== undefined) {
|
130 | elseAnalyzer.visitNode(node.elseStatement);
|
131 | if (!elseAnalyzer.isAlwaysCompleted()) {
|
132 | this.allBranchesCompleted = false;
|
133 | }
|
134 | }
|
135 | };
|
136 | PromiseCompletionWalker.prototype.visitCallExpression = function (node) {
|
137 | var _this = this;
|
138 | if (node.expression.kind === ts.SyntaxKind.Identifier) {
|
139 | if (this.isCompletionIdentifier(node.expression)) {
|
140 | this.wasCompleted = true;
|
141 | return;
|
142 | }
|
143 | }
|
144 | var referenceEscaped = Utils_1.Utils.exists(node.arguments, function (argument) {
|
145 | return _this.isCompletionIdentifier(argument);
|
146 | });
|
147 | if (referenceEscaped) {
|
148 | this.wasCompleted = true;
|
149 | return;
|
150 | }
|
151 | _super.prototype.visitCallExpression.call(this, node);
|
152 | };
|
153 | PromiseCompletionWalker.prototype.visitArrowFunction = function (node) {
|
154 | var nonShadowedIdentifiers = this.getNonShadowedCompletionIdentifiers(node);
|
155 | var analyzer = new PromiseCompletionWalker(this.getSourceFile(), this.walkerOptions, nonShadowedIdentifiers);
|
156 | analyzer.visitNode(node.body);
|
157 | if (analyzer.isAlwaysCompleted()) {
|
158 | this.wasCompleted = true;
|
159 | }
|
160 | };
|
161 | PromiseCompletionWalker.prototype.visitFunctionExpression = function (node) {
|
162 | var nonShadowedIdentifiers = this.getNonShadowedCompletionIdentifiers(node);
|
163 | var analyzer = new PromiseCompletionWalker(this.getSourceFile(), this.walkerOptions, nonShadowedIdentifiers);
|
164 | analyzer.visitNode(node.body);
|
165 | if (analyzer.isAlwaysCompleted()) {
|
166 | this.wasCompleted = true;
|
167 | }
|
168 | };
|
169 | PromiseCompletionWalker.prototype.getNonShadowedCompletionIdentifiers = function (declaration) {
|
170 | var result = [];
|
171 | this.completionIdentifiers.forEach(function (identifier) {
|
172 | var isShadowed = Utils_1.Utils.exists(declaration.parameters, function (parameter) {
|
173 | return AstUtils_1.AstUtils.isSameIdentifer(identifier, parameter.name);
|
174 | });
|
175 | if (!isShadowed) {
|
176 | result.push(identifier);
|
177 | }
|
178 | });
|
179 | return result;
|
180 | };
|
181 | PromiseCompletionWalker.prototype.isCompletionIdentifier = function (sourceIdentifier) {
|
182 | return Utils_1.Utils.exists(this.completionIdentifiers, function (identifier) {
|
183 | return AstUtils_1.AstUtils.isSameIdentifer(sourceIdentifier, identifier);
|
184 | });
|
185 | };
|
186 | return PromiseCompletionWalker;
|
187 | }(Lint.RuleWalker));
|
188 |
|
\ | No newline at end of file |