UNPKG

@rx-angular/state

Version:

@rx-angular/state is a light-weight, flexible, strongly typed and tested tool dedicated to reduce the complexity of managing component state and side effects in angular

89 lines (88 loc) 3.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.renamingRule = renamingRule; const schematics_1 = require("@angular-devkit/schematics"); const ng_morph_1 = require("ng-morph"); function renamingRule(packageName, renames) { const getRename = configureRenames(renames); return () => { return (0, schematics_1.chain)([ (tree) => { console.log('Migration schematics might cause your code to be formatted incorrectly. Make sure to run your formatter of choice after the migration.'); (0, ng_morph_1.setActiveProject)((0, ng_morph_1.createProject)(tree, '/', ['**/*.ts'])); const imports = (0, ng_morph_1.getImports)('**/*.ts', { moduleSpecifier: packageName, }); const newImports = new Map(); for (const importDeclaration of imports) { const namedImports = importDeclaration.getNamedImports(); for (const namedImport of namedImports) { const oldName = namedImport.getName(); const rename = getRename(oldName); if (rename == null) { continue; } const filePath = importDeclaration .getSourceFile() .getFilePath() .toString(); const key = `${filePath}__${rename.moduleSpecifier}`; const namedImportConfig = { name: rename.namedImport, }; if (namedImport.getAliasNode()) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion namedImportConfig.alias = namedImport.getAliasNode().getText(); } if (newImports.has(key)) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const value = newImports.get(key); newImports.set(key, [...value, namedImportConfig]); } else { newImports.set(key, [namedImportConfig]); } renameReferences(namedImport, oldName, rename.namedImport); namedImport.remove(); } if (importDeclaration.getNamedImports().length === 0) { importDeclaration.remove(); } } for (const [key, namedImports] of newImports.entries()) { const [filePath, moduleSpecifier] = key.split('__'); (0, ng_morph_1.addImports)(filePath, { namedImports: namedImports, moduleSpecifier: moduleSpecifier, }); } (0, ng_morph_1.saveActiveProject)(); }, ]); }; } function renameReferences(importSpecifier, oldName, newName) { importSpecifier .getNameNode() .findReferencesAsNodes() .forEach((ref) => { if (ref.getText() === oldName) { ref.replaceWithText(newName); } }); } function configureRenames(renames) { return (namedImport) => { if (renames[namedImport] == null) { return null; } return { namedImport: Array.isArray(renames[namedImport]) ? renames[namedImport][0] : namedImport, moduleSpecifier: Array.isArray(renames[namedImport]) ? renames[namedImport][1] : renames[namedImport], }; }; }