syntax | , make xpaths | minimize xpath | Search

The code requires several modules and defines a function testMakeXpaths that tests the functionality of the makeXpaths function by generating XPath expressions for a function declaration node and asserting that its parent has an expected attribute. The function also logs the result, including an HTML representation of the tree structure if the $ variable is defined.

Run example

npm run import -- "generate css selectors from code"

generate css selectors from code

var esprima = require('esprima'); 
var assert = require('assert');
var importer = require('../Core');
var {selectAst, makeXpaths, htmlToTree} = importer.import("select code tree",
"make xpaths",
"html to tree")

var code = `
var importer = require('../Core');
function name(params) {
    return importer.interpret('this is a describe request');
}
console.log()
`

function testMakeXpaths(code) {
    // make a path with the interpret symbol
    var node1 = selectAst(`//*[@name="name"]`, code)
    var parent = selectAst(`//*[@type="FunctionDeclaration"]`, node1);
    var output = makeXpaths(parent)
    var node2 = selectAst(`//${output[0]}`, code)
    console.log(node2)
    assert(node1.parentNode.getAttribute('name') === node2.getAttribute('name'));
    return node2
    
}
// TODO: compare with acorn
if(typeof $ !== 'undefined') {
    var nodes = testMakeXpaths(code);
    console.log(htmlToTree(nodes));
}

What the code could have been:

// Import required modules
const esprima = require('esprima');
const assert = require('assert');
const { selectAst, makeXpaths, htmlToTree } = require('../Core');

// Define a function to test making XPaths
function makeXpathsTest(code) {
    // Make a path with the interpret symbol
    const node1 = selectAst(`//*[@name="name"]`, code);
    const parent = selectAst(`//*[@type="FunctionDeclaration"]`, node1);
    const xpaths = makeXpaths(parent);
    const node2 = selectAst(`//${xpaths[0]}`, code);

    // Verify that the node attributes match
    assert(node1.parentNode.getAttribute('name') === node2.getAttribute('name'));

    // Return the second node for further processing
    return node2;
}

// Define the test code
const code = `
var importer = require('../Core');
function name(params) {
    return importer.interpret('this is a describe request');
}
console.log()
`;

// Test making XPaths
let nodes;
if (typeof $!== 'undefined') {
    nodes = makeXpathsTest(code);
    console.log(htmlToTree(nodes));
}

// TODO: Compare with Acorn
// Note: Acorn is a separate parser and doesn't have a direct equivalent in Esprima
// However, you can compare the output of both parsers to ensure they're identical

Code Breakdown

Requirements and Imports

The code starts by requiring several modules:

Function Definition

A function testMakeXpaths is defined, which takes a code string as input. The function:

  1. Selects a node in the AST using an XPath expression that targets a function declaration with the name "name".
  2. Finds the parent node of the selected node, which is a function declaration.
  3. Generates XPath expressions for the parent node using the makeXpaths function.
  4. Selects the node in the original code using the generated XPath expression.
  5. Asserts that the node's parent has the expected attribute.
  6. Returns the selected node.

Execution and Logging

The code executes the testMakeXpaths function with a specific code string and logs the result. If the $ variable is defined, it logs the HTML representation of the tree structure generated by the htmlToTree function.

Note

The code seems to be testing the functionality of the makeXpaths function and its integration with the selectAst function. The htmlToTree function is also used to visualize the tree structure of the code. The testMakeXpaths function is a test case that demonstrates the usage of these functions.