ECMAScript code generator that transforms Mozilla's Parser API ASTs back into executable JavaScript code
95
Build a JavaScript code generator that transforms Abstract Syntax Trees (ASTs) into formatted JavaScript source code.
Implement a module that provides the following capabilities:
Your implementation should:
type, operator, left, right, value, etc.)// test/generator.test.js
const ast = {
type: 'BinaryExpression',
operator: '+',
left: { type: 'Literal', value: 5 },
right: { type: 'Literal', value: 10 }
};
const result = generateCode(ast);
// Expected: '5 + 10'// test/generator.test.js
const ast = {
type: 'BinaryExpression',
operator: '*',
left: {
type: 'BinaryExpression',
operator: '+',
left: { type: 'Literal', value: 2 },
right: { type: 'Literal', value: 3 }
},
right: { type: 'Literal', value: 4 }
};
const result = generateCode(ast);
// Expected: '(2 + 3) * 4'// test/generator.test.js
const ast = {
type: 'Program',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'BinaryExpression',
operator: '+',
left: { type: 'Literal', value: 1 },
right: { type: 'Literal', value: 2 }
}
}]
};
const formatted = generateCode(ast, { format: 'readable' });
const minified = generateCode(ast, { format: 'compact' });
// Formatted should include whitespace and indentation
// Minified should be compact: '1+2;'JavaScript code generator from AST.
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