2025-07-09 13:26:51 +01:00
|
|
|
|
|
|
|
import assert from 'node:assert';
|
|
|
|
import { getASTAndScope } from './ast/analysis.mjs';
|
|
|
|
|
|
|
|
import { getRequireCallsAndConstantArgs } from './calls.mjs';
|
2025-07-21 17:01:43 +01:00
|
|
|
import { readFileSync ,realpathSync ,mkdirSync} from 'node:fs';
|
|
|
|
import { writeFile } from 'node:fs/promises';
|
2025-07-26 13:44:32 +01:00
|
|
|
import tsc, { Project, SyntaxKind } from 'ts-morph';
|
2025-07-21 17:01:43 +01:00
|
|
|
import {getSliceAndInfoSync} from 'slice-js/src/slice-code/test/helpers/utils.js';
|
|
|
|
import path, { dirname,join } from 'node:path';
|
2025-07-26 13:44:32 +01:00
|
|
|
// import tsc from 'typescript'
|
2025-07-09 13:26:51 +01:00
|
|
|
/**
|
|
|
|
* Call parameter generation
|
|
|
|
*/
|
|
|
|
function main() {
|
|
|
|
const FILE_PATH = './test_src/index.cjs';
|
|
|
|
const { scopeManager, _parsedModAST } = getASTAndScope(FILE_PATH);
|
|
|
|
assert(scopeManager.scopes.length >= 2, "expected atleast global and module scope");
|
|
|
|
assert(scopeManager.scopes[1].type === 'function', "expected the 'module' scope to have function scope");
|
|
|
|
|
|
|
|
const calls = getRequireCallsAndConstantArgs(scopeManager);
|
|
|
|
|
2025-07-21 17:01:43 +01:00
|
|
|
logCallList(calls);
|
2025-07-09 13:26:51 +01:00
|
|
|
|
2025-07-21 17:01:43 +01:00
|
|
|
const writePromises = [];
|
2025-07-09 13:26:51 +01:00
|
|
|
for (const [moduleName, callBox] of calls) {
|
2025-07-21 17:01:43 +01:00
|
|
|
if (!isRelativeModule(moduleName)) { // not relative module
|
2025-07-09 13:26:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const relatedModuleNamePath = join(realpathSync(dirname(FILE_PATH)) ,moduleName);
|
|
|
|
const fileSource = readFileSync(relatedModuleNamePath).toString('utf-8');
|
|
|
|
const {slicedCode} = getSliceAndInfoSync(fileSource, (moduleExports) => {
|
|
|
|
return [...callBox.entries()].flatMap(([methodName, methodArgsList])=>{
|
|
|
|
const methodNameNormed = methodName.substring(1);
|
|
|
|
console.log("Calls for ",methodNameNormed,methodArgsList)
|
2025-07-21 17:01:43 +01:00
|
|
|
return methodArgsList.map(methodArgsList=>{
|
|
|
|
const methodObj = methodNameNormed===''?moduleExports:moduleExports[methodNameNormed];
|
|
|
|
methodObj.apply(moduleExports[methodNameNormed],methodArgsList)
|
|
|
|
});
|
2025-07-09 13:26:51 +01:00
|
|
|
})
|
|
|
|
},relatedModuleNamePath);
|
2025-07-21 17:01:43 +01:00
|
|
|
// console.log(`Sliced code ${moduleName}\n`,slicedCode);
|
|
|
|
const writePath = path.resolve('./dist', moduleName);
|
|
|
|
if(writePath===moduleName){
|
|
|
|
throw Error("Will overwrite!!!!");
|
|
|
|
}
|
|
|
|
mkdirSync(path.dirname(writePath),{recursive: true});
|
|
|
|
console.log(`Writing to`,writePath);
|
2025-07-09 13:26:51 +01:00
|
|
|
|
2025-07-21 17:01:43 +01:00
|
|
|
writePromises.push(writeFile(writePath,slicedCode));
|
2025-07-26 13:44:32 +01:00
|
|
|
|
2025-07-21 17:01:43 +01:00
|
|
|
}
|
2025-07-09 13:26:51 +01:00
|
|
|
|
2025-07-21 17:01:43 +01:00
|
|
|
Promise.all(writePromises).then(p=>{
|
|
|
|
console.log("write finished")
|
|
|
|
}).catch(console.log);
|
2025-07-09 13:26:51 +01:00
|
|
|
}
|
|
|
|
|
2025-07-26 13:44:32 +01:00
|
|
|
class ImportCall{
|
|
|
|
/**
|
|
|
|
* @type {'import'|'importExpr'|'require'}
|
|
|
|
*/
|
|
|
|
importType;
|
|
|
|
/**
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
importSyntax;
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {'import'|'importExpr'|'require'} importType
|
|
|
|
* @param {string} importSyntax
|
|
|
|
*/
|
|
|
|
constructor(importType, importSyntax){
|
|
|
|
this.importSyntax = importSyntax;
|
|
|
|
this.importType = importType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function main2() {
|
|
|
|
const FILE_PATH = './test_src/index.cjs';
|
|
|
|
|
|
|
|
const project = new Project({compilerOptions:{allowJs: true, checkJs: false,}});
|
|
|
|
project.addSourceFileAtPathIfExists(FILE_PATH);
|
|
|
|
|
|
|
|
// const project = tsc.createProgram([FILE_PATH],);
|
|
|
|
const checker = project.getTypeChecker();
|
|
|
|
|
|
|
|
const sourceFile = project.getSourceFile(FILE_PATH)
|
|
|
|
|
|
|
|
const importDecls = sourceFile.getImportStringLiterals()
|
|
|
|
for(const importStringDecl of importDecls){
|
|
|
|
console.log(importStringDecl);
|
|
|
|
const importDecl = importStringDecl.getFirstAncestor();
|
|
|
|
if(importDecl.isKind(SyntaxKind.CallExpression)){
|
|
|
|
// the declaration is callExpression. Verify its based an identifier aliasing import or require
|
|
|
|
const importExpr = importDecl.getExpression();
|
|
|
|
const type = checker.getTypeAtLocation(importExpr);
|
|
|
|
console.log("Type of import expression",checker.compilerObject.resolveName());
|
|
|
|
console.log(importExpr);
|
|
|
|
if(importExpr.isKind(SyntaxKind.Identifier)){
|
|
|
|
// import is a require or import
|
|
|
|
const importName = importExpr.getText();
|
|
|
|
if(importName==='require' || importName==='import'){
|
|
|
|
console.log("Found require/import call",importExpr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}else if(importDecl.isKind(SyntaxKind.ImportDeclaration)){
|
|
|
|
// TODO pending extract the calls.
|
|
|
|
}else{
|
|
|
|
console.error("Unexpected import specifier",SyntaxKind[importDecl]);
|
|
|
|
}
|
|
|
|
const importThing = importStringDecl.getParent()
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(importDecls);
|
|
|
|
}
|
|
|
|
|
2025-07-09 13:26:51 +01:00
|
|
|
if (process.argv[1] === import.meta.filename) {
|
2025-07-26 13:44:32 +01:00
|
|
|
console.log("[SafeImport] started");
|
|
|
|
main2();
|
|
|
|
console.log("done");
|
2025-07-09 13:26:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-07-21 17:01:43 +01:00
|
|
|
function logCallList(calls) {
|
|
|
|
for (const [moduleName, callBoxes] of calls.entries()) {
|
|
|
|
if (isRelativeModule(moduleName)) {
|
|
|
|
console.log('Importing', moduleName, callBoxes);
|
|
|
|
} else {
|
|
|
|
console.log(`Module "${moduleName}" - System module. FIXME skipping`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(`Call List`, calls);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isRelativeModule(moduleName) {
|
|
|
|
return moduleName.startsWith('.');
|
|
|
|
}
|