UNPKG

@taiga-ui/cdk

Version:

Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance

110 lines 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.migrateDialogLegacySizes = migrateDialogLegacySizes; const ng_morph_1 = require("ng-morph"); const constants_1 = require("../../../constants"); const insert_todo_1 = require("../../../utils/insert-todo"); const is_service_method_call_1 = require("../../../utils/is-service-method-call"); const TAIGA_CORE = '@taiga-ui/core'; const TAIGA_LEGACY = '@taiga-ui/legacy'; const FACTORY_NAME = 'tuiDialog'; const SERVICE_NAME = 'TuiDialogService'; const METHOD_NAME = 'open'; const PROVIDER_NAME = 'tuiDialogOptionsProvider'; const MIGRATE_TOKENS = [FACTORY_NAME, SERVICE_NAME, PROVIDER_NAME]; const LEGACY_SIZE_FULLSCREEN = 'fullscreen'; const LEGACY_SIZES = ['auto', LEGACY_SIZE_FULLSCREEN, 'page']; const TODO_MESSAGE = `legacy dialog size detected (deprecated size: ${LEGACY_SIZES.map((size) => `'${size}'`).join(', ')}); migration only moved imports. Review this call.`; function migrateDialogLegacySizes(_tree, _options) { (0, ng_morph_1.getSourceFiles)(constants_1.ALL_TS_FILES).forEach((sourceFile) => { migrateSourceFile(sourceFile); }); } function migrateSourceFile(sourceFile) { const calls = sourceFile.getDescendantsOfKind(ng_morph_1.SyntaxKind.CallExpression); let hasLegacySize = false; calls.forEach((call) => { if (call.wasForgotten()) { return; } const isFactory = call.getExpression().getText() === FACTORY_NAME; const isProvider = call.getExpression().getText() === PROVIDER_NAME; const isMethod = (0, is_service_method_call_1.isServiceMethodCall)(call, SERVICE_NAME, METHOD_NAME); if (!isFactory && !isProvider && !isMethod) { return; } const options = isProvider ? call.getArguments()[0] : call.getArguments()[1]; if (!options || !ng_morph_1.Node.isObjectLiteralExpression(options)) { return; } const sizeProperty = options.getProperty('size'); if (!sizeProperty || !ng_morph_1.Node.isPropertyAssignment(sizeProperty)) { return; } const init = sizeProperty.getInitializer(); if (!init || (!ng_morph_1.Node.isStringLiteral(init) && !ng_morph_1.Node.isNoSubstitutionTemplateLiteral(init)) || !LEGACY_SIZES.includes(init.getLiteralValue())) { return; } const appearanceProperty = options.getProperty('appearance'); if (init.getLiteralValue() === LEGACY_SIZE_FULLSCREEN) { if (!appearanceProperty) { sizeProperty.replaceWithText(`appearance: '${LEGACY_SIZE_FULLSCREEN}'`); return; } if (ng_morph_1.Node.isPropertyAssignment(appearanceProperty)) { const appearanceInitializer = appearanceProperty.getInitializer(); if (appearanceInitializer && (ng_morph_1.Node.isStringLiteral(appearanceInitializer) || ng_morph_1.Node.isNoSubstitutionTemplateLiteral(appearanceInitializer)) && appearanceInitializer.getLiteralValue() === LEGACY_SIZE_FULLSCREEN) { sizeProperty.remove(); return; } } } hasLegacySize = true; (0, insert_todo_1.insertTodo)(sizeProperty, TODO_MESSAGE); }); if (hasLegacySize) { moveDialogImportsToLegacy(sourceFile); } } function moveDialogImportsToLegacy(sourceFile) { const moveToLegacy = []; sourceFile.getImportDeclarations().forEach((decl) => { if (!decl.getModuleSpecifierValue().startsWith(TAIGA_CORE)) { return; } decl.getNamedImports().forEach((spec) => { const name = spec.getName(); if (MIGRATE_TOKENS.includes(name)) { moveToLegacy.push(name); spec.remove(); } }); if (decl.getNamedImports().length === 0 && !decl.getDefaultImport() && !decl.getNamespaceImport()) { decl.remove(); } }); if (moveToLegacy.length === 0) { return; } let legacyDecl = sourceFile .getImportDeclarations() .find((d) => d.getModuleSpecifierValue() === TAIGA_LEGACY); if (!legacyDecl) { legacyDecl = sourceFile.addImportDeclaration({ moduleSpecifier: TAIGA_LEGACY, namedImports: [], }); } moveToLegacy.forEach((name) => { legacyDecl.addNamedImport({ name }); }); } //# sourceMappingURL=migrate-dialog-legacy-sizes.js.map