JSTQL-JS-Transform/src/index.ts
2024-05-28 22:25:34 +02:00

84 lines
1.8 KiB
TypeScript

import swc, { printSync } from "@swc/core";
import { MatchScript } from "./types";
import { from, to } from "./patterns/patterns";
const PATTERN_PATH = "src/patterns/test.json";
const main = async () => {
console.log(Bun.version);
let inputFile = await Bun.file("src/test_files/simple.js").text();
console.log("Hello!");
const hello = "lol";
console.log(
"=====================\nCurrent file to be transformed : \n" +
inputFile +
"\n====================",
);
swc.parseFile("src/test_files/simple.js", {
syntax: "ecmascript",
jsx: false,
target: "es2022",
isModule: false,
}).then((module) => {
//console.log(module);
// swc.print(module).then((o: swc.Output) => {
// console.log(o);
// });
// console.log(module.body);
matchStatements(module).then((a) => {
console.log(
"================\nOutput code: \n" +
a +
"\n========================",
);
});
});
};
const matchStatements = async (module: swc.Script) => {
let fromLocal = from;
let toLocal = to;
let key: "body" = "body";
module[key]
return match(fromLocal, toLocal, module.body);
};
enum MatchingResults{
FULL,
PARTIAL,
SINGLE,
NONE
}
const match = (from: any, to: any, module: swc.Statement[]): any => {
let curMatchType = MatchingResults.NONE;
for (const [key, value] of Object.entries(module)){
if (from[key] && key != "span"){
console.log(from[key] + " == " + value);
if (from[key as any] == value){
console.log("Found valid key with " + key);
let matchRes = match(from[key], to, value);
if (matchRes == MatchingResults.FULL){
curMatchType = MatchingResults.FULL;
}
}
}
}
return curMatchType;
};
main();