feat: update package.json to include mocha and chai for testing, add ansi-colors and fastest-levenshtein dependencies

refactor: modify sliceAndWriteCalls function to accept folderPath parameter and update driver function to use it

fix: enhance isNodeModule function to utilize builtinModules for better module detection

chore: remove unused constants file from utils

test: clean up instantiation.spec.js by removing commented assertions
This commit is contained in:
2025-08-07 15:33:52 +01:00
parent 91bf93e3f6
commit 393e22bd45
6 changed files with 861 additions and 23 deletions

832
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -5,13 +5,14 @@
"type": "module",
"scripts": {
"start": "node src/index.mjs",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha test"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"all-the-package-repos": "^2.0.2306",
"ansi-colors": "^4.1.3",
"csv": "^6.3.11",
"download-counts": "^2.20250701.0",
"eslint-scope": "^8.4.0",
@@ -19,12 +20,14 @@
"esquery": "^1.6.0",
"esrecurse": "^4.3.0",
"estraverse": "^5.3.0",
"fastest-levenshtein": "^1.0.16",
"find-package-json": "^1.2.0",
"git-client": "^1.9.3",
"glob": "^11.0.3",
"jalangi2": "^0.2.6",
"semver": "^7.7.2",
"set.prototype.intersection": "^1.1.7",
"slice-js": "file:/home/atreyab/Documents/Docs/SlicingImport/repos-js/slice-js",
"ts-morph": "^26.0.0",
"typescript": "^5.8.3",
"webpack": "^5.99.9"
@@ -34,6 +37,8 @@
"@types/eslint-scope": "^8.3.0",
"@types/estree": "^1.0.8",
"@types/node": "^24.0.0",
"classnames": "^2.5.1"
"chai": "^5.2.1",
"classnames": "^2.5.1",
"mocha": "^11.7.1"
}
}

View File

@@ -11,8 +11,9 @@ import { LibraryTypesRecorder } from './libcalls.mjs';
/**
*
* @param {ReturnType<LibraryTypesRecorder['generateAllArgumentsForRecordedCalls']>} calls
* @param {string} folderPath
*/
export async function sliceAndWriteCalls(calls) {
export async function sliceAndWriteCalls(calls, folderPath) {
const writePromises = [];
for (const [moduleName, callBox] of calls) {
@@ -24,8 +25,9 @@ export async function sliceAndWriteCalls(calls) {
// const relatedModuleNamePath = import.meta.resolve(moduleName);
// console.log(`Related module path`, relatedModuleNamePath);
const relatedModuleNamePath = await wpCompress(moduleName)
console.log("[wp] Compressing module", moduleName);
// throw Error("Module slicing not implemented yet");
const relatedModuleNamePath = await wpCompress(moduleName,folderPath );
const fileSource = readFileSync(relatedModuleNamePath).toString('utf-8');
// continue; // TODO - handle relative modules
const { slicedCode } = getSliceAndInfoSync(fileSource, (moduleExports) => {
@@ -66,11 +68,12 @@ export async function sliceAndWriteCalls(calls) {
}).catch(console.log);
}
// is-glob WORKED
/**
*
* @param {string} filePath
*/
function driver(folderPath = './test_src/anymatch') {
function driver(folderPath = './candidates/braces') {
// const FILE_PATH = './test_src/index.cjs';
const project = new Project({ compilerOptions: { allowJs: true, checkJs: false, } });
@@ -96,15 +99,19 @@ function driver(folderPath = './test_src/anymatch') {
const callMap = libraryTypesRecorder.generateAllArgumentsForRecordedCalls();
logCallList(callMap, 'FakeModuleName');
sliceAndWriteCalls(callMap).then(() => {
logCallList(callMap, folderPath);
sliceAndWriteCalls(callMap, folderPath).then(() => {
console.log("Slicing and writing calls done");
});
}
if (process.argv[1] === import.meta.filename) {
if(process.argv.length >2 && process.argv[2] !== '') {
console.log(`[SafeImport] started ${process.argv[2]}`);
driver(process.argv[2]);
}else{
console.log("[SafeImport] started");
driver();
driver();}
}

View File

@@ -2,6 +2,7 @@
import path from 'path';
import tsm, { Identifier, ImportSpecifier, StringLiteral, SyntaxKind, ts, } from 'ts-morph';
import { LibraryTypesRecorder } from './libcalls.mjs';
import {builtinModules} from 'node:module'
/**
*
@@ -50,6 +51,7 @@ export function getImportCallsAndArgumentTypes(importDecls, checker, mainFilePat
// const importArgs = importDecl.getArguments();
const parent = importDecl.getParent();
console.log("Parent of import call", parent?.getKindName(), parent?.getText());
if (parent?.isKind(SyntaxKind.VariableDeclaration)) {
// this is a variable declaration
const varDecl = parent;
@@ -76,9 +78,6 @@ export function getImportCallsAndArgumentTypes(importDecls, checker, mainFilePat
console.log("Variable name", varName);
// check if declaration is identifier or object pattern
}
console.log("Require arguments. Skipping");
continue;
throw Error("Not implemented yet");
}
}
@@ -298,8 +297,11 @@ export function isRelativeModule(moduleName) {
* @returns
*/
export function isNodeModule(moduleName) {
if (moduleName.startsWith('node:')) return true;
const nodeModules = ['fs', 'fs/promises', 'path', 'http', 'https', 'os', 'crypto','assert'];
return nodeModules.includes(moduleName);
if (builtinModules.includes(moduleName) ) return true;
if (moduleName.startsWith('node:')) {
return builtinModules.includes(moduleName.substring(5));// strip node: prefix
}
// const nodeModules = ['fs', 'fs/promises', 'path', 'http', 'https', 'os', 'crypto','assert'];
// return nodeModules.includes(moduleName);
}

View File

@@ -1,3 +0,0 @@
export const prependString = `(function(exports, require, module, __filename, __dirname) {\n`
export const appendString = `\n});`

View File

@@ -113,9 +113,6 @@ describe('Instantiation tests',function () {
console.log(ans);
assert.isArray(ans);
assert.isArray([10, 'a'], ans);
// assert.isBoolean(ans.x);
// assert.isObject(ans.parent);
// assert.isNumber(ans.data);
})