Smack my ass and call me larry the cable guy
This commit is contained in:
parent
ddc92a2b6d
commit
4b03caa360
4 changed files with 72 additions and 67 deletions
118
src/index.ts
118
src/index.ts
|
@ -1,81 +1,83 @@
|
||||||
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";
|
||||||
|
|
||||||
const main = async () => {
|
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(
|
const hello = "lol";
|
||||||
"=====================\nCurrent file to be transformed : \n" +
|
|
||||||
inputFile +
|
|
||||||
"\n===================="
|
|
||||||
);
|
|
||||||
swc.parseFile("src/test_files/simple.js", {
|
|
||||||
syntax: "ecmascript",
|
|
||||||
jsx: false,
|
|
||||||
|
|
||||||
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,
|
target: "es2022",
|
||||||
}).then((module) => {
|
|
||||||
//console.log(module);
|
|
||||||
// swc.print(module).then((o: swc.Output) => {
|
|
||||||
// console.log(o);
|
|
||||||
// });
|
|
||||||
|
|
||||||
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(module.body);
|
||||||
console.log(
|
|
||||||
"================\nOutput code: \n" +
|
matchStatements(module).then((a) => {
|
||||||
a.code +
|
console.log(
|
||||||
"\n========================"
|
"================\nOutput code: \n" +
|
||||||
);
|
a +
|
||||||
});
|
"\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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (allPresent) {
|
|
||||||
console.log("Found first match!");
|
|
||||||
for (const [key, val] of Object.entries(obj)) {
|
|
||||||
match(from["key"], to, val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
main();
|
||||||
|
|
|
@ -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,
|
||||||
},
|
},
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
var a = 100 + 100;
|
var oldVariableName = "Old_value";
|
||||||
|
|
11
src/types.ts
11
src/types.ts
|
@ -22,6 +22,7 @@ import {
|
||||||
ThrowStatement,
|
ThrowStatement,
|
||||||
TryStatement,
|
TryStatement,
|
||||||
VariableDeclaration,
|
VariableDeclaration,
|
||||||
|
VariableDeclarationKind,
|
||||||
WhileStatement,
|
WhileStatement,
|
||||||
WithStatement,
|
WithStatement,
|
||||||
} from "@swc/core";
|
} from "@swc/core";
|
||||||
|
@ -52,8 +53,8 @@ type MatchDeclaration =
|
||||||
| MatchFunctionDeclaration
|
| MatchFunctionDeclaration
|
||||||
| MatchVariableDeclaration;
|
| MatchVariableDeclaration;
|
||||||
|
|
||||||
export enum Transformation {
|
export enum WildCardType {
|
||||||
ANYTHING,
|
ANYTHING,
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MatchScript extends Script {}
|
export interface MatchScript extends Script {}
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue