UNPKG

@taiga-ui/cdk

Version:

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

117 lines 5.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.migrateCalendarSheetSingle = migrateCalendarSheetSingle; const ng_morph_1 = require("ng-morph"); const add_unique_import_1 = require("../../../../utils/add-unique-import"); const insert_todo_1 = require("../../../../utils/insert-todo"); const push_to_array_property_1 = require("../../../../utils/push-to-array-property"); const elements_1 = require("../../../../utils/templates/elements"); const template_resource_1 = require("../../../../utils/templates/template-resource"); const SELECTORS = ['tui-calendar-sheet', 'tui-mobile-calendar-sheet']; const INPUT = 'single'; const PROVIDER_FN = 'tuiCalendarSheetOptionsProvider'; const TAIGA_CORE = '@taiga-ui/core'; const TODO_MESSAGE = `[single] binding removed: use ${PROVIDER_FN}({rangeMode: boolean}) in the component providers instead`; function migrateCalendarSheetSingle({ resource, recorder, fileSystem, }) { const template = (0, template_resource_1.getTemplateFromTemplateResource)(resource, fileSystem); const templateOffset = (0, template_resource_1.getTemplateOffset)(resource); const elements = (0, elements_1.findElementsWithAttributeOnTag)(template, [`[${INPUT}]`, INPUT], SELECTORS); if (!elements.length) { return; } const found = elements .map((el) => findAttr(el)) .filter((x) => x !== null) .sort((a, b) => b.startOffset - a.startOffset); if (!found.length) { return; } let needsProvider = false; found.forEach(({ attrName, value, startOffset, endOffset, elementStart }) => { const isBinding = attrName === `[${INPUT}]`; const isStaticFalse = checkIsStaticFalse(value); const isDynamic = isBinding && !isStaticFalse && !checkIsStaticTrue(value); recorder.remove(templateOffset + startOffset, endOffset - startOffset); if (isStaticFalse) { needsProvider = true; } else if (isDynamic) { recorder.insertLeft(templateOffset + elementStart, `<!-- ${insert_todo_1.TODO_MARK} ${TODO_MESSAGE} -->\n`); } }); if (!needsProvider) { return; } const [component] = (0, ng_morph_1.getNgComponents)(resource.componentPath); if (!component) { return; } const decorator = component.getDecorator('Component'); if (!decorator) { return; } const [metadata] = decorator.getArguments(); if (!metadata || !ng_morph_1.Node.isObjectLiteralExpression(metadata)) { return; } const existingCall = findExistingProviderCall(metadata); if (existingCall) { const [arg] = existingCall.getArguments(); if (arg && ng_morph_1.Node.isObjectLiteralExpression(arg)) { const rangeModeProperty = arg.getProperty('rangeMode'); if (rangeModeProperty && ng_morph_1.Node.isPropertyAssignment(rangeModeProperty)) { rangeModeProperty.getInitializerOrThrow().replaceWithText('true'); } else if (!rangeModeProperty) { arg.addPropertyAssignment({ name: 'rangeMode', initializer: 'true' }); } } else if (!arg) { existingCall.addArgument('{rangeMode: true}'); } } else { (0, push_to_array_property_1.pushToObjectArrayProperty)(metadata, 'providers', `${PROVIDER_FN}({rangeMode: true})`, { unique: true, forceToArray: true }); (0, add_unique_import_1.addUniqueImport)(component.getSourceFile().getFilePath(), PROVIDER_FN, TAIGA_CORE); } } function findAttr(element) { var _a, _b, _c, _d; const attr = element.attrs.find((a) => a.name === `[${INPUT}]`) || element.attrs.find((a) => a.name === INPUT); if (!attr) { return null; } const loc = (_b = (_a = element.sourceCodeLocation) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b[attr.name]; const elementStart = (_d = (_c = element.sourceCodeLocation) === null || _c === void 0 ? void 0 : _c.startTag) === null || _d === void 0 ? void 0 : _d.startOffset; return !loc || elementStart === undefined ? null : { attrName: attr.name, value: attr.value, startOffset: loc.startOffset, endOffset: loc.endOffset, elementStart, }; } function checkIsStaticFalse(value) { return value === 'false' || value === "'false'" || value === '"false"'; } function checkIsStaticTrue(value) { return value === 'true' || value === "'true'" || value === '"true"' || value === ''; } function findExistingProviderCall(metadata) { const providersProperty = metadata.getProperty('providers'); if (!providersProperty || !ng_morph_1.Node.isPropertyAssignment(providersProperty)) { return null; } const init = providersProperty.getInitializer(); if (!ng_morph_1.Node.isArrayLiteralExpression(init)) { return null; } const call = init .getElements() .find((el) => ng_morph_1.Node.isCallExpression(el) && el.getExpression().getText() === PROVIDER_FN); return call && ng_morph_1.Node.isCallExpression(call) ? call : null; } //# sourceMappingURL=migrate-calendar-sheet-single.js.map