syntax | select ast | select acorn tree | Search

The code tests a given code string by selecting specific HTML nodes and logging their properties to the console. The code uses several functions from the importer, which is imported from the ../Core module, to perform the necessary operations.

Cell 20

var assert = require('assert');
var importer = require('../Core');
var {selectAst, makeXpaths, htmlToTree} = importer.import("select 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 testCodeToDom(code) {
    // make a path with the interpret symbol
    var node1 = selectAst(`//*[@name="interpret"]`, code)
    console.log(node1)
    var parent = selectAst(`parent::*`, node1);
    assert(parent.nodeName !== '', 'parent selector works');
    console.log(selectAst(`*`, node1).children.length);
    console.log(htmlToTree(parent));
}

if(typeof $ !== 'undefined') {
    testCodeToDom(code);
}

What the code could have been:

const assert = require('assert');
const { importModules } = require('../Core');

const { selectAst, makeXpaths, htmlToTree } = importModules([
 'select tree',
 'make xpaths',
  'html to tree'
]);

/**
 * Test code-to-dom functionality with the given code.
 *
 * @param {string} code - The code to test.
 */
function testCodeToDom(code) {
  // Make a path with the interpret symbol
  const interpretNode = selectAst(`//*[@name="interpret"]`, code);
  console.log(interpretNode);

  // Get the parent node of the interpret node
  const parentNode = selectAst(`parent::*`, interpretNode);
  assert(parentNode.nodeName!== '', 'Parent selector works');

  // Log the number of children of the interpret node
  console.log(selectAst(`*`, interpretNode).children.length);

  // Log the HTML tree of the parent node
  console.log(htmlToTree(parentNode));
}

/**
 * Entry point of the script.
 */
function main() {
  const code = `
    var importer = require('../Core');
    function name(params) {
      return importer.interpret('this is a describe request');
    }
    console.log()
  `;

  // Only run the test if $ is defined, e.g., in a browser environment
  if (typeof $!== 'undefined') {
    testCodeToDom(code);
  }
}

main();

Code Breakdown

Dependencies and Imports

Code to Test

testCodeToDom Function

Main Logic