awaitPromiseRule.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2017 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var tsutils_1 = require("tsutils");
  21. var ts = require("typescript");
  22. var Lint = require("../index");
  23. var Rule = /** @class */ (function (_super) {
  24. tslib_1.__extends(Rule, _super);
  25. function Rule() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. Rule.prototype.applyWithProgram = function (sourceFile, program) {
  29. var promiseTypes = new Set(["Promise"].concat(this.ruleArguments));
  30. return this.applyWithFunction(sourceFile, walk, promiseTypes, program.getTypeChecker());
  31. };
  32. /* tslint:disable:object-literal-sort-keys */
  33. Rule.metadata = {
  34. ruleName: "await-promise",
  35. description: "Warns for an awaited value that is not a Promise.",
  36. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n A list of 'string' names of any additional classes that should also be treated as Promises.\n For example, if you are using a class called 'Future' that implements the Thenable interface,\n you might tell the rule to consider type references with the name 'Future' as valid Promise-like\n types. Note that this rule doesn't check for type assignability or compatibility; it just checks\n type reference names.\n "], ["\n A list of 'string' names of any additional classes that should also be treated as Promises.\n For example, if you are using a class called 'Future' that implements the Thenable interface,\n you might tell the rule to consider type references with the name 'Future' as valid Promise-like\n types. Note that this rule doesn't check for type assignability or compatibility; it just checks\n type reference names.\n "]))),
  37. options: {
  38. type: "list",
  39. listType: {
  40. type: "array",
  41. items: { type: "string" },
  42. },
  43. },
  44. optionExamples: [true, [true, "Thenable"]],
  45. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n While it is valid JavaScript to await a non-Promise-like value (it will resolve immediately),\n this pattern is often a programmer error and the resulting semantics can be unintuitive.\n\n Awaiting non-Promise-like values often is an indication of programmer error, such as\n forgetting to add parenthesis to call a function that returns a Promise.\n "], ["\n While it is valid JavaScript to await a non-Promise-like value (it will resolve immediately),\n this pattern is often a programmer error and the resulting semantics can be unintuitive.\n\n Awaiting non-Promise-like values often is an indication of programmer error, such as\n forgetting to add parenthesis to call a function that returns a Promise.\n "]))),
  46. type: "functionality",
  47. typescriptOnly: true,
  48. requiresTypeInfo: true,
  49. };
  50. /* tslint:enable:object-literal-sort-keys */
  51. Rule.FAILURE_STRING = "Invalid 'await' of a non-Promise value.";
  52. Rule.FAILURE_FOR_AWAIT_OF = "Invalid 'for-await-of' of a non-AsyncIterable value.";
  53. return Rule;
  54. }(Lint.Rules.TypedRule));
  55. exports.Rule = Rule;
  56. function walk(ctx, tc) {
  57. var promiseTypes = ctx.options;
  58. return ts.forEachChild(ctx.sourceFile, cb);
  59. function cb(node) {
  60. if (tsutils_1.isAwaitExpression(node) && !containsType(tc.getTypeAtLocation(node.expression), isPromiseType)) {
  61. ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
  62. }
  63. else if (tsutils_1.isForOfStatement(node) && node.awaitModifier !== undefined &&
  64. !containsType(tc.getTypeAtLocation(node.expression), isAsyncIterable)) {
  65. ctx.addFailureAtNode(node.expression, Rule.FAILURE_FOR_AWAIT_OF);
  66. }
  67. return ts.forEachChild(node, cb);
  68. }
  69. function isPromiseType(name) {
  70. return promiseTypes.has(name);
  71. }
  72. }
  73. function containsType(type, predicate) {
  74. if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Any)) {
  75. return true;
  76. }
  77. if (tsutils_1.isTypeReference(type)) {
  78. type = type.target;
  79. }
  80. if (type.symbol !== undefined && predicate(type.symbol.name)) {
  81. return true;
  82. }
  83. if (tsutils_1.isUnionOrIntersectionType(type)) {
  84. return type.types.some(function (t) { return containsType(t, predicate); });
  85. }
  86. var bases = type.getBaseTypes();
  87. return bases !== undefined && bases.some(function (t) { return containsType(t, predicate); });
  88. }
  89. function isAsyncIterable(name) {
  90. return name === "AsyncIterable" || name === "AsyncIterableIterator";
  91. }
  92. var templateObject_1, templateObject_2;