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.
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, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
if(typeof $ !== 'undefined') {
var code = `
function name(params) {
}
console.log()
`
var ctx = setupASTArray(code);
var html = testASTArray(ctx);
$.html('<pre>' + htmlEntities(html) + '</pre>')
}
// 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 + '
');
}
importer
: a module from the ../Core
directory, which is used to import functions treeToHtml
and htmlToTree
.assert
: a built-in Node.js module for making assertions.esprima
: a module for parsing JavaScript code into Abstract Syntax Trees (ASTs).setupASTArray(code)
: a function that takes JavaScript code as input, parses it into an AST using esprima
, and returns the parsed AST.testASTArray(ctx)
: a function that takes an AST as input, converts it into HTML using treeToHtml
, converts the HTML back into an AST using htmlToTree
, and asserts that the original and translated ASTs are equal. The function returns the converted HTML.htmlEntities(str)
: a function that encodes HTML entities in a given string.testASTArray
function, making it available for use in other modules.$
object is defined, it means the code is being executed in a specific environment (likely a test runner or a web environment), and it uses the htmlEntities
function to encode the HTML output of testASTArray
and displays it in a <pre>
element.treeToHtml
and htmlToTree
functions are implemented in the ../Core
module and are able to convert between ASTs and HTML.$
object is available in the execution environment.esprima
module to parse JavaScript code into ASTs, which may require Node.js 12 or later to work correctly.