Skip to content

Commit

Permalink
chore(cdk): schematics for legacy mask utils (#6688)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov authored Feb 5, 2024
1 parent 3cbdb52 commit a3ee8d0
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 15 deletions.
8 changes: 7 additions & 1 deletion projects/cdk/schematics/ng-update/v4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {getExecutionTime} from '../../utils/get-execution-time';
import {projectRoot} from '../../utils/project-root';
import {replaceIdentifiers} from '../steps/replace-identifier';
import {showWarnings} from '../steps/show-warnings';
import {migrateTemplates, restoreTuiMapper, restoreTuiMatcher} from './steps';
import {
migrateLegacyMask,
migrateTemplates,
restoreTuiMapper,
restoreTuiMatcher,
} from './steps';
import {MIGRATION_WARNINGS} from './steps/constants';
import {IDENTIFIERS_TO_REPLACE} from './steps/constants/identifiers-to-replace';

Expand All @@ -24,6 +29,7 @@ function main(options: TuiSchema): Rule {

restoreTuiMapper(options);
restoreTuiMatcher(options);
migrateLegacyMask(options);

migrateTemplates(fileSystem, options);
showWarnings(context, MIGRATION_WARNINGS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,136 @@
import {MigrationWarning} from '../../../interfaces';

export const MIGRATION_WARNINGS: MigrationWarning[] = [
{
name: 'TuiNumberMaskOptions',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Number mask from Maskito instead https://maskito.dev/kit/number',
},
{
name: 'tuiCreateAutoCorrectedNumberPipe',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Number mask from Maskito instead https://maskito.dev/kit/number',
},
{
name: 'tuiCreateNumberMask',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Number mask from Maskito instead https://maskito.dev/kit/number',
},
{
name: 'tuiEnableAutoCorrectDecimalSymbol',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Number mask from Maskito instead https://maskito.dev/kit/number',
},
{
name: 'TuiTextMaskConfig',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Maskito instead https://maskito.dev',
},
{
name: 'TuiTextMaskCorrectionHandler',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Maskito instead https://maskito.dev',
},
{
name: 'TuiTextMaskListHandler',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Maskito instead https://maskito.dev',
},
{
name: 'TuiTextMaskList',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Maskito instead https://maskito.dev',
},
{
name: 'TuiTextMaskOptions',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Maskito instead https://maskito.dev',
},
{
name: 'TuiTextMaskPipeHandler',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Maskito instead https://maskito.dev',
},
{
name: 'TuiTextMaskPipeResult',
moduleSpecifier: '@taiga-ui/core',
message: 'Use Maskito instead https://maskito.dev',
},
{
name: 'tuiCreateCorrectionMask',
moduleSpecifier: '@taiga-ui/core',
message:
'Use processors from Maskito instead https://maskito.dev/core-concepts/processors',
},
{
name: 'tuiCreateTimeMask',
moduleSpecifier: '@taiga-ui/kit',
message:
'tuiCreateTimeMask has been removed. Use Time mask from Maskito instead https://maskito.dev/kit/time',
message: 'Use Time mask from Maskito instead https://maskito.dev/kit/time',
},
{
name: 'tuiCreateDateRangeMask',
moduleSpecifier: '@taiga-ui/kit',
message:
'tuiCreateDateRangeMask has been removed. Use DateRange mask from Maskito instead https://maskito.dev/kit/date-range',
'Use DateRange mask from Maskito instead https://maskito.dev/kit/date-range',
},
{
name: 'tuiCreateDateMask',
moduleSpecifier: '@taiga-ui/kit',
message:
'tuiCreateDateMask has been removed. Use Date mask from Maskito instead https://maskito.dev/kit/date',
message: 'Use Date mask from Maskito instead https://maskito.dev/kit/date',
},
{
name: 'tuiCreateAutoCorrectedTimePipe',
moduleSpecifier: '@taiga-ui/kit',
message:
'tuiCreateAutoCorrectedTimePipe has been removed. Use Time mask from Maskito instead https://maskito.dev/kit/time',
message: 'Use Time mask from Maskito instead https://maskito.dev/kit/time',
},
{
name: 'tuiCreateAutoCorrectedDateTimePipe',
moduleSpecifier: '@taiga-ui/kit',
message:
'tuiCreateAutoCorrectedDateTimePipe has been removed. Use DateTime mask from Maskito instead https://maskito.dev/kit/date-time',
'Use DateTime mask from Maskito instead https://maskito.dev/kit/date-time',
},
{
name: 'tuiCreateAutoCorrectedDateRangePipe',
moduleSpecifier: '@taiga-ui/kit',
message:
'tuiCreateAutoCorrectedDateRangePipe has been removed. Use DateRange mask from Maskito instead https://maskito.dev/kit/date-range',
'Use DateRange mask from Maskito instead https://maskito.dev/kit/date-range',
},
{
name: 'tuiCreateAutoCorrectedDatePipe',
moduleSpecifier: '@taiga-ui/kit',
message:
'tuiCreateAutoCorrectedDatePipe has been removed. Use Date mask from Maskito instead https://maskito.dev/kit/date',
message: 'Use Date mask from Maskito instead https://maskito.dev/kit/date',
},
{
name: 'TuiAutoCorrectedDatePipeConfigs',
moduleSpecifier: '@taiga-ui/kit',
message:
'TuiAutoCorrectedDatePipeConfigs has been removed. Use Date mask from Maskito instead https://maskito.dev/kit/date',
message: 'Use Date mask from Maskito instead https://maskito.dev/kit/date',
},
{
name: 'tuiNormalizeDateValue',
moduleSpecifier: '@taiga-ui/kit',
message: 'Use Date mask from Maskito instead https://maskito.dev/kit/date',
},
{
name: 'EMPTY_MASK',
moduleSpecifier: '@taiga-ui/kit',
message:
'Use Maskito instead https://maskito.dev (use MASKITO_DEFAULT_OPTIONS or just pass null to [maskito]-directive)',
},
{
name: 'TextMaskModule',
moduleSpecifier: '@taiga-ui/kit',
message: 'Use Maskito instead https://maskito.dev',
},

{
name: 'cardHolderMask',
moduleSpecifier: '@taiga-ui/addon-commerce',
message: 'Use TUI_CARD_HOLDER_MASK with Maskito instead https://maskito.dev',
},
{
name: 'tuiCreateAutoCorrectedExpirePipe',
moduleSpecifier: '@taiga-ui/addon-commerce',
message:
'tuiNormalizeDateValue has been removed. Use Date mask from Maskito instead https://maskito.dev/kit/date',
'Use Date mask (with [mode]="mm/yyyy") from Maskito instead https://maskito.dev/kit/date/API?mode=mm%2Fyyyy',
},
];
1 change: 1 addition & 0 deletions projects/cdk/schematics/ng-update/v4/steps/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './migrate-legacy-mask';
export * from './migrate-templates';
export * from './restore-tui-mapper';
export * from './restore-tui-matcher';
89 changes: 89 additions & 0 deletions projects/cdk/schematics/ng-update/v4/steps/migrate-legacy-mask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
addUniqueImport,
FINISH_SYMBOL,
getNamedImportReferences,
infoLog,
removeImport,
REPLACE_SYMBOL,
SMALL_TAB_SYMBOL,
titleLog,
} from '@taiga-ui/cdk/schematics';
import {Node} from 'ng-morph';

import {TuiSchema} from '../../../ng-add/schema';

export function migrateLegacyMask(options: TuiSchema): void {
!options['skip-logs'] &&
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} migrating legacy mask utils...`);

migrateTuiMaskedMoneyValueIsEmpty(options);
migrateTuiMaskedNumberStringToNumber(options);

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

function migrateTuiMaskedMoneyValueIsEmpty(options: TuiSchema): void {
!options['skip-logs'] &&
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} tuiMaskedMoneyValueIsEmpty`);

const references = getNamedImportReferences(
'tuiMaskedMoneyValueIsEmpty',
'@taiga-ui/core',
);

references.forEach(ref => {
if (ref.wasForgotten()) {
return;
}

const parent = ref.getParent();

if (Node.isImportSpecifier(parent)) {
removeImport(parent);
addUniqueImport(
parent.getSourceFile().getFilePath(),
'maskitoParseNumber',
'@maskito/kit',
);
} else if (Node.isCallExpression(parent)) {
const [value] = parent.getArguments();

parent.replaceWithText(
`Number.isNaN(maskitoParseNumber(${value.getText()}, ','))`,
);
}
});
}

