Skip to content

Commit

Permalink
chore(schematics): valid migration for anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Jan 9, 2025
1 parent 3d7263b commit a96af30
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export function replaceAttrValues({
filterFn,
newAttrName,
directiveModule,
dropAttr,
comment,
}) => {
const elements = [
...findElementsWithAttributeOnTag(
Expand Down Expand Up @@ -60,6 +62,8 @@ export function replaceAttrValues({
valueReplacer.forEach(({from, to}) => {
if (value === from) {
replaceValue({
dropAttr,
comment,
element,
recorder,
templateOffset,
Expand All @@ -71,6 +75,8 @@ export function replaceAttrValues({
});
} else {
replaceValue({
dropAttr,
comment,
element,
recorder,
templateOffset,
Expand All @@ -93,13 +99,17 @@ export function replaceAttrValues({
}

function replaceValue({
dropAttr,
comment,
element,
recorder,
templateOffset,
attrName,
attrNewName,
attrValue,
}: {
dropAttr?: boolean;
comment?: string;
element: Element;
recorder: UpdateRecorder;
templateOffset: number;
Expand All @@ -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),
`<!-- ${comment} -->\n`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => '',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ const TEMPLATE_BEFORE = `
[showLoader]="true"
href="https://taiga-ui.dev"
></a>
<button
tuiIconButton
type="button"
[icon]="icon"
[disabled]="true"
></button>
<a
tuiIconButton
[icon]="icon"
[disabled]="true"
href="https://taiga-ui.dev"
></a>
`;

const TEMPLATE_AFTER = `
Expand Down Expand Up @@ -115,6 +129,21 @@ const TEMPLATE_AFTER = `
[loading]="true"
href="https://taiga-ui.dev"
></a>
<button
tuiIconButton
type="button"
[iconStart]="icon"
[disabled]="true"
></button>
<!-- 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 -->
<a
tuiIconButton
[iconStart]="icon"
${''}
${''}
></a>
`;

const INLINE_TEMPLATE_COMPONENT_BEFORE = `
Expand Down

0 comments on commit a96af30

Please sign in to comment.