languages | Cell 2 | Cell 4 | Search

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.

Run example

npm run import -- "transpile code"

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
}

What the code could have been:

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:

  1. Imports:

  2. add, replace, remove Functions:

  3. transpile Function:

  4. Module Exports: