This code offers functions for manipulating code represented as an Abstract Syntax Tree (AST), including adding, replacing, and removing nodes, with a focus on the transpile
function for modifying the AST structure.
npm run import -- "transpile code"
var importer = require('../Core')
var {htmlToTree} = importer.import("html to tree")
var {selectAst} = importer.import("select code tree")
function add() {
}
function replace(func, ctx) {
if(!ctx) {
return (ctx) => replace(func, ctx)
}
ctx.replaceWith(func(ctx))
}
function remove(ctx) {
if(!Array.isArray(ctx)) {
ctx = [ctx]
}
ctx.forEach(o => {
var body = selectAst([`./parent::*`], o)[0]
body.removeChild(o)
})
}
// TODO: use selectDom or handle antlr trees or existing doms properly
function transpile(input, output, code) {
var root
if(Array.isArray(input) && typeof code === 'undefined') {
code = output
}
root = selectAst(`//Program`, code)
input.forEach(i => {
selectAst([...i], root)
})
// TODO: add a feature for parsing default parameters
// TODO: check if tree string and tree dom and tree ast are changed
/*
if()
} else {
root = selectAst(`//Program`, code)
selectAst([input, output], root)
}
*/
return root
}
module.exports = {
transpile,
remove,
replace
}
const { htmlToTree, selectAst } = require('../Core');
/**
* Replaces the content of a node in the DOM with the result of a function.
* If no context is provided, returns a function that can be used to replace
* the content at a later time.
*
* @param {function} func - The function to use to replace the content.
* @param {object} ctx - The context in which to replace the content.
* @returns {function|undefined} The function to use to replace the content, or undefined if a context is provided.
*/
function replace(func, ctx) {
if (!ctx) return (ctx) => replace(func, ctx);
ctx.replaceWith(func(ctx));
}
/**
* Removes a node from the DOM.
*
* @param {object|array} ctx - The node or nodes to remove.
*/
function remove(ctx) {
if (!Array.isArray(ctx)) ctx = [ctx];
ctx.forEach((node) => {
const body = selectAst([`./parent::*`], node)[0];
if (body) body.removeChild(node);
});
}
/**
* Transpiles code from one format to another.
*
* @param {object|array} input - The input code.
* @param {object|array} output - The output code.
* @param {string} [code] - The code to transpile. Defaults to the output code.
* @returns {object} The transpiled code.
*/
function transpile(input, output, code) {
if (Array.isArray(input) && typeof code === 'undefined') code = output;
const root = selectAst(`//Program`, code);
if (Array.isArray(input)) {
input.forEach((i) => {
selectAst([...i], root);
});
} else {
selectAst([input, output], root);
}
return root;
}
module.exports = { transpile, remove, replace };
This code provides functions for manipulating code represented as an Abstract Syntax Tree (AST).
Here's a breakdown:
Imports:
importer
: A module for importing other modules.htmlToTree
: A function for converting HTML to an AST.selectAst
: A function for selecting nodes in an AST.add
, replace
, remove
Functions:
add
: Currently empty, likely intended for adding nodes to the AST.replace
: Replaces a node in the AST with a new node.remove
: Removes nodes from the AST.transpile
Function:
Module Exports:
transpile
, remove
, and replace
functions.