patterns | select tree | Cell 4 | Search

This code is a JavaScript testing module that uses the esprima and escodegen libraries to parse and generate code, and the assert library to make assertions on the code's syntax. The module exports two test functions, testProgram and testSelect, which can be used to test the code's structure and behavior.

Cell 3


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

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

function testProgram(ctx) {
    var node = selectTree('Program', ctx);
    assert(node.type === 'Program', 'just a single node not a list');
    return node;
}

function testSelect(ctx) {
    var node = selectTree([
        'Program'
    ], ctx);
    assert(node.length === 1, 'list of root node');
    assert(node[0].type === 'Program');
    // using the first query results as the context for the second query on each item
    
    var node = selectTree([
        'Identifier, CallExpression', // interchangable CSS selector
    ], ctx);
    assert(node.length > 6, 'list of concatenated elements');
    assert(typeof node[6].name !== 'undefined');
    
    var node = selectTree([
        '//*[@type="Identifier"]', // interchangable CSS selector
        './@name'
    ], ctx);
    assert(node.length > 3, 'list of ids');
    assert(node[3] === 'params');
}

module.exports = {
    testProgram,
    testSelect
}

// TODO: compare with acorn
if(typeof $ !== 'undefined') {
    var ctx = esprima.parse(code);
    var output = testProgram(ctx);
    testSelect(ctx);
    var output = escodegen.generate(output);
    console.log(output)
}

What the code could have been:

const esprima = require('esprima');
const escodegen = require('escodegen');
const assert = require('assert');

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

const parseCode = () => JSON.stringify(esprima.parse(code), null, 2);
const generateCode = (node) => escodegen.generate(node);

const testProgram = (ctx) => {
    const programNode = selectTree('Program', ctx);
    assert.strictEqual(programNode.type, 'Program', 'Just a single node, not a list');
    return programNode;
};

const testSelect = (ctx) => {
    const programNode = selectTree(['Program'], ctx);
    assert.strictEqual(programNode.length, 1, 'A list with one root node');
    assert.strictEqual(programNode[0].type, 'Program', 'Root node is Program');

    const identifierCallExpressions = selectTree(['Identifier', 'CallExpression'], ctx);
    assert(identifierCallExpressions.length > 6, 'List of concatenated elements');
    assert(identifierCallExpressions[6].type === 'Identifier', '6th element is an Identifier');

    const identifierNames = selectTree(['//[@type="Identifier"]', './@name'], ctx);
    assert(identifierNames.length > 3, 'List of identifier names');
    assert(identifierNames[3] === 'params', '4th element is \'params\'');

    const callExpressions = selectTree(['CallExpression, Identifier'], ctx);
    assert(callExpressions.length > 6, 'List of concatenated elements');
    assert(callExpressions[6].type === 'Identifier', '7th element is an Identifier');
};

module.exports = {
    testProgram,
    testSelect
};

const $ = esprima.parse(code);
if ($!== undefined) {
    const output = testProgram($);
    testSelect($);
    const generatedCode = generateCode(output);
    console.log(generatedCode);
}

// TODO: Compare with acorn

Code Breakdown

Importing Dependencies

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

Code to Test

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

Test Functions

testProgram

function testProgram(ctx) {
    var node = selectTree('Program', ctx);
    assert(node.type === 'Program', 'just a single node not a list');
    return node;
}

testSelect

function testSelect(ctx) {
    //...
}

Module Exports

module.exports = {
    testProgram,
    testSelect
}

Optional Code Execution

if(typeof $!== 'undefined') {
    var ctx = esprima.parse(code);
    var output = testProgram(ctx);
    testSelect(ctx);
    var output = escodegen.generate(output);
    console.log(output)
}