@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
106 lines • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NormalizeDataProperty = NormalizeDataProperty;
exports.NormalizeDataPropertyList = NormalizeDataPropertyList;
exports.NormalizeDataPropertyToPropertySignatureStructure = NormalizeDataPropertyToPropertySignatureStructure;
const utilities_1 = require("@rxap/utilities");
const type_import_1 = require("./type-import");
const write_type_1 = require("./write-type");
function guessType(name) {
switch (name) {
case 'uuid':
case 'name':
return 'string';
case 'icon':
return {
name: 'IconConfig',
moduleSpecifier: '@rxap/utilities',
};
}
if (name.match(/Uuid$/)) {
return 'string';
}
if (name.match(/Name$/)) {
return 'string';
}
if (name.match(/^(is|has)[A-Z]/)) {
return 'boolean';
}
return 'unknown';
}
const notAllowedInVariableNames = [
" ", "!", "\"", "#", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "`", "{", "|", "}", "~"
];
function NormalizeDataProperty(property, defaultType = 'unknown', isArray = false) {
var _a, _b, _c, _d, _e, _f;
let name;
let type = 'unknown';
let isOptional = false;
let source = null;
let memberList = [];
if (typeof property === 'string') {
// name:type
// username:string
const fragments = property.split(':');
name = fragments[0];
type = fragments[1] || type; // convert an empty string to undefined
}
else {
name = property.name;
type = (_a = property.type) !== null && _a !== void 0 ? _a : type;
isArray = (_b = property.isArray) !== null && _b !== void 0 ? _b : isArray;
isOptional = (_c = property.isOptional) !== null && _c !== void 0 ? _c : isOptional;
source = (_d = property.source) !== null && _d !== void 0 ? _d : source;
memberList = NormalizeDataPropertyList(property.memberList, defaultType);
}
if (name.endsWith('[]')) {
isArray = true;
name = name.slice(0, -2);
}
if (name.startsWith('Array<') && name.endsWith('>')) {
isArray = true;
name = name.slice(6, -1);
}
type !== null && type !== void 0 ? type : (type = defaultType);
if (type === 'unknown' || (typeof type === 'object' && type.name === 'unknown')) {
if (defaultType === 'unknown') {
type = guessType(name);
}
else {
type = defaultType;
}
}
if (memberList.length) {
type = type_import_1.TypeNames.Deferred;
}
name = name.replace(/\.\?/g, '.').split('.').join('.?');
if (!isNaN(Number(name[0]))) {
name = `_${name}`;
}
if (notAllowedInVariableNames.some(c => name.includes(c))) {
const leadingUnderscoreCount = (_f = (_e = name.match(/^_*/)) === null || _e === void 0 ? void 0 : _e[0].length) !== null && _f !== void 0 ? _f : 0;
const nameWithoutLeadingUnderscores = name.slice(leadingUnderscoreCount);
name = (0, utilities_1.camelize)(nameWithoutLeadingUnderscores);
name = '_'.repeat(leadingUnderscoreCount) + name;
}
return Object.freeze({
name,
type: (0, type_import_1.NormalizeTypeImport)(type),
isArray,
isOptional,
source,
memberList,
});
}
function NormalizeDataPropertyList(propertyList, defaultType = 'unknown') {
var _a;
return (_a = propertyList === null || propertyList === void 0 ? void 0 : propertyList.map(property => NormalizeDataProperty(property, defaultType))) !== null && _a !== void 0 ? _a : [];
}
function NormalizeDataPropertyToPropertySignatureStructure(property, sourceFile) {
return {
type: (0, write_type_1.WriteType)(property, sourceFile),
name: property.name,
};
}
//# sourceMappingURL=data-property.js.map