starting to be something xDDDDD
This commit is contained in:
parent
e906095363
commit
8cb01d1bd0
5 changed files with 2361 additions and 2226 deletions
4465
package-lock.json
generated
4465
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -16,6 +16,7 @@
|
||||||
"@babel/generator": "^7.23.0",
|
"@babel/generator": "^7.23.0",
|
||||||
"@babel/parser": "^7.23.0",
|
"@babel/parser": "^7.23.0",
|
||||||
"@babel/traverse": "^7.23.0",
|
"@babel/traverse": "^7.23.0",
|
||||||
|
"@types/babel-traverse": "^6.25.10",
|
||||||
"babel": "^6.23.0",
|
"babel": "^6.23.0",
|
||||||
"bun": "^1.0.4",
|
"bun": "^1.0.4",
|
||||||
"ts-node": "^10.9.1"
|
"ts-node": "^10.9.1"
|
||||||
|
|
22
src/index.ts
22
src/index.ts
|
@ -1,10 +1,11 @@
|
||||||
import * as babelparser from "../babel/packages/babel-parser";
|
import * as babelparser from "../babel/packages/babel-parser";
|
||||||
|
import { parseApplicableTo } from "./parser/parse";
|
||||||
|
import { TransformRecipe, transform } from "./transform/transform";
|
||||||
/*
|
/*
|
||||||
proposal await_to_promise {
|
proposal await_to_promise {
|
||||||
applicable to {
|
applicable to {
|
||||||
let a = await b();
|
let a = await b();
|
||||||
console.log(b);
|
<<CONSUME>>
|
||||||
}
|
}
|
||||||
|
|
||||||
transform to {
|
transform to {
|
||||||
|
@ -14,13 +15,22 @@ proposal await_to_promise {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const transformExample:TransformRecipe = {
|
||||||
|
applicableTo: `let a = await b();<<REST_BLOCK:rest>>`,
|
||||||
|
consumeBlock: true,
|
||||||
|
identifiers: ["b", "a", "rest"],
|
||||||
|
transformTo: "b().then((a) => {<<REST_BLOCK:rest>>})"
|
||||||
|
}
|
||||||
|
const code = "let a = await b(); console.log(a);"
|
||||||
|
|
||||||
const main = (
|
const main = (): void => {
|
||||||
|
transform(transformExample, code);
|
||||||
|
|
||||||
): void => {};
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
50
src/parser/parse.ts
Normal file
50
src/parser/parse.ts
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
|
||||||
|
// This needs to support multiple commands in future
|
||||||
|
|
||||||
|
export interface Command{
|
||||||
|
commandName: string,
|
||||||
|
commandIdentifier: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ApplicableToResult{
|
||||||
|
commands: Command[],
|
||||||
|
applicableTo: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseApplicableTo(applicableTo:string) :ApplicableToResult {
|
||||||
|
|
||||||
|
let applicableToIter = applicableTo[Symbol.iterator]();
|
||||||
|
|
||||||
|
let applicableToOut = "";
|
||||||
|
|
||||||
|
let commands:Command[] = [];
|
||||||
|
let curCommandName = "";
|
||||||
|
let curCommandIdentifier = "";
|
||||||
|
|
||||||
|
let nextIter;
|
||||||
|
while(!(nextIter = applicableToIter.next()).done){
|
||||||
|
if (nextIter.value === "<" && applicableToIter.next().value === "<") {
|
||||||
|
let commandChar;
|
||||||
|
let commandName = "";
|
||||||
|
|
||||||
|
while((commandChar = applicableToIter.next()).value != ":"){
|
||||||
|
commandName += commandChar.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
let commandIdentifier = "";
|
||||||
|
|
||||||
|
while((commandChar = applicableToIter.next()).value != ">"){
|
||||||
|
commandIdentifier += commandChar.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = applicableToIter.next();
|
||||||
|
|
||||||
|
commands.push({commandIdentifier, commandName});
|
||||||
|
}else{
|
||||||
|
applicableToOut += nextIter.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {applicableTo:applicableToOut, commands};
|
||||||
|
|
||||||
|
}
|
49
src/transform/transform.ts
Normal file
49
src/transform/transform.ts
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
|
||||||
|
import * as babelparser from "@babel/parser";
|
||||||
|
import traverse from "@babel/traverse";
|
||||||
|
import * as t from "@babel/types";
|
||||||
|
|
||||||
|
import { parseApplicableTo } from "../parser/parse";
|
||||||
|
export interface TransformRecipe{
|
||||||
|
applicableTo: string,
|
||||||
|
identifiers: string[],
|
||||||
|
consumeBlock: boolean,
|
||||||
|
transformTo: string,
|
||||||
|
}
|
||||||
|
export function transform(recipe: TransformRecipe, code:string){
|
||||||
|
|
||||||
|
let {commands, applicableTo} = parseApplicableTo(recipe.applicableTo);
|
||||||
|
|
||||||
|
|
||||||
|
//console.log(applicableTo);
|
||||||
|
|
||||||
|
let applicableToAST = babelparser.parse(applicableTo, {allowAwaitOutsideFunction: true});
|
||||||
|
console.log(applicableToAST);
|
||||||
|
|
||||||
|
let codeAST = babelparser.parse(code, {allowAwaitOutsideFunction: true});
|
||||||
|
console.log(codeAST);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
traverse(applicableToAST, {
|
||||||
|
enter(path:any) {
|
||||||
|
traverse(codeAST, {
|
||||||
|
enter(codePath:any){
|
||||||
|
if (codePath.node.type === "Program"){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (codePath.node.type === path.node.type){
|
||||||
|
console.log("We found a match with");
|
||||||
|
console.log(codePath.node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue