@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
22 lines • 1.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RemoveComponentImport = RemoveComponentImport;
const ts_morph_1 = require("@rxap/ts-morph");
const ts_morph_2 = require("ts-morph");
function RemoveComponentImport(sourceFileOrClassDeclaration, componentImport) {
var _a, _b, _c;
const classDeclaration = sourceFileOrClassDeclaration.isKind(ts_morph_2.SyntaxKind.ClassDeclaration) ? sourceFileOrClassDeclaration : (0, ts_morph_1.GetComponentClass)(sourceFileOrClassDeclaration);
const sourceFile = sourceFileOrClassDeclaration.isKind(ts_morph_2.SyntaxKind.SourceFile) ? sourceFileOrClassDeclaration : sourceFileOrClassDeclaration.getSourceFile();
const importName = typeof componentImport === 'string' ? componentImport : (_a = componentImport.namedImport) !== null && _a !== void 0 ? _a : componentImport.name;
(_b = sourceFile.getImportDeclaration(importDeclaration => importDeclaration.getNamedImports().some(namedImport => namedImport.getName() === importName))) === null || _b === void 0 ? void 0 : _b.remove();
if ((0, ts_morph_1.IsTypeImport)(componentImport) && componentImport.moduleSpecifier) {
(_c = sourceFile.getImportDeclaration(componentImport.moduleSpecifier)) === null || _c === void 0 ? void 0 : _c.remove();
}
const componentDecoratorObject = (0, ts_morph_1.GetComponentDecoratorObject)(classDeclaration);
const importsArray = (0, ts_morph_1.GetArrayLiteralFromObjectLiteral)(componentDecoratorObject, 'imports');
const element = importsArray === null || importsArray === void 0 ? void 0 : importsArray.getElements().find(e => e.getText().includes(importName));
if (element) {
importsArray === null || importsArray === void 0 ? void 0 : importsArray.removeElement(element);
}
}
//# sourceMappingURL=remove-component-import.js.map