syntax | Cell 20 | , make xpaths | Search

The code uses the acorn JavaScript parser to parse code and collect comments and tokens, and the ../Core module to export necessary functions. It exports the selectAcorn(descriptor, code) function, which parses the code, selects an AST, and converts it to a tree structure using the htmlToTree(call) function.

Run example

npm run import -- "select acorn tree"

select acorn tree

var acorn = require("acorn")
var importer = require('../Core')
var {selectAst, htmlToTree} = importer.import("select code tree",
"html to tree")

function selectAcorn(descriptor, code) {
    var comments = [], tokens = [];
    if(typeof code === 'string') {
        code = acorn.parse(code, {
            ecmaVersion: 6,
            // collect ranges for each node
            locations: true,
            ranges: true,
            // collect comments in Esprima's format
            onComment: comments,
            // collect token ranges
            onToken: tokens,
        })
    }
    return selectAst(descriptor, code);
}

module.exports = {
    selectAcorn
}

if(typeof $ !== 'undefined') {
    var code = `
    var importer = require('../Core');
    function name(params) {
        return importer.interpret('this is a describe request');
    }
    console.log()
    `
    var call = selectAcorn(`//*[@name="interpret"]`, code);
    console.log(htmlToTree(call))
}

What the code could have been:

// Import required modules
const acorn = require("acorn");
const { selectAst, htmlToTree } = require('../Core');

/**
 * Selects the AST nodes that match the given descriptor.
 * 
 * @param {string} descriptor - The XPath expression to match against the AST nodes.
 * @param {string} code - The JavaScript code to parse.
 * @returns {Object[]} The selected AST nodes.
 */
function selectAcorn(descriptor, code) {
    // Initialize arrays to store comments and tokens
    const comments = [];
    const tokens = [];

    // If the code is a string, parse it using Acorn
    if (typeof code ==='string') {
        // Use Acorn's options to collect ranges, locations, comments, and tokens
        const ast = acorn.parse(code, {
            ecmaVersion: 6,
            locations: true,
            ranges: true,
            onComment: comments,
            onToken: tokens,
        });

        // Select the AST nodes that match the descriptor
        return selectAst(descriptor, ast);
    }

    // If the code is not a string, return an empty array
    return [];
}

// Export the selectAcorn function
module.exports = { selectAcorn };

// TODO: Move the test code to a separate file or a test framework
if (typeof $!== 'undefined') {
    const code = `
    var importer = require('../Core');
    function name(params) {
        return importer.interpret('this is a describe request');
    }
    console.log()
    `;

    const call = selectAcorn(`//*[@name="interpret"]`, code);
    console.log(htmlToTree(call));
}

Code Breakdown

Dependencies

Functions

selectAcorn(descriptor, code)

htmlToTree(call)

Usage

Module Exports

Note