templateNoNegatedAsyncRule.js 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
  16. if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
  17. return cooked;
  18. };
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. var compiler_1 = require("@angular/compiler");
  21. var rules_1 = require("tslint/lib/rules");
  22. var utils_1 = require("tslint/lib/utils");
  23. var ngWalker_1 = require("./angular/ngWalker");
  24. var recursiveAngularExpressionVisitor_1 = require("./angular/templates/recursiveAngularExpressionVisitor");
  25. var unstrictEqualityOperator = '==';
  26. var isAsyncBinding = function (ast) { return ast instanceof compiler_1.BindingPipe && ast.name === 'async'; };
  27. var Rule = (function (_super) {
  28. __extends(Rule, _super);
  29. function Rule() {
  30. return _super !== null && _super.apply(this, arguments) || this;
  31. }
  32. Rule.prototype.apply = function (sourceFile) {
  33. var walkerConfig = { expressionVisitorCtrl: ExpressionVisitorCtrl };
  34. var walker = new ngWalker_1.NgWalker(sourceFile, this.getOptions(), walkerConfig);
  35. return this.applyWithWalker(walker);
  36. };
  37. Rule.metadata = {
  38. description: 'Ensures that strict equality is used when evaluating negations on async pipe output.',
  39. options: null,
  40. optionsDescription: 'Not configurable.',
  41. rationale: utils_1.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n Angular's async pipes emit null initially, prior to the observable emitting any values, or the promise resolving. This can cause negations, like\n *ngIf=\"!(myConditional | async)\" to thrash the layout and cause expensive side-effects like firing off XHR requests for a component which should not be shown.\n "], ["\n Angular's async pipes emit null initially, prior to the observable emitting any values, or the promise resolving. This can cause negations, like\n *ngIf=\"!(myConditional | async)\" to thrash the layout and cause expensive side-effects like firing off XHR requests for a component which should not be shown.\n "]))),
  42. ruleName: 'template-no-negated-async',
  43. type: 'functionality',
  44. typescriptOnly: true
  45. };
  46. Rule.FAILURE_STRING_NEGATED_PIPE = 'Async pipes should not be negated. Use (observable | async) === (false | null | undefined) to check its value instead';
  47. Rule.FAILURE_STRING_UNSTRICT_EQUALITY = 'Async pipes must use strict equality `===` when comparing with `false`';
  48. return Rule;
  49. }(rules_1.AbstractRule));
  50. exports.Rule = Rule;
  51. var ExpressionVisitorCtrl = (function (_super) {
  52. __extends(ExpressionVisitorCtrl, _super);
  53. function ExpressionVisitorCtrl() {
  54. return _super !== null && _super.apply(this, arguments) || this;
  55. }
  56. ExpressionVisitorCtrl.prototype.visitBinary = function (ast, context) {
  57. this.validateBinary(ast);
  58. _super.prototype.visitBinary.call(this, ast, context);
  59. };
  60. ExpressionVisitorCtrl.prototype.visitPrefixNot = function (ast, context) {
  61. this.validatePrefixNot(ast);
  62. _super.prototype.visitPrefixNot.call(this, ast, context);
  63. };
  64. ExpressionVisitorCtrl.prototype.generateFailure = function (ast, errorMessage) {
  65. var _a = ast.span, spanEnd = _a.end, spanStart = _a.start;
  66. this.addFailureFromStartToEnd(spanStart, spanEnd, errorMessage);
  67. };
  68. ExpressionVisitorCtrl.prototype.validateBinary = function (ast) {
  69. var left = ast.left, operation = ast.operation, right = ast.right;
  70. if (!isAsyncBinding(left) || !(right instanceof compiler_1.LiteralPrimitive) || right.value !== false || operation !== unstrictEqualityOperator) {
  71. return;
  72. }
  73. this.generateFailure(ast, Rule.FAILURE_STRING_UNSTRICT_EQUALITY);
  74. };
  75. ExpressionVisitorCtrl.prototype.validatePrefixNot = function (ast) {
  76. var expression = ast.expression;
  77. if (!isAsyncBinding(expression))
  78. return;
  79. this.generateFailure(ast, Rule.FAILURE_STRING_NEGATED_PIPE);
  80. };
  81. return ExpressionVisitorCtrl;
  82. }(recursiveAngularExpressionVisitor_1.RecursiveAngularExpressionVisitor));
  83. var templateObject_1;