UNPKG

4.58 kBJavaScriptView Raw
1"use strict";
2var __read = (this && this.__read) || function (o, n) {
3 var m = typeof Symbol === "function" && o[Symbol.iterator];
4 if (!m) return o;
5 var i = m.call(o), r, ar = [], e;
6 try {
7 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8 }
9 catch (error) { e = { error: error }; }
10 finally {
11 try {
12 if (r && !r.done && (m = i["return"])) m.call(i);
13 }
14 finally { if (e) throw e.error; }
15 }
16 return ar;
17};
18var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19 if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20 if (ar || !(i in from)) {
21 if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22 ar[i] = from[i];
23 }
24 }
25 return to.concat(ar || Array.prototype.slice.call(from));
26};
27exports.__esModule = true;
28exports.migrateToCreators = void 0;
29var ts = require("typescript");
30var schematics_1 = require("@angular-devkit/schematics");
31var schematics_core_1 = require("../../schematics-core");
32function migrateToCreators() {
33 return function (tree) {
34 (0, schematics_core_1.visitTSSourceFiles)(tree, function (sourceFile) {
35 var effectsPerClass = sourceFile.statements
36 .filter(ts.isClassDeclaration)
37 .map(function (clas) {
38 return clas.members
39 .filter(ts.isPropertyDeclaration)
40 .filter(function (property) {
41 return property.decorators &&
42 property.decorators.some(isEffectDecorator);
43 });
44 });
45 var effects = effectsPerClass.reduce(function (acc, effects) { return acc.concat(effects); }, []);
46 var createEffectsChanges = replaceEffectDecorators(tree, sourceFile, effects);
47 var importChanges = (0, schematics_core_1.replaceImport)(sourceFile, sourceFile.fileName, '@ngrx/effects', 'Effect', 'createEffect');
48 (0, schematics_core_1.commitChanges)(tree, sourceFile.fileName, __spreadArray(__spreadArray([], __read(importChanges), false), __read(createEffectsChanges), false));
49 });
50 };
51}
52exports.migrateToCreators = migrateToCreators;
53function replaceEffectDecorators(host, sourceFile, effects) {
54 var inserts = effects
55 .map(function (effect) {
56 if (!effect.initializer) {
57 return [];
58 }
59 var decorator = (effect.decorators || []).find(isEffectDecorator);
60 if (!decorator) {
61 return [];
62 }
63 if (effect.initializer.getText().includes('createEffect')) {
64 return [];
65 }
66 var effectArguments = getDispatchProperties(host, sourceFile.text, decorator);
67 var end = effectArguments ? ", " + effectArguments + ")" : ')';
68 return [
69 new schematics_core_1.InsertChange(sourceFile.fileName, effect.initializer.pos, ' createEffect(() =>'),
70 new schematics_core_1.InsertChange(sourceFile.fileName, effect.initializer.end, end),
71 ];
72 })
73 .reduce(function (acc, inserts) { return acc.concat(inserts); }, []);
74 var removes = effects
75 .map(function (effect) { return effect.decorators; })
76 .map(function (decorators) {
77 if (!decorators) {
78 return [];
79 }
80 var effectDecorators = decorators.filter(isEffectDecorator);
81 return effectDecorators.map(function (decorator) {
82 return new schematics_core_1.RemoveChange(sourceFile.fileName, decorator.expression.pos - 1, // also get the @ sign
83 decorator.expression.end);
84 });
85 })
86 .reduce(function (acc, removes) { return acc.concat(removes); }, []);
87 return __spreadArray(__spreadArray([], __read(inserts), false), __read(removes), false);
88}
89function isEffectDecorator(decorator) {
90 return (ts.isCallExpression(decorator.expression) &&
91 ts.isIdentifier(decorator.expression.expression) &&
92 decorator.expression.expression.text === 'Effect');
93}
94function getDispatchProperties(host, fileContent, decorator) {
95 if (!decorator.expression || !ts.isCallExpression(decorator.expression)) {
96 return '';
97 }
98 // just copy the effect properties
99 var args = fileContent
100 .substring(decorator.expression.arguments.pos, decorator.expression.arguments.end)
101 .trim();
102 return args;
103}
104function default_1() {
105 return (0, schematics_1.chain)([migrateToCreators()]);
106}
107exports["default"] = default_1;
108//# sourceMappingURL=index.js.map
\No newline at end of file