From a96af30cf8eccdd45b9afe67710ba3c7db05a9f8 Mon Sep 17 00:00:00 2001 From: splincode Date: Thu, 9 Jan 2025 17:48:34 +0300 Subject: [PATCH] chore(schematics): valid migration for anchor --- .../interfaces/replacement-attribute-value.ts | 4 ++- .../utils/templates/replace-attr-values.ts | 25 +++++++++++++++- .../constants/attr-with-values-to-replace.ts | 21 ++++++++++++++ .../v4/tests/schematic-migrate-button.spec.ts | 29 +++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/projects/cdk/schematics/ng-update/interfaces/replacement-attribute-value.ts b/projects/cdk/schematics/ng-update/interfaces/replacement-attribute-value.ts index 0fcf916342b8..41b384feb281 100644 --- a/projects/cdk/schematics/ng-update/interfaces/replacement-attribute-value.ts +++ b/projects/cdk/schematics/ng-update/interfaces/replacement-attribute-value.ts @@ -1,8 +1,10 @@ import type {Element} from 'parse5/dist/tree-adapters/default'; export interface ReplacementAttributeValue { + readonly dropAttr?: boolean; + readonly comment?: string; readonly attrNames: string[]; - readonly newAttrName?: string; + readonly newAttrName?: string | null; readonly valueReplacer: | Array<{ readonly from: string; diff --git a/projects/cdk/schematics/ng-update/utils/templates/replace-attr-values.ts b/projects/cdk/schematics/ng-update/utils/templates/replace-attr-values.ts index 0649427aa8b1..7cc710747082 100644 --- a/projects/cdk/schematics/ng-update/utils/templates/replace-attr-values.ts +++ b/projects/cdk/schematics/ng-update/utils/templates/replace-attr-values.ts @@ -33,6 +33,8 @@ export function replaceAttrValues({ filterFn, newAttrName, directiveModule, + dropAttr, + comment, }) => { const elements = [ ...findElementsWithAttributeOnTag( @@ -60,6 +62,8 @@ export function replaceAttrValues({ valueReplacer.forEach(({from, to}) => { if (value === from) { replaceValue({ + dropAttr, + comment, element, recorder, templateOffset, @@ -71,6 +75,8 @@ export function replaceAttrValues({ }); } else { replaceValue({ + dropAttr, + comment, element, recorder, templateOffset, @@ -93,6 +99,8 @@ export function replaceAttrValues({ } function replaceValue({ + dropAttr, + comment, element, recorder, templateOffset, @@ -100,6 +108,8 @@ function replaceValue({ attrNewName, attrValue, }: { + dropAttr?: boolean; + comment?: string; element: Element; recorder: UpdateRecorder; templateOffset: number; @@ -113,5 +123,18 @@ function replaceValue({ }; recorder.remove(templateOffset + startOffset, endOffset - startOffset); - recorder.insertRight(templateOffset + startOffset, `${attrNewName}="${attrValue}"`); + + if (!dropAttr) { + recorder.insertRight( + templateOffset + startOffset, + attrValue ? `${attrNewName}="${attrValue}"` : attrNewName, + ); + } + + if (comment) { + recorder.insertRight( + templateOffset + (element.sourceCodeLocation?.startTag?.startOffset ?? 0), + `\n`, + ); + } } diff --git a/projects/cdk/schematics/ng-update/v4/steps/constants/attr-with-values-to-replace.ts b/projects/cdk/schematics/ng-update/v4/steps/constants/attr-with-values-to-replace.ts index f3e658af17eb..bb47540099bd 100644 --- a/projects/cdk/schematics/ng-update/v4/steps/constants/attr-with-values-to-replace.ts +++ b/projects/cdk/schematics/ng-update/v4/steps/constants/attr-with-values-to-replace.ts @@ -65,4 +65,25 @@ export const ATTR_WITH_VALUES_TO_REPLACE: ReplacementAttributeValue[] = [ withTagNames: ['tui-data-list-wrapper'], valueReplacer: [{from: 'xs', to: 's'}], }, + { + dropAttr: true, + attrNames: ['href', '[href]'], + withTagNames: ['a'], + filterFn: (el) => + (hasElementAttribute(el, 'tuiButton') || + hasElementAttribute(el, 'tuiIconButton')) && + hasElementAttribute(el, 'disabled'), + valueReplacer: () => '', + }, + { + dropAttr: true, + comment: + 'TODO: (Taiga UI migration): A link cannot have a "disabled" attribute. If you want a disabled appearance, use the tuiAppearanceState directive. See https://taiga-ui.dev/directives/appearance/API?tuiAppearanceState=disabled', + attrNames: ['disabled', '[disabled]'], + withTagNames: ['a'], + filterFn: (el) => + hasElementAttribute(el, 'tuiButton') || + hasElementAttribute(el, 'tuiIconButton'), + valueReplacer: () => '', + }, ]; diff --git a/projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-button.spec.ts b/projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-button.spec.ts index 5471e6ffc3d8..f9ce378cf947 100644 --- a/projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-button.spec.ts +++ b/projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-button.spec.ts @@ -77,6 +77,20 @@ const TEMPLATE_BEFORE = ` [showLoader]="true" href="https://taiga-ui.dev" > + + + + `; const TEMPLATE_AFTER = ` @@ -115,6 +129,21 @@ const TEMPLATE_AFTER = ` [loading]="true" href="https://taiga-ui.dev" > + + + + + `; const INLINE_TEMPLATE_COMPONENT_BEFORE = `