Skip to content

Commit

Permalink
chore(cdk): fix 'no code comments'-problem in `TuiDestroyService => t…
Browse files Browse the repository at this point in the history
…akeUntilDestroyed` migration (#7044)
  • Loading branch information
nsbarsukov authored Mar 22, 2024
1 parent b47906c commit 905b96e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function migrateDestroyService(options: TuiSchema): void {
);

const references = getNamedImportReferences('TuiDestroyService', '@taiga-ui/cdk');
const nodesForComments: Node[] = [];

references.forEach(ref => {
if (ref.wasForgotten()) {
Expand Down Expand Up @@ -89,15 +90,9 @@ export function migrateDestroyService(options: TuiSchema): void {
...injectDestination.findReferencesAsNodes(),
);
injectDestination.remove();
} else {
const identifier = ref.getFirstAncestorByKind(SyntaxKind.Identifier);

identifier &&
insertTodo(
identifier,
'use takeUntilDestroyed instead (https://angular.io/api/core/rxjs-interop/takeUntilDestroyed)',
);
}
} else {
nodesForComments.push(ref);
}

destroyObservableUsages.forEach(node => {
Expand Down Expand Up @@ -146,16 +141,34 @@ export function migrateDestroyService(options: TuiSchema): void {
);
}

const identifier = node.getFirstAncestorByKind(SyntaxKind.Identifier);
return nodesForComments.push(node);
});
});

/**
* Inserting code comment should be placed after all AST-manipulations of this migration!
* When we insert some text into the source file,
* all previously queried refs will be forgotten and not be available for use.
* So, we should re-query them. It is not performance-friendly.
* ---
* We suppose that one code comment per file about `TuiDestroyService` is enough.
* ---
* @see https://ts-morph.com/manipulation/#strongwarningstrong
*/
nodesForComments.forEach(ref => {
if (ref.wasForgotten()) {
return;
}

return (
identifier &&
insertTodo(
identifier,
'use takeUntilDestroyed instead (https://angular.io/api/core/rxjs-interop/takeUntilDestroyed)',
)
const identifier = Node.isIdentifier(ref)
? ref
: ref.getFirstAncestorByKind(SyntaxKind.Identifier);

identifier &&
insertTodo(
identifier,
'use takeUntilDestroyed (https://angular.io/api/core/rxjs-interop/takeUntilDestroyed) instead of legacy TuiDestroyService',
);
});
});

!options['skip-logs'] && titleLog(`${FINISH_SYMBOL} successfully migrated \n`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,40 @@ export class TuiDestroyExample implements OnInit {
);
});

describe('too tricky use cases (just add code comment)', () => {
it('deps of provided DI entity', async () => {
expect(
await runMigration(
`
import {ElementRef, Provider} from '@angular/core';
import {TUI_DOC_PAGE_LOADED} from '@taiga-ui/addon-doc';
import {TuiDestroyService} from '@taiga-ui/cdk';
import {ResizeObserverService} from '@ng-web-apis/resize-observer';
export const DEMO_PAGE_LOADED_PROVIDER = {
provide: TUI_DOC_PAGE_LOADED,
deps: [ElementRef, ResizeObserverService, TuiDestroyService],
useFactory: readyToScrollFactory,
};
`.trim(),
),
).toEqual(
`
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import {ElementRef, Provider} from '@angular/core';
import {TUI_DOC_PAGE_LOADED} from '@taiga-ui/addon-doc';
import {ResizeObserverService} from '@ng-web-apis/resize-observer';
export const DEMO_PAGE_LOADED_PROVIDER = {
provide: TUI_DOC_PAGE_LOADED,
// TODO: (Taiga UI migration) use takeUntilDestroyed (https://angular.io/api/core/rxjs-interop/takeUntilDestroyed) instead of legacy TuiDestroyService
deps: [ElementRef, ResizeObserverService, TuiDestroyService],
useFactory: readyToScrollFactory,
};
`.trim(),
);
});
});

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

0 comments on commit 905b96e

Please sign in to comment.