|
| 1 | +// Original author Bowen Ni |
| 2 | +// Modifications mgechev. |
| 3 | + |
| 4 | +import * as Lint from 'tslint'; |
| 5 | +import * as tsutils from 'tsutils'; |
| 6 | +import * as ts from 'typescript'; |
| 7 | +import { subtractSets, concatSets, isObservable, returnsObservable, computeInsertionIndexForImports } from './utils'; |
| 8 | +/** |
| 9 | + * A typed TSLint rule that inspects observable chains using patched RxJs |
| 10 | + * operators and turns them into a pipeable operator chain. |
| 11 | + */ |
| 12 | +export class Rule extends Lint.Rules.TypedRule { |
| 13 | + static metadata: Lint.IRuleMetadata = { |
| 14 | + ruleName: 'migrate-static-observable-methods', |
| 15 | + description: 'Updates the static methods of the Observable class.', |
| 16 | + optionsDescription: '', |
| 17 | + options: null, |
| 18 | + typescriptOnly: true, |
| 19 | + type: 'functionality' |
| 20 | + }; |
| 21 | + static IMPORT_FAILURE_STRING = 'prefer operator imports with no side-effects'; |
| 22 | + static OBSERVABLE_FAILURE_STRING = 'prefer function calls'; |
| 23 | + |
| 24 | + applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { |
| 25 | + const failure = this.applyWithFunction(sourceFile, ctx => this.walk(ctx, program)); |
| 26 | + return failure; |
| 27 | + } |
| 28 | + private walk(ctx: Lint.WalkContext<void>, program: ts.Program) { |
| 29 | + this.removePatchedOperatorImports(ctx); |
| 30 | + const sourceFile = ctx.sourceFile; |
| 31 | + const typeChecker = program.getTypeChecker(); |
| 32 | + const insertionStart = computeInsertionIndexForImports(sourceFile); |
| 33 | + let rxjsOperatorImports = findImportedRxjsOperators(sourceFile); |
| 34 | + |
| 35 | + function checkPatchableOperatorUsage(node: ts.Node) { |
| 36 | + if (!isRxjsStaticOperatorCallExpression(node, typeChecker)) { |
| 37 | + return ts.forEachChild(node, checkPatchableOperatorUsage); |
| 38 | + } |
| 39 | + |
| 40 | + const callExpr = node as ts.CallExpression; |
| 41 | + if (!tsutils.isPropertyAccessExpression(callExpr.expression)) { |
| 42 | + return ts.forEachChild(node, checkPatchableOperatorUsage); |
| 43 | + } |
| 44 | + |
| 45 | + const propAccess = callExpr.expression as ts.PropertyAccessExpression; |
| 46 | + const name = propAccess.name.getText(sourceFile); |
| 47 | + const operatorName = OPERATOR_RENAMES[name] || name; |
| 48 | + const start = propAccess.getStart(sourceFile); |
| 49 | + const end = propAccess.getEnd(); |
| 50 | + const operatorsToImport = new Set<string>([operatorName]); |
| 51 | + const operatorsToAdd = subtractSets(operatorsToImport, rxjsOperatorImports); |
| 52 | + const imports = createImportReplacements(operatorsToAdd, insertionStart); |
| 53 | + rxjsOperatorImports = concatSets(rxjsOperatorImports, operatorsToAdd); |
| 54 | + ctx.addFailure( |
| 55 | + start, |
| 56 | + end, |
| 57 | + Rule.OBSERVABLE_FAILURE_STRING, |
| 58 | + [Lint.Replacement.replaceFromTo(start, end, operatorAlias(operatorName))].concat(imports) |
| 59 | + ); |
| 60 | + return ts.forEachChild(node, checkPatchableOperatorUsage); |
| 61 | + } |
| 62 | + |
| 63 | + return ts.forEachChild(ctx.sourceFile, checkPatchableOperatorUsage); |
| 64 | + } |
| 65 | + |
| 66 | + private removePatchedOperatorImports(ctx: Lint.WalkContext<void>): void { |
| 67 | + const sourceFile = ctx.sourceFile; |
| 68 | + for (const importStatement of sourceFile.statements.filter(tsutils.isImportDeclaration)) { |
| 69 | + const moduleSpecifier = importStatement.moduleSpecifier.getText(); |
| 70 | + if (!moduleSpecifier.startsWith(`'rxjs/add/observable/`)) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + const importStatementStart = importStatement.getStart(sourceFile); |
| 74 | + const importStatementEnd = importStatement.getEnd(); |
| 75 | + ctx.addFailure( |
| 76 | + importStatementStart, |
| 77 | + importStatementEnd, |
| 78 | + Rule.IMPORT_FAILURE_STRING, |
| 79 | + Lint.Replacement.deleteFromTo(importStatementStart, importStatementEnd) |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +function isRxjsStaticOperator(node: ts.PropertyAccessExpression) { |
| 86 | + return 'Observable' === node.expression.getText() && RXJS_OPERATORS.has(node.name.getText()); |
| 87 | +} |
| 88 | + |
| 89 | +function isRxjsStaticOperatorCallExpression(node: ts.Node, typeChecker: ts.TypeChecker) { |
| 90 | + // Expression is of the form fn() |
| 91 | + if (!tsutils.isCallExpression(node)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + // Expression is of the form foo.fn |
| 95 | + if (!tsutils.isPropertyAccessExpression(node.expression)) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + // fn is one of RxJs instance operators |
| 99 | + if (!isRxjsStaticOperator(node.expression)) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + // fn(): k. Checks if k is an observable. Required to distinguish between |
| 103 | + // array operators with same name as RxJs operators. |
| 104 | + if (!returnsObservable(node, typeChecker)) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +function findImportedRxjsOperators(sourceFile: ts.SourceFile): Set<string> { |
| 111 | + return new Set<string>( |
| 112 | + sourceFile.statements.filter(tsutils.isImportDeclaration).reduce((current, decl) => { |
| 113 | + if (!decl.importClause) { |
| 114 | + return current; |
| 115 | + } |
| 116 | + if (!decl.moduleSpecifier.getText().startsWith(`'rxjs'`)) { |
| 117 | + return current; |
| 118 | + } |
| 119 | + if (!decl.importClause.namedBindings) { |
| 120 | + return current; |
| 121 | + } |
| 122 | + const bindings = decl.importClause.namedBindings; |
| 123 | + if (ts.isNamedImports(bindings)) { |
| 124 | + return [ |
| 125 | + ...current, |
| 126 | + ...(Array.from(bindings.elements) || []).map(element => { |
| 127 | + return element.name.getText(); |
| 128 | + }) |
| 129 | + ]; |
| 130 | + } |
| 131 | + return current; |
| 132 | + }, []) |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +function operatorAlias(operator: string) { |
| 137 | + return 'observable' + operator[0].toUpperCase() + operator.substring(1, operator.length); |
| 138 | +} |
| 139 | + |
| 140 | +function createImportReplacements(operatorsToAdd: Set<string>, startIndex: number): Lint.Replacement[] { |
| 141 | + return [...Array.from(operatorsToAdd.values())].map(operator => |
| 142 | + Lint.Replacement.appendText(startIndex, `\nimport {${operator} as ${operatorAlias(operator)}} from 'rxjs';\n`) |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +/* |
| 147 | + * https://github.com/ReactiveX/rxjs/tree/master/compat/add/observable |
| 148 | + */ |
| 149 | +const RXJS_OPERATORS = new Set([ |
| 150 | + 'bindCallback', |
| 151 | + 'bindNodeCallback', |
| 152 | + 'combineLatest', |
| 153 | + 'concat', |
| 154 | + 'defer', |
| 155 | + 'empty', |
| 156 | + 'forkJoin', |
| 157 | + 'from', |
| 158 | + 'fromEvent', |
| 159 | + 'fromEventPattern', |
| 160 | + 'fromPromise', |
| 161 | + 'generate', |
| 162 | + 'if', |
| 163 | + 'interval', |
| 164 | + 'merge', |
| 165 | + 'never', |
| 166 | + 'of', |
| 167 | + 'onErrorResumeNext', |
| 168 | + 'pairs', |
| 169 | + 'rase', |
| 170 | + 'range', |
| 171 | + 'throw', |
| 172 | + 'timer', |
| 173 | + 'using', |
| 174 | + 'zip' |
| 175 | +]); |
| 176 | + |
| 177 | +// Not handling NEVER |
| 178 | +const OPERATOR_RENAMES: { [key: string]: string } = { |
| 179 | + throw: 'throwError', |
| 180 | + if: 'iif', |
| 181 | + fromPromise: 'from' |
| 182 | +}; |
0 commit comments