ECMAScript code generator that transforms Mozilla's Parser API ASTs back into executable JavaScript code
95
A utility that generates JavaScript code from Abstract Syntax Trees (ASTs) with different formatting styles. The utility should support configuring indentation, whitespace, and quote styles to match different coding standards.
Generate code with configurable indentation settings (spaces vs tabs, indent level).
function add(a, b) { return a + b; }, formatting with 2-space indentation produces code where the return statement is indented with exactly 2 spaces @testfunction multiply(x, y) { return x * y; }, formatting with tab indentation produces code where the return statement is indented with exactly one tab character @testGenerate code with specified quote styles for string literals.
var msg = "hello";, formatting with single quote style produces code with the string as 'hello' @testvar msg = 'world';, formatting with double quote style produces code with the string as "world" @testGenerate minified code with minimal whitespace.
function test() { var x = 1; return x + 2; }, formatting in compact mode produces function test(){var x=1;return x+2;} (no spaces around operators or after keywords) @testGenerate code with a specified base indentation level.
var x = { a: 1, b: 2 };, formatting with base indentation of 1 level produces code where var starts with 4 spaces and object properties are indented 8 spaces @test@generates
/**
* Formats JavaScript code from an AST with 2-space indentation.
*
* @param {Object} ast - The Abstract Syntax Tree node to format
* @returns {string} The formatted JavaScript code with 2-space indentation
*/
function formatWithTwoSpaces(ast) {
// IMPLEMENTATION HERE
}
/**
* Formats JavaScript code from an AST with tab indentation.
*
* @param {Object} ast - The Abstract Syntax Tree node to format
* @returns {string} The formatted JavaScript code with tab indentation
*/
function formatWithTabs(ast) {
// IMPLEMENTATION HERE
}
/**
* Formats JavaScript code from an AST with single quotes for strings.
*
* @param {Object} ast - The Abstract Syntax Tree node to format
* @returns {string} The formatted JavaScript code with single-quoted strings
*/
function formatWithSingleQuotes(ast) {
// IMPLEMENTATION HERE
}
/**
* Formats JavaScript code from an AST with double quotes for strings.
*
* @param {Object} ast - The Abstract Syntax Tree node to format
* @returns {string} The formatted JavaScript code with double-quoted strings
*/
function formatWithDoubleQuotes(ast) {
// IMPLEMENTATION HERE
}
/**
* Formats JavaScript code from an AST in compact mode (minified).
*
* @param {Object} ast - The Abstract Syntax Tree node to format
* @returns {string} The formatted JavaScript code with minimal whitespace
*/
function formatCompact(ast) {
// IMPLEMENTATION HERE
}
/**
* Formats JavaScript code from an AST with a base indentation level.
*
* @param {Object} ast - The Abstract Syntax Tree node to format
* @param {number} baseIndentLevel - The base indentation level (e.g., 1 for one indent)
* @returns {string} The formatted JavaScript code with base indentation applied
*/
function formatWithBaseIndent(ast, baseIndentLevel) {
// IMPLEMENTATION HERE
}
module.exports = {
formatWithTwoSpaces,
formatWithTabs,
formatWithSingleQuotes,
formatWithDoubleQuotes,
formatCompact,
formatWithBaseIndent
};Provides JavaScript code generation from AST with formatting control.
@satisfied-by
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