[add] require support
This commit is contained in:
@@ -5,7 +5,7 @@ import { writeFile } from 'node:fs/promises';
|
|||||||
import { Project } from 'ts-morph';
|
import { Project } from 'ts-morph';
|
||||||
import { getSliceAndInfoSync } from 'slice-js/src/slice-code/test/helpers/utils.js';
|
import { getSliceAndInfoSync } from 'slice-js/src/slice-code/test/helpers/utils.js';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { getImportCallsAndArgumentTypes } from './tsCalls.mjs';
|
import { getImportCallsAndArgumentTypes, isNodeModule, isRelativeModule, logCallList } from './tsCalls.mjs';
|
||||||
import { wpCompress } from './bundle/index.mjs';
|
import { wpCompress } from './bundle/index.mjs';
|
||||||
import { LibraryTypesRecorder } from './libcalls.mjs';
|
import { LibraryTypesRecorder } from './libcalls.mjs';
|
||||||
/**
|
/**
|
||||||
@@ -60,7 +60,7 @@ export async function sliceAndWriteCalls(calls, FILE_PATH) {
|
|||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
// const FILE_PATH = './test_src/index.cjs';
|
// const FILE_PATH = './test_src/index.cjs';
|
||||||
const FILE_PATH = './test_src/index.mjs';
|
const FILE_PATH = './test_src/index.cjs';
|
||||||
|
|
||||||
const project = new Project({ compilerOptions: { allowJs: true, checkJs: false, } });
|
const project = new Project({ compilerOptions: { allowJs: true, checkJs: false, } });
|
||||||
project.addSourceFileAtPathIfExists(FILE_PATH);
|
project.addSourceFileAtPathIfExists(FILE_PATH);
|
||||||
@@ -77,7 +77,7 @@ function main() {
|
|||||||
|
|
||||||
const callMap = calls.generateAllArgumentsForRecordedCalls();
|
const callMap = calls.generateAllArgumentsForRecordedCalls();
|
||||||
|
|
||||||
logCallList(callMap);
|
logCallList(callMap,FILE_PATH);
|
||||||
sliceAndWriteCalls(callMap, FILE_PATH).then(() => {
|
sliceAndWriteCalls(callMap, FILE_PATH).then(() => {
|
||||||
console.log("Slicing and writing calls done");
|
console.log("Slicing and writing calls done");
|
||||||
});
|
});
|
||||||
@@ -86,35 +86,6 @@ function main() {
|
|||||||
if (process.argv[1] === import.meta.filename) {
|
if (process.argv[1] === import.meta.filename) {
|
||||||
console.log("[SafeImport] started");
|
console.log("[SafeImport] started");
|
||||||
main();
|
main();
|
||||||
// console.log("done");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function logCallList(calls) {
|
|
||||||
console.log(`[Call Log] Call List for ${calls.size} modules`);
|
|
||||||
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);
|
|
||||||
console.log(`[Call Log] End List for ${calls.size} modules`);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function isRelativeModule(moduleName) {
|
|
||||||
return moduleName.startsWith('.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* True if an inbuilt Node.js module.
|
|
||||||
* @param {string} moduleName
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
function isNodeModule(moduleName) {
|
|
||||||
if(moduleName.startsWith('node:')) return true;
|
|
||||||
const nodeModules = ['fs', 'fs/promises', 'path', 'http', 'https', 'os', 'crypto']
|
|
||||||
return nodeModules.includes(moduleName);
|
|
||||||
}
|
|
@@ -52,9 +52,29 @@ export function getImportCallsAndArgumentTypes(importDecls, checker, mainFilePat
|
|||||||
// this is a variable declaration
|
// this is a variable declaration
|
||||||
const varDecl = parent;
|
const varDecl = parent;
|
||||||
const varName = varDecl.getName();
|
const varName = varDecl.getName();
|
||||||
|
const varDecls = varDecl.getNameNode();
|
||||||
|
// default import
|
||||||
|
if( varDecls.isKind(SyntaxKind.Identifier)) {
|
||||||
|
recordImportedIdentifierUsage(varDecls, mainFilePath, libraryCallsRecorder, importStringDecl, true);
|
||||||
|
}else if(varDecls.isKind(SyntaxKind.ObjectBindingPattern)) {
|
||||||
|
const destructuredElements = varDecls.getElements();
|
||||||
|
for (const destructuredElement of destructuredElements) {
|
||||||
|
const destructuredElementName = destructuredElement.getNameNode();
|
||||||
|
if (destructuredElementName.isKind(SyntaxKind.Identifier)) {
|
||||||
|
recordImportedIdentifierUsage(destructuredElementName, mainFilePath, libraryCallsRecorder, importStringDecl);
|
||||||
|
} else if (destructuredElementName.isKind(SyntaxKind.ObjectBindingPattern)) {
|
||||||
|
// TODO handle object binding pattern
|
||||||
|
console.warn("Nested binding pattern not handled yet", destructuredElementName.getText());
|
||||||
|
} else {
|
||||||
|
console.error("Unexpected destructured element", destructuredElementName.getText());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
console.log("Variable name", varName);
|
console.log("Variable name", varName);
|
||||||
// check if declaration is identifier or object pattern
|
// check if declaration is identifier or object pattern
|
||||||
}
|
}
|
||||||
|
console.log("Require arguments. Skipping");
|
||||||
|
continue;
|
||||||
throw Error("Not implemented yet");
|
throw Error("Not implemented yet");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,4 +242,36 @@ function recordImportedIdentifierUsage(importNode, mainFilePath, libraryCallsRec
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {*} calls
|
||||||
|
* @param {string} fileName
|
||||||
|
*/
|
||||||
|
export function logCallList(calls,fileName) {
|
||||||
|
console.log(`--- [Call Log List: ${fileName}] ---`)
|
||||||
|
console.log(`[Call Log] Call List for ${calls.size} modules`);
|
||||||
|
for (const [moduleName, callBoxes] of calls.entries()) {
|
||||||
|
if (isRelativeModule(moduleName) || isNodeModule(moduleName)) {
|
||||||
|
console.log(`Local/sys Module "${moduleName}" - System module. FIXME skipping`);
|
||||||
|
} else {
|
||||||
|
console.log('Library Module', moduleName, callBoxes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log(`Call List`, calls);
|
||||||
|
console.log(`--- [Call Log End List: ${fileName}] ---`);
|
||||||
|
|
||||||
|
}
|
||||||
|
export function isRelativeModule(moduleName) {
|
||||||
|
return moduleName.startsWith('.');
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* True if an inbuilt Node.js module.
|
||||||
|
* @param {string} moduleName
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export function isNodeModule(moduleName) {
|
||||||
|
if (moduleName.startsWith('node:')) return true;
|
||||||
|
const nodeModules = ['fs', 'fs/promises', 'path', 'http', 'https', 'os', 'crypto'];
|
||||||
|
return nodeModules.includes(moduleName);
|
||||||
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user