From 0e7a0193089cbe0e78f2466fa8da6a3101792f90 Mon Sep 17 00:00:00 2001 From: polsevev Date: Mon, 25 Sep 2023 13:45:29 +0200 Subject: [PATCH] Something --- src/index.ts | 118 ++++++++++++++++++++------------------- src/patterns/patterns.ts | 8 +-- src/test_files/simple.js | 2 +- src/types.ts | 11 ++-- 4 files changed, 72 insertions(+), 67 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8b2c122..1611098 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,81 +1,83 @@ -import swc from "@swc/core"; +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); + 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!"); - console.log( - "=====================\nCurrent file to be transformed : \n" + - inputFile + - "\n====================" - ); - swc.parseFile("src/test_files/simple.js", { - syntax: "ecmascript", - jsx: false, + const hello = "lol"; - target: "es2022", + console.log( + "=====================\nCurrent file to be transformed : \n" + + inputFile + + "\n====================", + ); + swc.parseFile("src/test_files/simple.js", { + syntax: "ecmascript", + jsx: false, - isModule: false, - }).then((module) => { - //console.log(module); - // swc.print(module).then((o: swc.Output) => { - // console.log(o); - // }); + target: "es2022", - console.log(module.body); + isModule: false, + }).then((module) => { + //console.log(module); + // swc.print(module).then((o: swc.Output) => { + // console.log(o); + // }); - matchStatements(module).then((a) => { - console.log( - "================\nOutput code: \n" + - a.code + - "\n========================" - ); - }); +// console.log(module.body); + + matchStatements(module).then((a) => { + console.log( + "================\nOutput code: \n" + + a + + "\n========================", + ); }); + }); }; const matchStatements = async (module: swc.Script) => { - const patternFile = Bun.file(PATTERN_PATH); - const [from, to]: [MatchScript, MatchScript] = JSON.parse( - await patternFile.text() - ); - return await swc.printSync(match(from, to, module)); + let fromLocal = from; + let toLocal = to; + + return match(fromLocal, toLocal, module.body); }; -const match = (from: any, to: any, module: swc.Script): swc.Script => { - console.log(to); - console.log(module); - console.log(from); +enum MatchingResults{ + FULL, + PARTIAL, + SINGLE, + NONE +} - for (const obj of module.body) { - let allPresent = true; - for (const key in obj) { - if (!(key in from)) { - allPresent = false; - } - } - if (allPresent) { - console.log("Found first match!"); - for (const [key, val] of Object.entries(obj)) { - match(from["key"], to, val); - } - } - } +const match = (from: any, to: any, module: swc.Statement[]): any => { + + let curMatchType = MatchingResults.NONE; - return module; + 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; }; -const matchAndReplace = ( - statement: swc.Statement, - from: Object, - to: Object -) => { - for (const [key, value] of Object.entries(from)) { - } -}; + main(); diff --git a/src/patterns/patterns.ts b/src/patterns/patterns.ts index ff24979..8d2ff76 100644 --- a/src/patterns/patterns.ts +++ b/src/patterns/patterns.ts @@ -1,5 +1,5 @@ import { Statement, VariableDeclarator } from "@swc/types"; -import { MatchStatement } from "../types"; +import { MatchStatement, WildCardType } from "../types"; export const from: MatchStatement = { type: "VariableDeclaration", @@ -8,7 +8,7 @@ export const from: MatchStatement = { end: 13, ctxt: 0, }, - kind: "var", + kind: WildCardType.ANYTHING, declare: false, declarations: [ { @@ -58,7 +58,7 @@ export const to: VariableDeclarator[] = [ end: 6, ctxt: 2, }, - value: "a", + value: "Inserted_By_Program", optional: false, }, init: { @@ -69,7 +69,7 @@ export const to: VariableDeclarator[] = [ ctxt: 0, }, value: 100, - raw: "100", + raw: "InsertedValue!", }, definite: false, }, diff --git a/src/test_files/simple.js b/src/test_files/simple.js index 1ded49b..f4b8bbf 100644 --- a/src/test_files/simple.js +++ b/src/test_files/simple.js @@ -1 +1 @@ -var a = 100 + 100; +var oldVariableName = "Old_value"; diff --git a/src/types.ts b/src/types.ts index 4a326ed..92dc2c8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,6 +22,7 @@ import { ThrowStatement, TryStatement, VariableDeclaration, + VariableDeclarationKind, WhileStatement, WithStatement, } from "@swc/core"; @@ -52,8 +53,8 @@ type MatchDeclaration = | MatchFunctionDeclaration | MatchVariableDeclaration; -export enum Transformation { - ANYTHING, +export enum WildCardType { + ANYTHING, } export interface MatchScript extends Script {} @@ -95,11 +96,13 @@ export interface MatchExpressionStatement extends ExpressionStatement {} export interface matchIfStatement extends IfStatement {} export interface matchBinaryExpression extends BlockStatement { - __stmts: Transformation; + __stmts: WildCardType; } export interface MatchClassDeclaration extends ClassDeclaration {} export interface MatchFunctionDeclaration extends FunctionDeclaration {} -export interface MatchVariableDeclaration extends VariableDeclaration {} +export interface MatchVariableDeclaration extends Omit { + kind : VariableDeclarationKind | WildCardType +}