syntax | get imports from source | Cell 11 | Search

This code imports modules, defines sample JavaScript code, and tests a function getImports on various inputs, including a file and a string, and logs the results in a conditional block.

Run example

npm run import -- "test get imports"

test get imports

var importer = require('../Core');
var getImports = importer.import("get imports from source")
var {selectAst} = importer.import("select code tree")
var {htmlToTree} = importer.import("html to tree")

var code = `
var importer = require('../Core');
var getParameters = importer.import("get parameter names")
`

function testGetImports() {
    console.log(getImports(importer.interpret('rpc.ipynb[1]').code))
    console.log(htmlToTree(selectAst('//CallExpression', code)[1]))
    return getImports(code)
}

if(typeof $ != 'undefined') {
    testGetImports()
    
    /*
    expected output
    get parameter names
    import
    
    */
}

What the code could have been:

// Import necessary modules and functions
const Core = require('../Core');
const { getImports, selectAst, htmlToTree } = Core.importer;

/**
 * Function to test get imports functionality.
 *
 * @returns {object} An object containing the result of the get imports function.
 */
function testGetImports() {
    // Get imports from the interpreted code in rpc.ipynb
    const imports = getImports(Core.interpret('rpc.ipynb[1]').code);
    console.log(imports);

    // Parse and convert code to a tree
    const codeTree = htmlToTree(selectAst('//CallExpression', code)[1]);
    console.log(codeTree);

    // Return the result of get imports function
    return getImports(code);
}

// Define code to test the get imports function
const code = `
var importer = require('../Core');
var getParameters = importer.import('get parameter names')
`;

// Check if $ is defined and call the test function
if (typeof $!== 'undefined') {
    testGetImports();

    // Expected output
    // get parameter names
    // import
}

// TODO: Implement test cases for the get imports function
// TODO: Consider using a testing framework like Jest for unit testing

Code Breakdown

Importing Modules

var importer = require('../Core');
var getImports = importer.import('get imports from source')
var {selectAst} = importer.import('select code tree')
var {htmlToTree} = importer.import('html to tree')

This code imports a module named Core from the parent directory using require. It then creates three variables that interact with the imported module:

Defining Code to be Analyzed

var code = `
var importer = require('../Core');
var getParameters = importer.import('get parameter names')
`

This code defines a string code containing sample JavaScript code.

Test Function

function testGetImports() {
    console.log(getImports(importer.interpret('rpc.ipynb[1]').code))
    console.log(htmlToTree(selectAst('//CallExpression', code)[1]))
    return getImports(code)
}

This function tests the getImports function on several inputs:

Conditional Execution

if(typeof $!= 'undefined') {
    testGetImports()
    /*
    expected output
    get parameter names
    import
    
    */
}

This code checks if a variable named $ is defined. If it is, the testGetImports function is executed, and the expected output is documented.