function migrateTuiMaskedNumberStringToNumber(options: TuiSchema): void {
!options['skip-logs'] &&
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} tuiMaskedNumberStringToNumber`);

const references = getNamedImportReferences(
'tuiMaskedNumberStringToNumber',
'@taiga-ui/core',
);

references.forEach(ref => {
if (ref.wasForgotten()) {
return;
}

const parent = ref.getParent();

if (Node.isImportSpecifier(parent)) {
removeImport(parent);
addUniqueImport(
parent.getSourceFile().getFilePath(),
'maskitoParseNumber',
'@maskito/kit',
);
} else if (Node.isCallExpression(parent)) {
const [value, decimalSeparator] = parent.getArguments();

parent.replaceWithText(
`maskitoParseNumber(${value.getText()}, ${decimalSeparator.getText()})`,
);
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import {TuiSchema} from '@taiga-ui/cdk/schematics/ng-add/schema';
import {
createProject,
createSourceFile,
resetActiveProject,
saveActiveProject,
setActiveProject,
} from 'ng-morph';
import {join} from 'path';

const collectionPath = join(__dirname, '../../../migration.json');

const BEFORE = `
import {
AModule,
tuiMaskedMoneyValueIsEmpty,
BModule,
tuiMaskedNumberStringToNumber,
CModule
} from '@taiga-ui/core';
@Component({
standalone: true
template: '',
imports: [AModule, BModule, CModule],
})
export class TestComponent {
onClick(): void {
const isEmpty = tuiMaskedMoneyValueIsEmpty('123,45');
const parsedValue = tuiMaskedNumberStringToNumber('123,45', ',', ' ');
const parsedValueAgain = tuiMaskedNumberStringToNumber(
this.value,
this.decimalSeparator,
CHAR_NO_BREAK_SPACE
);
console.info(isEmpty, parsedValue, parsedValueAgain);
}
}`.trim();

const AFTER = `
import { maskitoParseNumber } from "@maskito/kit";
import {
AModule,
BModule,
CModule
} from '@taiga-ui/core';
@Component({
standalone: true
template: '',
imports: [AModule, BModule, CModule],
})
export class TestComponent {
onClick(): void {
const isEmpty = Number.isNaN(maskitoParseNumber('123,45', ','));
const parsedValue = maskitoParseNumber('123,45', ',');
const parsedValueAgain = maskitoParseNumber(this.value, this.decimalSeparator);
console.info(isEmpty, parsedValue, parsedValueAgain);
}
}`.trim();

describe('ng-update', () => {
let host: UnitTestTree;
let runner: SchematicTestRunner;

beforeEach(() => {
host = new UnitTestTree(new HostTree());
runner = new SchematicTestRunner('schematics', collectionPath);

setActiveProject(createProject(host));

createSourceFile('test/app/test.component.ts', BEFORE);

saveActiveProject();
});

it('migration works', async () => {
const tree = await runner
.runSchematicAsync(
'updateToV4',
{'skip-logs': process.env['TUI_CI'] === 'true'} as Partial<TuiSchema>,
host,
)
.toPromise();

const modifiedFile = tree.readContent('test/app/test.component.ts');

expect(modifiedFile).toEqual(AFTER);
});

afterEach(() => resetActiveProject());
});

0 comments on commit a3ee8d0

Please sign in to comment.