@ngrx/operators
Version:
Shared RxJS Operators for NgRx libraries
139 lines • 7.3 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = migrateTapResponse;
var index_1 = require("../../schematics-core/index");
var visitors_1 = require("../../schematics-core/utility/visitors");
var ts = require("typescript");
function migrateTapResponse() {
return function (tree, context) {
(0, index_1.visitTSSourceFiles)(tree, function (sourceFile) {
var e_1, _a;
var changes = [];
var printer = ts.createPrinter();
var tapResponseIdentifiers = new Set();
var namespaceImportsFromOperators = new Set();
var aliasedTapResponseVariables = new Set();
var importOriginMap = new Map();
// Collect import origins and aliases
ts.forEachChild(sourceFile, function (node) {
var e_2, _a, e_3, _b;
var _c;
if (ts.isImportDeclaration(node) &&
ts.isStringLiteral(node.moduleSpecifier) &&
((_c = node.importClause) === null || _c === void 0 ? void 0 : _c.namedBindings)) {
var moduleName = node.moduleSpecifier.text;
var bindings = node.importClause.namedBindings;
if (ts.isNamedImports(bindings)) {
try {
for (var _d = __values(bindings.elements), _e = _d.next(); !_e.done; _e = _d.next()) {
var element = _e.value;
var importedName = element.name.text;
importOriginMap.set(importedName, moduleName);
if (moduleName === '@ngrx/operators') {
tapResponseIdentifiers.add(importedName);
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
}
finally { if (e_2) throw e_2.error; }
}
}
else if (ts.isNamespaceImport(bindings)) {
if (moduleName === '@ngrx/operators') {
namespaceImportsFromOperators.add(bindings.name.text);
}
}
}
// Track variables assigned to known tapResponse identifiers from @ngrx/operators
if (ts.isVariableStatement(node)) {
try {
for (var _f = __values(node.declarationList.declarations), _g = _f.next(); !_g.done; _g = _f.next()) {
var decl = _g.value;
if (ts.isIdentifier(decl.name) &&
decl.initializer &&
ts.isIdentifier(decl.initializer)) {
var original = decl.initializer.text;
if (tapResponseIdentifiers.has(original) &&
importOriginMap.get(original) === '@ngrx/operators') {
aliasedTapResponseVariables.add(decl.name.text);
}
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
}
finally { if (e_3) throw e_3.error; }
}
}
});
try {
// Combine aliases into the main set
for (var aliasedTapResponseVariables_1 = __values(aliasedTapResponseVariables), aliasedTapResponseVariables_1_1 = aliasedTapResponseVariables_1.next(); !aliasedTapResponseVariables_1_1.done; aliasedTapResponseVariables_1_1 = aliasedTapResponseVariables_1.next()) {
var alias = aliasedTapResponseVariables_1_1.value;
tapResponseIdentifiers.add(alias);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (aliasedTapResponseVariables_1_1 && !aliasedTapResponseVariables_1_1.done && (_a = aliasedTapResponseVariables_1.return)) _a.call(aliasedTapResponseVariables_1);
}
finally { if (e_1) throw e_1.error; }
}
(0, visitors_1.visitCallExpression)(sourceFile, function (node) {
var expression = node.expression, args = node.arguments;
var isTapResponseCall = false;
if (ts.isIdentifier(expression)) {
if (tapResponseIdentifiers.has(expression.text)) {
isTapResponseCall = true;
}
}
else if (ts.isPropertyAccessExpression(expression)) {
var namespace = expression.expression.getText();
var fnName = expression.name.text;
if (fnName === 'tapResponse' &&
namespaceImportsFromOperators.has(namespace)) {
isTapResponseCall = true;
}
}
if (isTapResponseCall &&
(args.length === 2 || args.length === 3) &&
args.every(function (arg) { return ts.isArrowFunction(arg) || ts.isFunctionExpression(arg); })) {
var props = [
ts.factory.createPropertyAssignment('next', args[0]),
ts.factory.createPropertyAssignment('error', args[1]),
];
if (args[2]) {
props.push(ts.factory.createPropertyAssignment('complete', args[2]));
}
var newCall = ts.factory.updateCallExpression(node, expression, node.typeArguments, [ts.factory.createObjectLiteralExpression(props, true)]);
var newText = printer.printNode(ts.EmitHint.Expression, newCall, sourceFile);
changes.push((0, index_1.createReplaceChange)(sourceFile, node, node.getText(), newText));
}
});
if (changes.length) {
(0, index_1.commitChanges)(tree, sourceFile.fileName, changes);
context.logger.info("[ngrx/operators] Migrated deprecated tapResponse in ".concat(sourceFile.fileName));
}
});
};
}
//# sourceMappingURL=index.js.map