@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
98 lines • 4.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateBreakpointService = migrateBreakpointService;
const ng_morph_1 = require("ng-morph");
const constants_1 = require("../../../constants");
const add_unique_import_1 = require("../../../utils/add-unique-import");
const import_manipulations_1 = require("../../../utils/import-manipulations");
const insert_todo_1 = require("../../../utils/insert-todo");
const TAIGA_CORE = '@taiga-ui/core';
const BREAKPOINT_SERVICE = 'TuiBreakpointService';
const BREAKPOINT_TOKEN = 'TUI_BREAKPOINT';
const RXJS_INTEROP = '@angular/core/rxjs-interop';
const TO_OBSERVABLE = 'toObservable';
const BREAKPOINT_TODO_MESSAGE = 'TuiBreakpointService is deprecated. Use TUI_BREAKPOINT (signal token), wrap with toObservable(...) for Observable-based code if needed';
function migrateBreakpointService(_tree, _options) {
(0, ng_morph_1.getSourceFiles)(constants_1.ALL_TS_FILES).forEach((sourceFile) => {
const changed = migrateSourceFile(sourceFile);
addTodoForUnsupportedUsages(sourceFile);
if (!changed) {
return;
}
(0, add_unique_import_1.addUniqueImport)(sourceFile.getFilePath(), BREAKPOINT_TOKEN, TAIGA_CORE);
(0, add_unique_import_1.addUniqueImport)(sourceFile.getFilePath(), TO_OBSERVABLE, RXJS_INTEROP);
cleanupBreakpointServiceImport(sourceFile);
});
}
function migrateSourceFile(sourceFile) {
let changed = false;
sourceFile.getDescendantsOfKind(ng_morph_1.SyntaxKind.CallExpression).forEach((call) => {
if (call.getExpression().getText() !== 'inject') {
return;
}
const [firstArg] = call.getArguments();
if ((firstArg === null || firstArg === void 0 ? void 0 : firstArg.getText()) !== BREAKPOINT_SERVICE) {
return;
}
firstArg.replaceWithText(BREAKPOINT_TOKEN);
const parent = call.getParent();
const isAlreadyWrapped = ng_morph_1.Node.isCallExpression(parent) &&
parent.getExpression().getText() === TO_OBSERVABLE &&
parent.getArguments()[0] === call;
if (!isAlreadyWrapped) {
call.replaceWithText(`${TO_OBSERVABLE}(${call.getText()})`);
}
changed = true;
});
return changed;
}
function cleanupBreakpointServiceImport(sourceFile) {
sourceFile
.getImportDeclarations()
.filter((decl) => decl.getModuleSpecifierValue() === TAIGA_CORE)
.forEach((decl) => {
const specifier = decl
.getNamedImports()
.find((namedImport) => namedImport.getName() === BREAKPOINT_SERVICE);
if (!specifier) {
return;
}
const nameNode = specifier.getNameNode();
const refs = ng_morph_1.Node.isIdentifier(nameNode)
? nameNode
.findReferencesAsNodes()
.filter((ref) => ref.getSourceFile().getFilePath() ===
sourceFile.getFilePath())
: [];
if (refs.length <= 1) {
(0, import_manipulations_1.removeImport)(specifier);
}
});
}
function addTodoForUnsupportedUsages(sourceFile) {
sourceFile
.getImportDeclarations()
.filter((decl) => decl.getModuleSpecifierValue() === TAIGA_CORE)
.forEach((decl) => {
const specifier = decl
.getNamedImports()
.find((namedImport) => namedImport.getName() === BREAKPOINT_SERVICE);
if (!specifier) {
return;
}
const nameNode = specifier.getNameNode();
const refs = ng_morph_1.Node.isIdentifier(nameNode)
? nameNode
.findReferencesAsNodes()
.filter((ref) => ref.getSourceFile().getFilePath() ===
sourceFile.getFilePath())
: [];
refs.forEach((ref) => {
if (ng_morph_1.Node.isImportSpecifier(ref.getParent())) {
return;
}
(0, insert_todo_1.insertTodo)(ref, BREAKPOINT_TODO_MESSAGE);
});
});
}
//# sourceMappingURL=migrate-breakpoint-service.js.map