UNPKG

rxjs-tslint-rules

Version:
87 lines (86 loc) 3.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Rule = void 0; var tslib_1 = require("tslib"); var Lint = tslib_1.__importStar(require("tslint")); var tsutils = tslib_1.__importStar(require("tsutils")); var ts = tslib_1.__importStar(require("typescript")); var scope_walker_1 = require("../support/scope-walker"); var util_1 = require("../support/util"); var Rule = (function (_super) { tslib_1.__extends(Rule, _super); function Rule() { return _super !== null && _super.apply(this, arguments) || this; } Rule.prototype.applyWithProgram = function (sourceFile, program) { return this.applyWithWalker(new Walker(sourceFile, this.getOptions(), program)); }; Rule.metadata = { deprecationMessage: "Use rxjs-no-ignored-takewhile-value instead.", description: "Disallows the use of variables/properties from outer scopes in takeWhile.", options: null, optionsDescription: "Not configurable.", requiresTypeInfo: true, ruleName: "rxjs-no-unsafe-takewhile", type: "functionality", typescriptOnly: true, }; Rule.FAILURE_STRING = "Outer scopes are forbidden in takeWhile"; return Rule; }(Lint.Rules.TypedRule)); exports.Rule = Rule; var Walker = (function (_super) { tslib_1.__extends(Walker, _super); function Walker() { return _super !== null && _super.apply(this, arguments) || this; } Walker.prototype.visitNode = function (node) { if (this.callbackStack.length) { var validateNode = tsutils.isIdentifier(node) || util_1.isThis(node); if (validateNode && this.isUnsafe(node)) { this.addFailureAtNode(node, Rule.FAILURE_STRING); } } _super.prototype.visitNode.call(this, node); }; Walker.prototype.isUnsafe = function (node) { var callbackStack = this.callbackStack; var rootCallback = callbackStack[0]; var typeChecker = this.getTypeChecker(); var symbol = typeChecker.getSymbolAtLocation(node); if (!symbol) { return false; } var _a = tslib_1.__read(symbol.getDeclarations(), 1), declaration = _a[0]; if (declaration.pos >= rootCallback.pos && declaration.pos < rootCallback.end) { return false; } if (tsutils.isCallExpression(node.parent)) { return false; } if (tsutils.isNewExpression(node.parent)) { return false; } if (tsutils.isPropertyAccessExpression(node.parent)) { if (node === node.parent.name) { return false; } else if (tsutils.isCallExpression(node.parent.parent)) { return false; } var type = typeChecker.getTypeAtLocation(node.parent.name); if ((type.flags & ts.TypeFlags.EnumLiteral) !== 0) { return false; } } if (util_1.isConstDeclaration(declaration)) { return false; } if (tsutils.isImportSpecifier(declaration)) { return false; } return true; }; return Walker; }(scope_walker_1.ScopeWalker));