@rxap/ts-morph
Version:
Provides utilities for manipulating TypeScript code using the ts-morph library. It offers a fluent API to add, modify, and remove code elements such as classes, decorators, imports, and properties in both Angular and NestJS projects. This package simplifi
75 lines • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetRouteChildrenArray = GetRouteChildrenArray;
exports.FindParentRouteByPath = FindParentRouteByPath;
exports.FindParentRouteByComponent = FindParentRouteByComponent;
exports.FindParentRouteChildrenArrayByPath = FindParentRouteChildrenArrayByPath;
exports.FindParentRouteChildrenArrayByComponent = FindParentRouteChildrenArrayByComponent;
const ts_morph_1 = require("ts-morph");
function GetRouteChildrenArray(e) {
var _a;
const childrenProperty = (_a = e.getProperty('children')) !== null && _a !== void 0 ? _a : e.addProperty({
name: 'children',
initializer: '[]',
kind: ts_morph_1.StructureKind.PropertyAssignment,
});
if (childrenProperty.isKind(ts_morph_1.SyntaxKind.PropertyAssignment)) {
return childrenProperty.getInitializerIfKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression);
}
throw new Error('Children property is not a PropertyAssignment');
}
function FindParentRouteByPath(ale, path) {
const fragment = path.pop();
for (const e of ale.getElements()) {
if (e.isKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)) {
const pathProperty = e.getProperty('path');
if (pathProperty === null || pathProperty === void 0 ? void 0 : pathProperty.isKind(ts_morph_1.SyntaxKind.PropertyAssignment)) {
const initializer = pathProperty.getInitializerIfKind(ts_morph_1.SyntaxKind.StringLiteral);
if ((initializer === null || initializer === void 0 ? void 0 : initializer.getLiteralText()) === fragment) {
if (path.length) {
const children = GetRouteChildrenArray(e);
// console.log('Continue search for parent route');
return FindParentRouteByPath(children, path);
}
else {
// console.log('Found parent route');
return e;
}
}
else {
// console.log('Path property does not match', initializer.getLiteralText(), fragment);
}
}
else {
// console.log('Element has no path property');
}
}
else {
// console.log('Element is not an ObjectLiteralExpression');
}
}
return null;
}
function FindParentRouteByComponent(ale, component) {
for (const e of ale.getElements()) {
if (e.isKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)) {
const componentProperty = e.getProperty('component');
if (componentProperty === null || componentProperty === void 0 ? void 0 : componentProperty.isKind(ts_morph_1.SyntaxKind.PropertyAssignment)) {
const initializer = componentProperty.getInitializer();
if ((initializer === null || initializer === void 0 ? void 0 : initializer.getText()) === component) {
return e;
}
}
}
}
return null;
}
function FindParentRouteChildrenArrayByPath(ale, path) {
const parent = FindParentRouteByPath(ale, path);
return parent ? GetRouteChildrenArray(parent) : null;
}
function FindParentRouteChildrenArrayByComponent(ale, component) {
const parent = FindParentRouteByComponent(ale, component);
return parent ? GetRouteChildrenArray(parent) : null;
}
//# sourceMappingURL=find-parent-route.js.map