ECMAScript code generator that transforms Mozilla's Parser API ASTs back into executable JavaScript code
95
Build a utility that generates JavaScript code from Abstract Syntax Trees (AST) while correctly handling context-specific constraints.
When generating JavaScript code from an AST, the same expression can require different handling depending on its syntactic context. Your task is to build a code generator that:
in operator in different contexts (allowed in regular expressions but not in for-loop initializers)The generator should:
in operator is allowed in the current positionin operator correctly @testin operator @test@generates
/**
* Generates JavaScript code from an AST node with context tracking.
*
* @param {Object} node - The AST node to generate code from
* @param {Object} flags - Context flags that control generation
* @param {boolean} flags.allowIn - Whether 'in' operator is allowed (false for for-loop init)
* @param {boolean} flags.inFunctionBody - Whether we're inside a function body
* @returns {string} Generated JavaScript code
*/
function generateWithFlags(node, flags) {
// IMPLEMENTATION HERE
}
/**
* Checks if an AST node contains an 'in' operator that would be invalid in the current context.
*
* @param {Object} node - The AST node to check
* @returns {boolean} True if node contains 'in' operator
*/
function containsInOperator(node) {
// IMPLEMENTATION HERE
}
module.exports = {
generateWithFlags,
containsInOperator
};Provides JavaScript code generation from AST with context-aware features.
Install with Tessl CLI
npx tessl i tessl/npm-escodegendocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10