JSTQL-JS-Transform/src/index.ts

84 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-09-25 11:45:29 +00:00
import swc, { printSync } from "@swc/core";
2023-09-13 12:45:06 +00:00
import { MatchScript } from "./types";
2023-09-25 11:45:29 +00:00
import { from, to } from "./patterns/patterns";
2023-09-13 12:45:06 +00:00
const PATTERN_PATH = "src/patterns/test.json";
const main = async () => {
2023-09-25 11:45:29 +00:00
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========================",
);
2023-09-13 12:45:06 +00:00
});
2023-09-25 11:45:29 +00:00
});
2023-09-13 12:45:06 +00:00
};
const matchStatements = async (module: swc.Script) => {
2023-09-25 11:45:29 +00:00
let fromLocal = from;
let toLocal = to;
2023-09-13 12:45:06 +00:00
2023-09-25 11:45:29 +00:00
return match(fromLocal, toLocal, module.body);
2023-09-13 12:45:06 +00:00
};
2023-09-25 11:45:29 +00:00
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] == value){
console.log("Found valid key with " + key);
let matchRes = match(from[key], to, value);
if (matchRes == MatchingResults.FULL){
curMatchType = MatchingResults.FULL;
}
}
}
}
return curMatchType;
2023-09-13 12:45:06 +00:00
};
2023-09-25 11:45:29 +00:00
2023-09-13 12:45:06 +00:00
main();