@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
40 lines • 2.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoerceComponentImport = CoerceComponentImport;
const ts_morph_1 = require("ts-morph");
const coerce_array_element_1 = require("../coerce-array-element");
const coerce_imports_1 = require("../coerce-imports");
const get_coerce_array_literal_form_object_literal_1 = require("../get-coerce-array-literal-form-object-literal");
const type_import_1 = require("../type-import");
const get_component_class_1 = require("./get-component-class");
const get_component_decorator_object_1 = require("./get-component-decorator-object");
/**
* Coerces the component import by adding it to the imports array of the component decorator object.
* If a module specifier is provided, the import is also coerced at the file level.
*
* @param sourceFileOrClassDeclaration - The source file or class declaration that contains the component import.
* @param componentImport - The name or TypeImport of the import to coerce.
* @returns - The updated imports array after coercing the import.
*/
function CoerceComponentImport(sourceFileOrClassDeclaration, componentImport) {
var _a;
const sourceFile = sourceFileOrClassDeclaration.isKind(ts_morph_1.SyntaxKind.SourceFile) ? sourceFileOrClassDeclaration : sourceFileOrClassDeclaration.getSourceFile();
let componentDecoratorObject;
if (sourceFileOrClassDeclaration.isKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)) {
componentDecoratorObject = sourceFileOrClassDeclaration;
}
else if (sourceFileOrClassDeclaration.isKind(ts_morph_1.SyntaxKind.ClassDeclaration)) {
componentDecoratorObject = (0, get_component_decorator_object_1.GetComponentDecoratorObject)(sourceFileOrClassDeclaration);
}
else {
componentDecoratorObject = (0, get_component_decorator_object_1.GetComponentDecoratorObject)((0, get_component_class_1.GetComponentClass)(sourceFileOrClassDeclaration));
}
const importName = typeof componentImport === 'string' ? componentImport : (_a = componentImport.namedImport) !== null && _a !== void 0 ? _a : componentImport.name;
if ((0, type_import_1.IsTypeImport)(componentImport)) {
(0, coerce_imports_1.CoerceImports)(sourceFile, (0, type_import_1.TypeImportToImportStructure)(componentImport));
}
const importsArray = (0, get_coerce_array_literal_form_object_literal_1.GetCoerceArrayLiteralFromObjectLiteral)(componentDecoratorObject, 'imports');
(0, coerce_array_element_1.CoerceArrayElement)(importsArray, importName);
return importsArray;
}
//# sourceMappingURL=coerce-component-import.js.map