@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
73 lines • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IsWriteTypeOptions = IsWriteTypeOptions;
exports.WriteStringType = WriteStringType;
exports.WriteType = WriteType;
const coerce_imports_1 = require("./coerce-imports");
const type_import_1 = require("./type-import");
function IsWriteTypeOptions(value) {
return value && typeof value === 'object' && value.type;
}
/**
* Uses the CodeBlockWriter to write the type to the AST thereby the value of the property type is evaluated
* to determine the value that should be written to the AST
* @param type
* @param w
* @constructor
*/
function WriteStringType(type, w) {
switch (type) {
case 'date':
w.write('Date');
break;
case 'uuid':
w.write('string');
break;
default:
w.write(type);
break;
}
}
function WriteType(propertyOrType, sourceFile) {
var _a;
let isArray = false;
let type;
if (IsWriteTypeOptions(propertyOrType)) {
isArray = (_a = propertyOrType.isArray) !== null && _a !== void 0 ? _a : isArray;
type = propertyOrType.type;
}
else {
type = propertyOrType;
}
if ((0, type_import_1.IsTypeImport)(type)) {
if (sourceFile) {
if ((0, type_import_1.RequiresTypeImport)(type)) {
(0, coerce_imports_1.CoerceImports)(sourceFile, (0, type_import_1.TypeImportToImportStructure)(type));
}
}
else {
console.warn('WriteType :: No source file provided to coerce imports');
}
}
return (w) => {
if (isArray) {
w.write('Array<');
}
if (typeof type === 'string') {
WriteStringType(type, w);
}
else if (typeof type === 'function') {
type(w);
}
else if ((0, type_import_1.IsTypeImport)(type)) {
WriteStringType(type.name, w);
}
else {
throw new Error('Invalid type');
}
if (isArray) {
w.write('>');
}
};
}
//# sourceMappingURL=write-type.js.map