UNPKG

rxjs-tslint-rules

Version:
62 lines (61 loc) 3.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var tsquery_1 = require("@phenomnomnominal/tsquery"); var Lint = require("tslint"); var tsutils = require("tsutils"); 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) { var _this = this; var failures = []; var typeChecker = program.getTypeChecker(); var importIdentifier = undefined; var importDeclarations = tsquery_1.tsquery(sourceFile, "ImportDeclaration:has(StringLiteral[value=\"rxjs\"]),ImportDeclaration:has(StringLiteral[value=\"rxjs/observable/of\"])"); importDeclarations.forEach(function (importDeclaration) { var importSpecifiers = tsquery_1.tsquery(importDeclaration, "ImportSpecifier:has(Identifier[escapedText=\"of\"])"); importSpecifiers.forEach(function (node) { var importSpecifier = node; if (!importSpecifier.propertyName && importSpecifier.name.getText() === "of") { importIdentifier = importSpecifier.name; var fix = Lint.Replacement.replaceFromTo(importIdentifier.getStart(), importIdentifier.getStart() + importIdentifier.getWidth(), "of as just"); failures.push(new Lint.RuleFailure(sourceFile, importIdentifier.getStart(), importIdentifier.getStart() + importIdentifier.getWidth(), Rule.FAILURE_STRING, _this.ruleName, fix)); } }); }); if (importIdentifier) { var ofIdentifiers = tsquery_1.tsquery(sourceFile, "CallExpression Identifier[escapedText=\"of\"]"); ofIdentifiers.forEach(function (ofIdentifier) { var callExpression = ofIdentifier.parent; if (tsutils.isCallExpression(callExpression)) { var expression = callExpression.expression; if (tsutils.isIdentifier(expression)) { var declaration = util_1.findDeclaration(expression, typeChecker); if (declaration === importIdentifier.parent) { var fix = Lint.Replacement.replaceFromTo(expression.getStart(), expression.getStart() + expression.getWidth(), "just"); failures.push(new Lint.RuleFailure(sourceFile, expression.getStart(), expression.getStart() + expression.getWidth(), Rule.FAILURE_STRING, _this.ruleName, fix)); } } } }); } return failures; }; Rule.metadata = { description: "Enforces the use of a `just` alias for `of`.", options: null, optionsDescription: "Not configurable.", requiresTypeInfo: true, ruleName: "rxjs-just", type: "style", typescriptOnly: true }; Rule.FAILURE_STRING = "Use just alias"; return Rule; }(Lint.Rules.TypedRule)); exports.Rule = Rule;