Files
safeImport/src/libcalls.mjs

117 lines
3.9 KiB
JavaScript
Raw Normal View History

2025-07-09 13:26:51 +01:00
//@ts-check
/**
* @typedef {import('estree').Literal["value"]} GenericLiteralType
*/
2025-08-01 20:19:24 +01:00
import tsm, { Type } from 'ts-morph';
import { simpleFaker } from '@faker-js/faker'
export class LibraryTypesRecorder {
2025-07-09 13:26:51 +01:00
/**
* @type {Map<string,Map<string,Type[][]>>}
2025-07-09 13:26:51 +01:00
*/
#calls = new Map();
/**
* @type {tsm.TypeChecker} checker
2025-07-09 13:26:51 +01:00
*/
checker;
2025-08-01 20:19:24 +01:00
/**
*
* @param {tsm.TypeChecker} checker
2025-08-01 20:19:24 +01:00
*/
constructor(checker) {
this.checker = checker;
}
2025-08-01 20:19:24 +01:00
/**
*
* @param {string} moduleName
* @param {string} libraryFunctionSegment
* @param {Type[]} argumentsCalled
*/
pushToMap(moduleName, libraryFunctionSegment, argumentsCalled) {
const modulePortion = this.#calls.get(moduleName) ?? new Map();
2025-08-01 20:19:24 +01:00
const defArgs = modulePortion.get(libraryFunctionSegment) ?? [];
defArgs.push(argumentsCalled);
modulePortion.set(libraryFunctionSegment, defArgs);
2025-08-01 20:19:24 +01:00
this.#calls.set(moduleName, modulePortion);
}
get calls() {
2025-08-01 20:19:24 +01:00
return this.#calls;
}
generateAllArgumentsForRecordedCalls() {
/**
* @type {Map<string,Map<string,(GenericLiteralType|null|undefined|{})[][]>>}
*/
2025-08-01 20:19:24 +01:00
const callMap = new Map();
for (const [moduleName, modulePortion] of this.#calls) {
2025-08-01 20:19:24 +01:00
/**
* @type {Map<string,(GenericLiteralType|null|undefined|{})[][]>}
*/
const moduleCallMap = new Map();// todo refactor
for (const [libraryFunctionSegment, argsList] of modulePortion) {
// const argsForFunctionSimple = argsList.map(args => args.map(arg => this.instantiateType(arg)));
const argsForFunction = argsList.flatMap(args => simpleFaker.helpers.multiple(()=> args.map(arg => this.instantiateFakerOnType(arg))));
moduleCallMap.set(libraryFunctionSegment, argsForFunction);
2025-08-01 20:19:24 +01:00
}
callMap.set(moduleName, moduleCallMap);
2025-08-01 20:19:24 +01:00
}
return callMap;
}
/**
*
* @param {Type} type
*/
instantiateFakerOnType(type) {
const literalValue = type.getLiteralValue();
if(type.isBooleanLiteral()){
return type.getText() === 'true';
} else if (literalValue !== undefined) {
return literalValue;
} else if (type.isUndefined()) {
return undefined;
} else if (type.isString()) {
return simpleFaker.string.alphanumeric();
} else if (type.isNumber()) {
return simpleFaker.number.int();
} else if (type.isBoolean()) {
return simpleFaker.datatype.boolean();
} else if (type.isArray()) {
return []// TODO - handle arrays;
} else if (type.isObject()) {
const newObj = {};
for (const prop of type.getProperties()) {
const propName = prop.getName();
const declarations = prop.getDeclarations();
let propType = prop.getDeclaredType();
if (declarations.length !== 1) {
console.warn("Multiple declarations for property", propName, "in type", type.getText());
} else {
propType = this.checker.getTypeOfSymbolAtLocation(prop, declarations[0]);
}
newObj[propName] = this.instantiateFakerOnType(propType);
}
// TODO - handle functions
return newObj;
} else {
console.warn("Unknown type to instantiate", type.getText());
if (type.isAny()) {
return simpleFaker.helpers.arrayElement([
simpleFaker.string.sample(),
simpleFaker.number.int(),
simpleFaker.datatype.boolean(),
{},
[]
]);
}
return undefined;
}
2025-08-01 20:19:24 +01:00
}
2025-07-09 13:26:51 +01:00
}