Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schematics): ng update for TuiMapper and TuiMatcher #5679

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
EMPTY_QUERY,
TuiContextWithImplicit,
TuiIdService,
TuiMapper,
tuiPure,
tuiSum,
TuiTypedMapper,
} from '@taiga-ui/cdk';
import {
TuiHintHoverDirective,
Expand Down Expand Up @@ -71,9 +71,11 @@ export class TuiBarChartComponent {
return this.max || this.getMax(this.value, this.collapsed);
}

readonly percentMapper: TuiTypedMapper<[readonly number[], boolean, number], number> =
(set, collapsed, max) =>
(100 * (collapsed ? tuiSum(...set) : Math.max(...set))) / max;
readonly percentMapper: TuiMapper<[readonly number[], boolean, number], number> = (
set,
collapsed,
max,
) => (100 * (collapsed ? tuiSum(...set) : Math.max(...set))) / max;

getHintId(index: number): string {
return `${this.autoIdString}_${index}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
EMPTY_QUERY,
TuiDestroyService,
tuiHexToRgb,
TuiMatcher,
tuiQueryListChanges,
TuiTypedMatcher,
tuiWatch,
} from '@taiga-ui/cdk';
import {merge} from 'rxjs';
Expand Down Expand Up @@ -88,7 +88,7 @@ export class TuiDocDocumentationComponent implements AfterContentInit {
return this.isAPI ? this.texts[0] : this.texts[1];
}

matcher: TuiTypedMatcher<
matcher: TuiMatcher<
[TuiDocDocumentationPropertyConnectorDirective<any>, Set<string>]
> = (item, exclusions) => !exclusions.has(item.documentationPropertyName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import {
TuiDayRange,
TuiDestroyService,
TuiInjectionTokenType,
TuiMapper,
TuiMonth,
tuiTypedFromEvent,
TuiTypedMapper,
} from '@taiga-ui/cdk';
import {
TUI_ANIMATIONS_DURATION,
Expand Down Expand Up @@ -225,7 +225,7 @@ export class TuiMobileCalendarComponent implements AfterViewInit {
});
}

readonly disabledItemHandlerMapper: TuiTypedMapper<
readonly disabledItemHandlerMapper: TuiMapper<
[TuiBooleanHandler<TuiDay>, TuiDay, TuiDay],
TuiBooleanHandler<TuiDay>
> = (disabledItemHandler, min, max) => item =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Output,
QueryList,
} from '@angular/core';
import {TuiRowContext} from '@taiga-ui/addon-table/interfaces';
import {EMPTY_QUERY} from '@taiga-ui/cdk';
import {TUI_ARROW_OPTIONS, TuiArrowOptions} from '@taiga-ui/kit';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
Expand Down Expand Up @@ -59,10 +60,10 @@ export class TuiTbodyComponent<T extends Partial<Record<keyof T, any>>> {
return this.pipe.transform(this.data);
}

readonly toContext = (
$implicit: T,
index: number,
): {$implicit: T; index: number} => ({$implicit, index});
readonly toContext = ($implicit: T, index: number): TuiRowContext<T> => ({
$implicit,
index,
});

onClick(): void {
this.open = !this.open;
Expand Down
4 changes: 2 additions & 2 deletions projects/cdk/components/alert-host/alert-host.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import {TUI_PARENT_ANIMATION} from '@taiga-ui/cdk/constants';
import {TuiDestroyService} from '@taiga-ui/cdk/services';
import {TUI_ALERTS} from '@taiga-ui/cdk/tokens';
import {TuiDialog, TuiTypedMapper} from '@taiga-ui/cdk/types';
import {TuiDialog, TuiMapper} from '@taiga-ui/cdk/types';
import {POLYMORPHEUS_CONTEXT} from '@tinkoff/ng-polymorpheus';
import {combineLatest, Observable} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
Expand Down Expand Up @@ -54,7 +54,7 @@ export class TuiAlertHostComponent<T extends TuiDialog<unknown, unknown>>
});
}

readonly mapper: TuiTypedMapper<[unknown], Injector> = useValue =>
readonly mapper: TuiMapper<[unknown], Injector> = useValue =>
Injector.create({
providers: [
{
Expand Down
5 changes: 2 additions & 3 deletions projects/cdk/pipes/filter/filter.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Pipe, PipeTransform} from '@angular/core';
import {TuiMatcher, TuiTypedMatcher} from '@taiga-ui/cdk/types';
import {TuiMatcher} from '@taiga-ui/cdk/types';

@Pipe({name: `tuiFilter`})
export class TuiFilterPipe implements PipeTransform {
Expand All @@ -10,10 +10,9 @@ export class TuiFilterPipe implements PipeTransform {
* @param matcher method for filtering
* @param args arbitrary number of additional arguments
*/
transform<T>(items: readonly T[], matcher: TuiMatcher<T>, ...args: any[]): T[];
transform<T, U extends unknown[]>(
items: readonly T[],
matcher: TuiTypedMatcher<[T, ...U]>,
matcher: TuiMatcher<[T, ...U]>,
...args: U
): T[] {
return items.filter(item => matcher(item, ...args));
Expand Down
9 changes: 3 additions & 6 deletions projects/cdk/pipes/filter/test/filter.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {TuiFilterPipe, TuiTypedMatcher} from '@taiga-ui/cdk';
import {TuiFilterPipe, TuiMatcher} from '@taiga-ui/cdk';

describe(`TuiFilter pipe`, () => {
let pipe: TuiFilterPipe;
const data = [`two`, `eleven`];
const args = [`two`, `four`];
const matcher: TuiTypedMatcher<[string, number, ...string[]]> = (
item,
search,
...rest
) => item.length === search || rest.includes(item);
const matcher: TuiMatcher<[string, number, ...string[]]> = (item, search, ...rest) =>
item.length === search || rest.includes(item);

beforeEach(() => {
pipe = new TuiFilterPipe();
Expand Down
5 changes: 2 additions & 3 deletions projects/cdk/pipes/mapper/mapper.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Pipe, PipeTransform} from '@angular/core';
import {TuiMapper, TuiTypedMapper} from '@taiga-ui/cdk/types';
import {TuiMapper} from '@taiga-ui/cdk/types';

@Pipe({name: `tuiMapper`})
export class TuiMapperPipe implements PipeTransform {
Expand All @@ -10,10 +10,9 @@ export class TuiMapperPipe implements PipeTransform {
* @param mapper a mapping function
* @param args arbitrary number of additional arguments
*/
transform<T, G>(value: T, mapper: TuiMapper<T, G>, ...args: any[]): G;
transform<T extends unknown[], U, G>(
value: U,
mapper: TuiTypedMapper<[U, ...T], G>,
mapper: TuiMapper<[U, ...T], G>,
...args: T
): G {
return mapper(value, ...args);
Expand Down
4 changes: 2 additions & 2 deletions projects/cdk/pipes/mapper/test/mapper.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {TuiMapperPipe, TuiTypedMapper} from '@taiga-ui/cdk';
import {TuiMapper, TuiMapperPipe} from '@taiga-ui/cdk';

describe(`TuiMapper pipe`, () => {
let pipe: TuiMapperPipe;
const data = `test`;
const args = [`three`, `eleven`];
const mapper: TuiTypedMapper<[string, ...string[]], string> = (item, ...rest) =>
const mapper: TuiMapper<[string, ...string[]], string> = (item, ...rest) =>
item.toUpperCase() + rest.join(` `);

beforeEach(() => {
Expand Down
4 changes: 4 additions & 0 deletions projects/cdk/schematics/ng-update/v4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {TuiSchema} from '../../ng-add/schema';
import {FINISH_SYMBOL, START_SYMBOL, titleLog} from '../../utils/colored-log';
import {getExecutionTime} from '../../utils/get-execution-time';
import {replaceThumbnailCard} from './steps/replace-thumbnail-card';
import {restoreTuiMapper} from './steps/restore-tui-mapper';
import {restoreTuiMatcher} from './steps/restore-tui-matcher';

export function updateToV4(options: TuiSchema): Rule {
const t0 = performance.now();
Expand All @@ -18,6 +20,8 @@ export function updateToV4(options: TuiSchema): Rule {

return chain([
replaceThumbnailCard(options),
restoreTuiMapper(options),
restoreTuiMatcher(options),
() => {
const executionTime = getExecutionTime(t0, performance.now());

Expand Down
128 changes: 128 additions & 0 deletions projects/cdk/schematics/ng-update/v4/steps/restore-tui-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
import {
createProject,
DevkitFileSystem,
getSourceFiles,
saveActiveProject,
setActiveProject,
SourceFile,
} from 'ng-morph';
import {ts} from 'ts-morph';

import {ALL_FILES, ALL_TS_FILES} from '../../../constants';
import {TuiSchema} from '../../../ng-add/schema';
import {
FINISH_SYMBOL,
infoLog,
REPLACE_SYMBOL,
SMALL_TAB_SYMBOL,
titleLog,
} from '../../../utils/colored-log';
import {projectRoot} from '../../../utils/project-root';
import {getFileSystem} from '../../utils/get-file-system';
import {Replacement, replaceSubstrings} from './utils/replace-substrings';

export function restoreTuiMapper(options: TuiSchema): Rule {
return (tree: Tree, _: SchematicContext) => {
const fileSystem = getFileSystem(tree);

updateTuiMapper(options, fileSystem);
renameTuiTypedMapper(options, fileSystem);

!options[`skip-logs`] && titleLog(`${FINISH_SYMBOL} successfully migrated \n`);
};
}

function updateTuiMapper(options: TuiSchema, fileSystem: DevkitFileSystem): void {
!options[`skip-logs`] &&
infoLog(
`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} updating TuiMapper typing to the typed version`,
);

getSourceFiles(ALL_TS_FILES).forEach(sourceFile => {
const replacements = findTuiMapperFirstTypeArgReplacements(sourceFile);
const sourceText = sourceFile.getFullText();
const transformed = replaceSubstrings(sourceText, replacements);

sourceFile.replaceWithText(transformed);
});

fileSystem.commitEdits();
saveActiveProject();
setActiveProject(createProject(fileSystem.tree, projectRoot(), ALL_FILES));
}

function findTuiMapperFirstTypeArgReplacements(sourceFile: SourceFile): Replacement[] {
const replacements: Replacement[] = [];

sourceFile.transform(traversal => {
const node = traversal.visitChildren();

if (!ts.isTypeReferenceNode(node) || node.typeName.getText() !== `TuiMapper`) {
return node;
}

const typeArguments = node.typeArguments;

if (!typeArguments || typeArguments.length !== 2) {
return node;
}

const [inputType] = typeArguments;

replacements.push({
start: inputType.getStart(),
from: inputType.getText(),
to: `[${inputType.getText()}, ...any]`,
});

return node;
});

return replacements;
}

function renameTuiTypedMapper(options: TuiSchema, fileSystem: DevkitFileSystem): void {
!options[`skip-logs`] &&
infoLog(
`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} renaming TuiTypedMapper to TuiMapper`,
);

getSourceFiles(ALL_TS_FILES).forEach(sourceFile => {
const replacements = findTuiTypedMapperReplacements(sourceFile);
const sourceText = sourceFile.getFullText();
const transformed = replaceSubstrings(sourceText, replacements);

sourceFile.replaceWithText(transformed);
sourceFile.organizeImports();
});

fileSystem.commitEdits();
saveActiveProject();
setActiveProject(createProject(fileSystem.tree, projectRoot(), ALL_FILES));
}

function findTuiTypedMapperReplacements(sourceFile: SourceFile): Replacement[] {
const replacements: Replacement[] = [];

sourceFile.transform(traversal => {
const node = traversal.visitChildren();

if (
!ts.isTypeReferenceNode(node) ||
node.typeName.getText() !== `TuiTypedMapper`
) {
return node;
}

replacements.push({
start: node.getStart(),
from: `TuiTypedMapper`,
to: `TuiMapper`,
});

return node;
});

return replacements;
}
Loading