Skip to content

Commit

Permalink
chore: fix migrations (#8069)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin authored Jul 10, 2024
1 parent 9be34d5 commit 6dce393
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export interface ReplacementIdentifierMulti extends Pick<ReplacementIdentifier,
readonly moduleSpecifier: string;
readonly name: string;
readonly namedImport?: string;
readonly spreadInModule?: boolean;
}>
| {
readonly moduleSpecifier: string;
readonly name: string;
readonly namedImport?: string;
readonly spreadInModule?: boolean;
};
}
31 changes: 23 additions & 8 deletions projects/cdk/schematics/ng-update/steps/replace-identifier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Node} from 'ng-morph';
import {Node, ts} from 'ng-morph';

import type {TuiSchema} from '../../ng-add/schema';
import {addUniqueImport} from '../../utils/add-unique-import';
Expand Down Expand Up @@ -40,9 +40,15 @@ export function replaceIdentifier({from, to}: ReplacementIdentifierMulti): void
removeImport(parent);
addImports(to, parent.getSourceFile().getFilePath());
} else {
ref?.replaceWithText(
Array.isArray(to) ? to.map(({name}) => name).join(', ') : to.name,
const decorator = ref.getParentWhile(
(node) => node.getKindName() !== 'Decorator',
);

const inModule =
decorator?.getFirstChildIfKind(ts.SyntaxKind.Identifier)?.getText() ===
'NgModule';

ref.replaceWithText(getReplacementText(to, !!inModule));
}
});
}
Expand All @@ -55,9 +61,18 @@ function addImports(
? identifier.forEach(({name, namedImport, moduleSpecifier}) => {
addUniqueImport(filePath, namedImport || name, moduleSpecifier);
})
: addUniqueImport(
filePath,
identifier.namedImport || identifier.name,
identifier.moduleSpecifier,
);
: addUniqueImport(filePath, identifier.name, identifier.moduleSpecifier);
}

function getReplacementText(
to: ReplacementIdentifierMulti['to'],
inModule: boolean,
): string {
const res = Array.isArray(to) ? to : [to];

return res
.map(({name, spreadInModule}) =>
spreadInModule && inModule ? `...${name}` : name,
)
.join(', ');
}
4 changes: 3 additions & 1 deletion projects/cdk/schematics/ng-update/v4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ import {
} from './steps/constants';
import {MODULES_TO_REPLACE_WITH_PROVIDERS} from './steps/constants/modules-to-replace';
import {TYPES_TO_RENAME} from './steps/constants/types';
import {migrateRoot} from './steps/migrate-root';
import {replaceModulesWithProviders} from './steps/utils/replace-modules-with-providers';

