Something

This commit is contained in:
Rolf Martin Glomsrud 2023-09-25 13:45:29 +02:00
parent ddc92a2b6d
commit 0e7a019308
4 changed files with 72 additions and 67 deletions

View file

@ -1,5 +1,6 @@
import swc from "@swc/core"; import swc, { printSync } from "@swc/core";
import { MatchScript } from "./types"; import { MatchScript } from "./types";
import { from, to } from "./patterns/patterns";
const PATTERN_PATH = "src/patterns/test.json"; const PATTERN_PATH = "src/patterns/test.json";
@ -7,11 +8,14 @@ const main = async () => {
console.log(Bun.version); console.log(Bun.version);
let inputFile = await Bun.file("src/test_files/simple.js").text(); let inputFile = await Bun.file("src/test_files/simple.js").text();
console.log("Hello!");
const hello = "lol";
console.log( console.log(
"=====================\nCurrent file to be transformed : \n" + "=====================\nCurrent file to be transformed : \n" +
inputFile + inputFile +
"\n====================" "\n====================",
); );
swc.parseFile("src/test_files/simple.js", { swc.parseFile("src/test_files/simple.js", {
syntax: "ecmascript", syntax: "ecmascript",
@ -26,56 +30,54 @@ const main = async () => {
// console.log(o); // console.log(o);
// }); // });
console.log(module.body); // console.log(module.body);
matchStatements(module).then((a) => { matchStatements(module).then((a) => {
console.log( console.log(
"================\nOutput code: \n" + "================\nOutput code: \n" +
a.code + a +
"\n========================" "\n========================",
); );
}); });
}); });
}; };
const matchStatements = async (module: swc.Script) => { const matchStatements = async (module: swc.Script) => {
const patternFile = Bun.file(PATTERN_PATH); let fromLocal = from;
const [from, to]: [MatchScript, MatchScript] = JSON.parse( let toLocal = to;
await patternFile.text()
); return match(fromLocal, toLocal, module.body);
return await swc.printSync(match(from, to, module));
}; };
const match = (from: any, to: any, module: swc.Script): swc.Script => { enum MatchingResults{
console.log(to); FULL,
console.log(module); PARTIAL,
console.log(from); SINGLE,
NONE
}
for (const obj of module.body) { const match = (from: any, to: any, module: swc.Statement[]): any => {
let allPresent = true;
for (const key in obj) { let curMatchType = MatchingResults.NONE;
if (!(key in from)) {
allPresent = false; 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;
} }
}
if (allPresent) {
console.log("Found first match!");
for (const [key, val] of Object.entries(obj)) {
match(from["key"], to, val);
} }
} }
} }
return module; return curMatchType;
}; };
const matchAndReplace = (
statement: swc.Statement,
from: Object,
to: Object
) => {
for (const [key, value] of Object.entries(from)) {
}
};
main(); main();

View file

@ -1,5 +1,5 @@
import { Statement, VariableDeclarator } from "@swc/types"; import { Statement, VariableDeclarator } from "@swc/types";
import { MatchStatement } from "../types"; import { MatchStatement, WildCardType } from "../types";
export const from: MatchStatement = { export const from: MatchStatement = {
type: "VariableDeclaration", type: "VariableDeclaration",
@ -8,7 +8,7 @@ export const from: MatchStatement = {
end: 13, end: 13,
ctxt: 0, ctxt: 0,
}, },
kind: "var", kind: WildCardType.ANYTHING,
declare: false, declare: false,
declarations: [ declarations: [
{ {
@ -58,7 +58,7 @@ export const to: VariableDeclarator[] = [
end: 6, end: 6,
ctxt: 2, ctxt: 2,
}, },
value: "a", value: "Inserted_By_Program",
optional: false, optional: false,
}, },
init: { init: {
@ -69,7 +69,7 @@ export const to: VariableDeclarator[] = [
ctxt: 0, ctxt: 0,
}, },
value: 100, value: 100,
raw: "100", raw: "InsertedValue!",
}, },
definite: false, definite: false,
}, },

View file

@ -1 +1 @@
var a = 100 + 100; var oldVariableName = "Old_value";

View file

@ -22,6 +22,7 @@ import {
ThrowStatement, ThrowStatement,
TryStatement, TryStatement,
VariableDeclaration, VariableDeclaration,
VariableDeclarationKind,
WhileStatement, WhileStatement,
WithStatement, WithStatement,
} from "@swc/core"; } from "@swc/core";
@ -52,7 +53,7 @@ type MatchDeclaration =
| MatchFunctionDeclaration | MatchFunctionDeclaration
| MatchVariableDeclaration; | MatchVariableDeclaration;
export enum Transformation { export enum WildCardType {
ANYTHING, ANYTHING,
} }
@ -95,11 +96,13 @@ export interface MatchExpressionStatement extends ExpressionStatement {}
export interface matchIfStatement extends IfStatement {} export interface matchIfStatement extends IfStatement {}
export interface matchBinaryExpression extends BlockStatement { export interface matchBinaryExpression extends BlockStatement {
__stmts: Transformation; __stmts: WildCardType;
} }
export interface MatchClassDeclaration extends ClassDeclaration {} export interface MatchClassDeclaration extends ClassDeclaration {}
export interface MatchFunctionDeclaration extends FunctionDeclaration {} export interface MatchFunctionDeclaration extends FunctionDeclaration {}
export interface MatchVariableDeclaration extends VariableDeclaration {} export interface MatchVariableDeclaration extends Omit<VariableDeclaration, "kind"> {
kind : VariableDeclarationKind | WildCardType
}