syntax | html to tree | Cell 18 | Search

This code imports necessary modules, defines functions for parsing and converting JavaScript code into HTML, and exports the testASTArray function for use in other modules. It also includes functionality for encoding HTML entities and assumes the presence of specific functions and an object ($) in the execution environment.

Cell 17

var importer = require('../Core');
var {treeToHtml, htmlToTree} = importer.import("tree to html",
"html to tree");

var assert = require('assert');
var esprima = require('esprima'); 

function setupASTArray(code) {
    var ctx = esprima.parse(code, {range: true, tokens: true,
                        comment: true, whitespace: true})
    
    return ctx;
}

function testASTArray(ctx) {
    var page = treeToHtml(ctx);
    var translated_back = htmlToTree(page);
    console.log(translated_back[0]);
    assert(JSON.stringify(ctx).length
           === JSON.stringify(translated_back[0]).length,
           'different code trees');    
    console.log('two JSON trees are same-same')
    return page;
}

module.exports = testASTArray;

function htmlEntities(str) {
    return String(str)
        .replace(/&/g, '&')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;');
}

if(typeof $ !== 'undefined') {
    var code = `
function name(params) {
    
}
console.log()
`
    var ctx = setupASTArray(code);
    var html = testASTArray(ctx);
    $.html('<pre>' + htmlEntities(html) + '</pre>')
}

What the code could have been:

// Import required modules
const importer = require('../Core');
const { treeToHtml, htmlToTree } = importer.import(['tree to html', 'html to tree']);
const assert = require('assert');
const esprima = require('esprima');

/**
 * Setup an Abstract Syntax Tree (AST) array from the given JavaScript code.
 * @param {string} code JavaScript code to parse
 * @returns {object} Parsed AST array
 */
function setupASTArray(code) {
    // Parse the code using esprima
    const ctx = esprima.parseScript(code, { range: true, tokens: true, comment: true, whitespace: true });
    return ctx;
}

/**
 * Test the AST array by converting it to HTML, then back to an AST array,
 * and assert that the two AST arrays are equal.
 * @param {object} ctx Parsed AST array
 * @returns {string} Converted HTML
 */
function testASTArray(ctx) {
    // Convert the AST array to HTML
    const html = treeToHtml(ctx);
    // Convert the HTML back to an AST array
    const translatedBack = htmlToTree(html);
    
    // Assert that the two AST arrays are equal
    assert.deepEqual(JSON.stringify(ctx), JSON.stringify(translatedBack[0]), 'Different code trees');
    
    // Log success message
    globalThis.console.log('Two JSON trees are same-same');
    
    return html;
}

// Define a function to encode HTML entities
function htmlEntities(str) {
    // Use the String.prototype.replace method to replace special characters
    return String(str)
       .replace(/&/g, '&')
       .replace(//g, '>')
       .replace(/"/g, '"');
}

// Export the testASTArray function
module.exports = testASTArray;

// Check if the $ object is defined
if (typeof $!== 'undefined') {
    // Define the code to parse
    const code = `
function name(params) {
    
}
console.log()
`;
    
    // Parse the code
    const ctx = setupASTArray(code);
    // Test the AST array
    const html = testASTArray(ctx);
    // Encode the HTML and display it
    const encodedHtml = htmlEntities(html);
    globalThis.$html('
' + encodedHtml + '
'); }

Code Breakdown

Importing Modules

Function Definitions

HTML Entity Encoding

Testing and Exporting

Code Assumptions