function main(options: TuiSchema): Rule {
return (tree: Tree, context: SchematicContext) => {
const fileSystem = getFileSystem(tree);

replaceEnums(options, ENUMS_TO_REPLACE);
migrateRoot(fileSystem, options);
replaceIdentifiers(options, IDENTIFIERS_TO_REPLACE);
removeModules(options, MODULES_TO_REMOVE);
replaceModulesWithProviders(options, MODULES_TO_REPLACE_WITH_PROVIDERS);
Expand All @@ -54,7 +57,6 @@ function main(options: TuiSchema): Rule {
migrateOptionProviders(options);
migrateAllCountryIsoCodes(options);

replaceEnums(options, ENUMS_TO_REPLACE);
migrateTemplates(fileSystem, options);
showWarnings(context, MIGRATION_WARNINGS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
from: {name: 'TuiCheckboxModule', moduleSpecifier: '@taiga-ui/experimental'},
to: {name: 'TuiCheckbox', moduleSpecifier: '@taiga-ui/kit'},
},
{
from: {name: 'TuiCheckboxModule', moduleSpecifier: '@taiga-ui/kit'},
to: {name: 'TuiCheckbox', moduleSpecifier: '@taiga-ui/kit'},
},
{
from: {name: 'TuiSwipeActionsModule', moduleSpecifier: '@taiga-ui/experimental'},
to: [
Expand Down Expand Up @@ -455,7 +459,7 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
to: {name: 'TUI_SCROLL_REF', moduleSpecifier: '@taiga-ui/core'},
},
{
from: {name: 'TUI_SANITIZER', moduleSpecifier: '@taiga-ui/cdk'},
from: {name: 'TUI_SANITIZER', moduleSpecifier: '@taiga-ui/core'},
to: {name: 'TUI_SANITIZER', moduleSpecifier: '@taiga-ui/legacy'},
},
{
Expand Down Expand Up @@ -656,10 +660,6 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
from: {name: 'TuiSvgModule', moduleSpecifier: '@taiga-ui/core'},
to: {name: 'TuiSvgComponent', moduleSpecifier: '@taiga-ui/legacy'},
},
{
from: {name: 'TuiRootModule', moduleSpecifier: '@taiga-ui/core'},
to: {name: 'TuiRoot', moduleSpecifier: '@taiga-ui/core'},
},
{
from: {name: 'TuiCellModule', moduleSpecifier: '@taiga-ui/experimental'},
to: {name: 'TuiCell', moduleSpecifier: '@taiga-ui/layout'},
Expand Down Expand Up @@ -874,16 +874,6 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
moduleSpecifier: '@taiga-ui/proprietary',
},
},
{
from: {
name: 'TuiProprietaryRoot2023Module',
moduleSpecifier: '@taiga-ui/proprietary-core',
},
to: {
name: 'TuiProprietaryRoot2023Component',
moduleSpecifier: '@taiga-ui/proprietary',
},
},
{
from: {
name: 'TuiSignalModule',
Expand Down Expand Up @@ -1103,7 +1093,7 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
},
to: {
name: 'TuiArrowComponent',
moduleSpecifier: '@taiga-ui/kit',
moduleSpecifier: '@taiga-ui/legacy',
},
},
{
Expand Down Expand Up @@ -1256,6 +1246,26 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
moduleSpecifier: '@taiga-ui/kit',
},
},
{
from: {
name: 'TuiPreviewDialogService',
moduleSpecifier: '@taiga-ui/addon-preview',
},
to: {
name: 'TuiPreviewDialogService',
moduleSpecifier: '@taiga-ui/kit',
},
},
{
from: {
name: 'PolymorpheusModule',
moduleSpecifier: '@tinkoff/ng-polymorpheus',
},
to: [
{name: 'PolymorpheusTemplate', moduleSpecifier: '@taiga-ui/polymorpheus'},
{name: 'PolymorpheusOutlet', moduleSpecifier: '@taiga-ui/polymorpheus'},
],
},
{
from: {
name: 'tuiDefaultSort',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
interface ModuleToReplace {
export interface ProviderToReplace {
name: string;
providerSpecifier: string;
isFunction?: boolean;
}

export interface ModuleToReplace {
from: {name: string; moduleSpecifier: string};
to: {name: string; providerSpecifier: string};
to: ProviderToReplace | ProviderToReplace[];
}

export const MODULES_TO_REPLACE_WITH_PROVIDERS: ModuleToReplace[] = [
Expand All @@ -12,6 +18,7 @@ export const MODULES_TO_REPLACE_WITH_PROVIDERS: ModuleToReplace[] = [
to: {
name: 'tuiProvideMobileCalendar',
providerSpecifier: '@taiga-ui/addon-mobile',
isFunction: true,
},
},
];
93 changes: 93 additions & 0 deletions projects/cdk/schematics/ng-update/v4/steps/migrate-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {
type DevkitFileSystem,
getActiveProject,
getPackageJsonDependency,
Node,
} from 'ng-morph';

import type {TuiSchema} from '../../../ng-add/schema';
import {addUniqueImport} from '../../../utils/add-unique-import';
import {infoLog, REPLACE_SYMBOL, SMALL_TAB_SYMBOL} from '../../../utils/colored-log';
import {getNamedImportReferences} from '../../../utils/get-named-import-references';
import {removeImport} from '../../../utils/import-manipulations';

export function migrateRoot(fileSystem: DevkitFileSystem, options: TuiSchema): void {
!options['skip-logs'] &&
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} updating TuiRoot`);

const refs = [
...getNamedImportReferences('TuiRootModule', '@taiga-ui/core'),
...getNamedImportReferences(
'TuiProprietaryRootModule',
'@taiga-ui/proprietary-core',
),
...getNamedImportReferences(
'TuiProprietaryRoot2023Module',
'@taiga-ui/proprietary-core',
),
];

for (const ref of refs) {
if (ref.wasForgotten()) {
return;
}

const parent = ref.getParent();

if (Node.isImportSpecifier(parent)) {
removeImport(parent);
addUniqueImport(
parent.getSourceFile().getFilePath(),
'TuiRoot',
'@taiga-ui/core',
);
} else {
replaceRootIdentifier(ref, fileSystem);
}
}
}

function replaceRootIdentifier(ref: Node, fileSystem: DevkitFileSystem): void {
const callExpression = ref.getParentWhile(Node.isCallExpression);

if (
callExpression &&
callExpression.getExpression().getText() === 'importProvidersFrom'
) {
ref.replaceWithText('');
addProviders(callExpression, fileSystem);
} else {
ref.replaceWithText('TuiRoot');
}
}

function addProviders(callExpression: Node, fileSystem: DevkitFileSystem): void {
const array = callExpression.getParentWhile(Node.isArrayLiteralExpression);

if (!array) {
return;
}

array.addElement('NG_EVENT_PLUGINS');
addUniqueImport(
array.getSourceFile().getFilePath(),
'NG_EVENT_PLUGINS',
'@taiga-ui/event-plugins',
);

getActiveProject();
const proprietary = getPackageJsonDependency(
fileSystem.tree,
'@taiga-ui/proprietary-core',
);

if (proprietary) {
array.addElement('TBANK_PROVIDERS');

addUniqueImport(
array.getSourceFile().getFilePath(),
'TBANK_PROVIDERS',
'@taiga-ui/proprietary',
);
}
}
13 changes: 12 additions & 1 deletion projects/cdk/schematics/ng-update/v4/steps/update-packages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/// <reference lib="es2021" />
import type {DevkitFileSystem} from 'ng-morph';
import {addPackageJsonDependency, removePackageJsonDependency} from 'ng-morph';
import {
addPackageJsonDependency,
getPackageJsonDependency,
removePackageJsonDependency,
} from 'ng-morph';

import {TUI_VERSION} from '../../../../constants/version';
import type {TuiSchema} from '../../../ng-add/schema';
Expand Down Expand Up @@ -42,4 +46,11 @@ export function updatePackages({tree}: DevkitFileSystem, _: TuiSchema): void {
},
tree,
);

if (!getPackageJsonDependency(tree, '@taiga-ui/event-plugins')) {
addPackageJsonDependency(tree, {
name: '@taiga-ui/event-plugins',
version: '^4.0.1',
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import {
} from '../../../../utils/colored-log';
import {getNamedImportReferences} from '../../../../utils/get-named-import-references';
import {removeImport} from '../../../../utils/import-manipulations';

interface ModuleToReplace {
from: {name: string; moduleSpecifier: string};
to: {name: string; providerSpecifier: string};
}
import type {ModuleToReplace, ProviderToReplace} from '../constants/modules-to-replace';

export const replaceModulesWithProviders = (
options: TuiSchema,
Expand All @@ -40,6 +36,7 @@ export const replaceModulesWithProviders = (

function replaceModule({from, to}: ModuleToReplace): void {
const references = getNamedImportReferences(from.name, from.moduleSpecifier);
const toReplace = Array.isArray(to) ? to : [to];

references.forEach((ref) => {
if (ref.wasForgotten()) {
Expand All @@ -50,21 +47,28 @@ function replaceModule({from, to}: ModuleToReplace): void {

if (Node.isImportSpecifier(parent)) {
removeImport(parent);
addImport(to, parent.getSourceFile().getFilePath());
toReplace.forEach((provider) =>
addImport(provider, parent.getSourceFile().getFilePath()),
);
} else if (Node.isArrayLiteralExpression(parent)) {
parent.removeElement(ref.getChildIndex());

addProvider(to, parent.getSourceFile().getFilePath());
const index = parent
.getElements()
.findIndex((el) => el.getText() === from.name);

parent.removeElement(index);
toReplace.forEach((provider) =>
addProvider(provider, parent.getSourceFile().getFilePath()),
);
}
});
}

function addImport(identifier: ModuleToReplace['to'], filePath: string): void {
function addImport(identifier: ProviderToReplace, filePath: string): void {
addUniqueImport(filePath, identifier.name, identifier.providerSpecifier);
}

function addProvider(identifier: ModuleToReplace['to'], filePath: string): void {
const provider = `${identifier.name}()`;
function addProvider(identifier: ProviderToReplace, filePath: string): void {
const provider = `${identifier.name}${identifier.isFunction ? '()' : ''}`;

const componentClass = getNgComponents(filePath)[0];

Expand Down
Loading

0 comments on commit 6dce393

Please sign in to comment.