From cf04d1429daa5028820a1e82889f9353b7273ce3 Mon Sep 17 00:00:00 2001 From: Vladimir Potekhin <46284632+vladimirpotekhin@users.noreply.github.com> Date: Tue, 11 Apr 2023 13:43:33 +0300 Subject: [PATCH] fix(cdk): `schematics` fix incorrectly processing `setNativeFocused` (#4171) --- .../v3/constants/deprecated-functions.ts | 5 ---- projects/cdk/schematics/ng-update/v3/index.ts | 4 ++- .../ng-update/v3/steps/replace-functions.ts | 25 +++++++++++++++++++ ...matic-replace-deprecated-functions.spec.ts | 7 +----- .../tests/schematic-replace-functions.spec.ts | 14 ++++++++++- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/projects/cdk/schematics/ng-update/v3/constants/deprecated-functions.ts b/projects/cdk/schematics/ng-update/v3/constants/deprecated-functions.ts index e7f764b75e22..513127cd614f 100644 --- a/projects/cdk/schematics/ng-update/v3/constants/deprecated-functions.ts +++ b/projects/cdk/schematics/ng-update/v3/constants/deprecated-functions.ts @@ -356,11 +356,6 @@ export const DEPRECATED_FUNCTIONS: readonly TypeToRename[] = [ to: `tuiMoveFocus`, moduleSpecifier: [`@taiga-ui/cdk`], }, - { - from: `setNativeFocused`, - to: `tuiSetNativeFocused`, - moduleSpecifier: [`@taiga-ui/cdk`], - }, { from: `setNativeMouseFocused`, to: `tuiSetNativeMouseFocused`, diff --git a/projects/cdk/schematics/ng-update/v3/index.ts b/projects/cdk/schematics/ng-update/v3/index.ts index 8361da71cfac..d259972ea2f8 100644 --- a/projects/cdk/schematics/ng-update/v3/index.ts +++ b/projects/cdk/schematics/ng-update/v3/index.ts @@ -109,7 +109,9 @@ function main(options: TuiSchema): Rule { function addTaigaStyles(options: TuiSchema): Rule { return async (tree: Tree, context) => { - const taigaStyles = [TAIGA_THEME_FONTS]; + const proprietary = getPackageJsonDependency(tree, `@taiga-ui/proprietary-core`); + + const taigaStyles = proprietary ? [] : [TAIGA_THEME_FONTS]; const stylesToReplace = { from: TAIGA_GLOBAL_OLD_STYLE, to: [TAIGA_GLOBAL_NEW_STYLE], diff --git a/projects/cdk/schematics/ng-update/v3/steps/replace-functions.ts b/projects/cdk/schematics/ng-update/v3/steps/replace-functions.ts index c8c6a00c0d29..a0eaee5a0400 100644 --- a/projects/cdk/schematics/ng-update/v3/steps/replace-functions.ts +++ b/projects/cdk/schematics/ng-update/v3/steps/replace-functions.ts @@ -20,6 +20,10 @@ export function replaceFunctions(options: TuiSchema): void { replaceFallbackValue(getNamedImportReferences(`fallbackValue`, `@taiga-ui/cdk`)); replaceCustomEvent(getNamedImportReferences(`tuiCustomEvent`, `@taiga-ui/cdk`)); replaceClosestElement(getNamedImportReferences(`getClosestElement`, `@taiga-ui/cdk`)); + replaceNativeFocused([ + ...getNamedImportReferences(`tuiSetNativeFocused`, `@taiga-ui/cdk`), + ...getNamedImportReferences(`setNativeFocused`, `@taiga-ui/cdk`), + ]); replaceDeprecatedFunction(); modifyFormatNumberArgs(); modifyClosestFocusable(); @@ -67,6 +71,27 @@ function replacePadStart(references: Node[]): void { }); } +function replaceNativeFocused(references: Node[]): void { + references.forEach(ref => { + const parent = ref.getParent(); + + if (Node.isImportSpecifier(parent)) { + removeImport(parent); + } else if (Node.isCallExpression(parent)) { + const [targetString, focusedArg, preventScroll] = parent.getArguments(); + + const setFocused = !focusedArg || focusedArg.getText() === `true`; + + const focus = `${targetString.getText()}.focus(${ + preventScroll?.getText() ? `{preventScroll: true}` : `` + })`; + const blur = `${targetString.getText()}.blur()`; + + parent.replaceWithText(setFocused ? focus : blur); + } + }); +} + function replaceClosestElement(references: Node[]): void { references.forEach(ref => { const parent = ref.getParent(); diff --git a/projects/cdk/schematics/ng-update/v3/tests/schematic-replace-deprecated-functions.spec.ts b/projects/cdk/schematics/ng-update/v3/tests/schematic-replace-deprecated-functions.spec.ts index e1d7747b08e2..a6a51d325764 100644 --- a/projects/cdk/schematics/ng-update/v3/tests/schematic-replace-deprecated-functions.spec.ts +++ b/projects/cdk/schematics/ng-update/v3/tests/schematic-replace-deprecated-functions.spec.ts @@ -94,7 +94,6 @@ import { isNativeKeyboardFocusable, isNativeMouseFocusable, moveFocus, - setNativeFocused, setNativeMouseFocused, px, clamp, @@ -241,7 +240,6 @@ isNativeFocused(...args); isNativeKeyboardFocusable(...args); isNativeMouseFocusable(...args); moveFocus(...args); -setNativeFocused(...args); setNativeMouseFocused(...args); px(...args); clamp(...args); @@ -384,7 +382,6 @@ class Component { isNativeKeyboardFocusable(); isNativeMouseFocusable(); moveFocus(); - setNativeFocused(); setNativeMouseFocused(); px(); clamp(); @@ -488,7 +485,7 @@ import { tuiToGradient, } from '@taiga-ui/addon-editor'; import {tuiDefaultSort} from '@taiga-ui/addon-table'; -import { tuiMockCurrentDate, tuiRestoreRealDate, tuiMockDateInside, tuiPendingIfNotMoscowTimeZone, tuiDragAndDropFrom, tuiFocusVisibleObservable, tuiQueryListChanges, tuiMouseDragFinishFrom, tuiMustBePresent, tuiPressedObservable, tuiPreventDefault, tuiStopPropagation, tuiTypedFromEvent, tuiWatch, tuiIsEdgeOlderThan, tuiIsEdge, tuiIsFirefox, tuiIsIE, tuiIsSafari, tuiCanScroll, tuiContainsOrAfter, tuiGetActualTarget, tuiGetClipboardDataText, tuiGetDocumentOrShadowRoot, tuiGetElementObscures, tuiGetElementOffset, tuiGetScrollParent, tuiIsCurrentTarget, tuiIsInsideIframe, tuiIsNodeIn, tuiBlurNativeFocused, tuiGetNativeFocused, tuiIsNativeFocusedIn, tuiIsNativeFocused, tuiIsNativeKeyboardFocusable, tuiIsNativeMouseFocusable, tuiMoveFocus, tuiSetNativeFocused, tuiSetNativeMouseFocused, tuiPx, tuiClamp, tuiInRange, tuiNormalizeToIntNumber, tuiQuantize, tuiRound, tuiCeil, tuiFloor, tuiSum, tuiToInt, tuiToRadians, tuiDistanceBetweenTouches, tuiEaseInOutQuad, tuiFlatLength, tuiGetOriginalArrayFromQueryList, tuiGetSwipeDirection, tuiIsElementEditable, tuiIsNumber, tuiIsPresent, tuiMarkControlAsTouchedAndValidate, tuiNullableSame, tuiUniqBy, tuiIsApplePlatform, tuiIsApple, tuiIsIos, tuiSvgLinearGradientProcessor, tuiGetClosestFocusable, tuiHexToRgb, tuiHsvToRgb, tuiParseColor, tuiRgbToHex, tuiRgbToHsv } from '@taiga-ui/cdk'; +import { tuiMockCurrentDate, tuiRestoreRealDate, tuiMockDateInside, tuiPendingIfNotMoscowTimeZone, tuiDragAndDropFrom, tuiFocusVisibleObservable, tuiQueryListChanges, tuiMouseDragFinishFrom, tuiMustBePresent, tuiPressedObservable, tuiPreventDefault, tuiStopPropagation, tuiTypedFromEvent, tuiWatch, tuiIsEdgeOlderThan, tuiIsEdge, tuiIsFirefox, tuiIsIE, tuiIsSafari, tuiCanScroll, tuiContainsOrAfter, tuiGetActualTarget, tuiGetClipboardDataText, tuiGetDocumentOrShadowRoot, tuiGetElementObscures, tuiGetElementOffset, tuiGetScrollParent, tuiIsCurrentTarget, tuiIsInsideIframe, tuiIsNodeIn, tuiBlurNativeFocused, tuiGetNativeFocused, tuiIsNativeFocusedIn, tuiIsNativeFocused, tuiIsNativeKeyboardFocusable, tuiIsNativeMouseFocusable, tuiMoveFocus, tuiSetNativeMouseFocused, tuiPx, tuiClamp, tuiInRange, tuiNormalizeToIntNumber, tuiQuantize, tuiRound, tuiCeil, tuiFloor, tuiSum, tuiToInt, tuiToRadians, tuiDistanceBetweenTouches, tuiEaseInOutQuad, tuiFlatLength, tuiGetOriginalArrayFromQueryList, tuiGetSwipeDirection, tuiIsElementEditable, tuiIsNumber, tuiIsPresent, tuiMarkControlAsTouchedAndValidate, tuiNullableSame, tuiUniqBy, tuiIsApplePlatform, tuiIsApple, tuiIsIos, tuiSvgLinearGradientProcessor, tuiGetClosestFocusable, tuiHexToRgb, tuiHsvToRgb, tuiParseColor, tuiRgbToHex, tuiRgbToHsv } from '@taiga-ui/cdk'; import { tuiTextfieldWatchedControllerFactory, tuiSmartSearch, @@ -607,7 +604,6 @@ tuiIsNativeFocused(...args); tuiIsNativeKeyboardFocusable(...args); tuiIsNativeMouseFocusable(...args); tuiMoveFocus(...args); -tuiSetNativeFocused(...args); tuiSetNativeMouseFocused(...args); tuiPx(...args); tuiClamp(...args); @@ -750,7 +746,6 @@ class Component { tuiIsNativeKeyboardFocusable(); tuiIsNativeMouseFocusable(); tuiMoveFocus(); - tuiSetNativeFocused(); tuiSetNativeMouseFocused(); tuiPx(); tuiClamp(); diff --git a/projects/cdk/schematics/ng-update/v3/tests/schematic-replace-functions.spec.ts b/projects/cdk/schematics/ng-update/v3/tests/schematic-replace-functions.spec.ts index d567f0489953..02e79efe77ee 100644 --- a/projects/cdk/schematics/ng-update/v3/tests/schematic-replace-functions.spec.ts +++ b/projects/cdk/schematics/ng-update/v3/tests/schematic-replace-functions.spec.ts @@ -23,6 +23,12 @@ import { TuiNumberFormatSettings, } from '@taiga-ui/core'; +element.focus(); +element.focus(); +element.focus(); +element.blur(); +element.focus({preventScroll: true}); + tuiFormatNumber(1000); tuiFormatNumber(1.234, {decimalLimit: 2, decimalSeparator: ',', thousandSeparator: ' ', zeroPadding: true}); tuiFormatNumber(123.45, {decimalLimit: 3, decimalSeparator: '.', thousandSeparator: ' ', zeroPadding: true}); @@ -82,13 +88,19 @@ const event = new CustomEvent("hello", { `; const BEFORE = `import {Component} from '@angular/core'; -import { fallbackValue, tuiCustomEvent, getClosestElement, padStart, tuiGetClosestFocusable } from '@taiga-ui/cdk'; +import { fallbackValue, tuiCustomEvent, getClosestElement, padStart, tuiGetClosestFocusable, tuiSetNativeFocused, setNativeFocused } from '@taiga-ui/cdk'; import { TUI_NUMBER_FORMAT, tuiFormatNumber, TuiNumberFormatSettings, } from '@taiga-ui/core'; +setNativeFocused(element); +tuiSetNativeFocused(element); +tuiSetNativeFocused(element, true); +tuiSetNativeFocused(element, false); +tuiSetNativeFocused(element, true, true); + tuiFormatNumber(1000); tuiFormatNumber(1.234, 2); tuiFormatNumber(123.45, 3, '.');