The code uses the acorn
JavaScript parser to parse code and collect comments and tokens, and the ../Core
module to export necessary functions. It exports the selectAcorn(descriptor, code)
function, which parses the code, selects an AST, and converts it to a tree structure using the htmlToTree(call)
function.
npm run import -- "select acorn tree"
var acorn = require("acorn")
var importer = require('../Core')
var {selectAst, htmlToTree} = importer.import("select code tree",
"html to tree")
function selectAcorn(descriptor, code) {
var comments = [], tokens = [];
if(typeof code === 'string') {
code = acorn.parse(code, {
ecmaVersion: 6,
// collect ranges for each node
locations: true,
ranges: true,
// collect comments in Esprima's format
onComment: comments,
// collect token ranges
onToken: tokens,
})
}
return selectAst(descriptor, code);
}
module.exports = {
selectAcorn
}
if(typeof $ !== 'undefined') {
var code = `
var importer = require('../Core');
function name(params) {
return importer.interpret('this is a describe request');
}
console.log()
`
var call = selectAcorn(`//*[@name="interpret"]`, code);
console.log(htmlToTree(call))
}
// Import required modules
const acorn = require("acorn");
const { selectAst, htmlToTree } = require('../Core');
/**
* Selects the AST nodes that match the given descriptor.
*
* @param {string} descriptor - The XPath expression to match against the AST nodes.
* @param {string} code - The JavaScript code to parse.
* @returns {Object[]} The selected AST nodes.
*/
function selectAcorn(descriptor, code) {
// Initialize arrays to store comments and tokens
const comments = [];
const tokens = [];
// If the code is a string, parse it using Acorn
if (typeof code ==='string') {
// Use Acorn's options to collect ranges, locations, comments, and tokens
const ast = acorn.parse(code, {
ecmaVersion: 6,
locations: true,
ranges: true,
onComment: comments,
onToken: tokens,
});
// Select the AST nodes that match the descriptor
return selectAst(descriptor, ast);
}
// If the code is not a string, return an empty array
return [];
}
// Export the selectAcorn function
module.exports = { selectAcorn };
// TODO: Move the test code to a separate file or a test framework
if (typeof $!== 'undefined') {
const code = `
var importer = require('../Core');
function name(params) {
return importer.interpret('this is a describe request');
}
console.log()
`;
const call = selectAcorn(`//*[@name="interpret"]`, code);
console.log(htmlToTree(call));
}
acorn
is a JavaScript parser and used to parse the code.../Core
is an external module that exports the necessary functions.selectAcorn(descriptor, code)
descriptor
and code
as input.acorn.parse()
function to parse the code and collect comments and tokens.selectAst()
function with the parsed code as an argument.htmlToTree(call)
call
as input and converts it to a tree structure.selectAcorn()
function is called with a string representation of a call.$
is defined, and if so, it calls selectAcorn()
function with a specific descriptor
and code
as input.htmlToTree()
function and logged to the console.selectAcorn()
function is exported as a module.acorn.parse()
function is used with the following options:
ecmaVersion
: 6 (parse ES6 code)locations
: true (collect ranges for each node)ranges
: true (collect token ranges)onComment
: collect comments in Esprima's formatonToken
: collect token ranges