CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-escodegen

ECMAScript code generator that transforms Mozilla's Parser API ASTs back into executable JavaScript code

95

1.07x
Overview
Eval results
Files

task.mdevals/scenario-9/

Expression Parenthesizer

A utility that transforms JavaScript Abstract Syntax Trees (ASTs) into properly parenthesized JavaScript code based on operator precedence rules.

Capabilities

Generates code with minimal parentheses

  • Given an AST representing 1 + 2 * 3, generates the code 1 + 2 * 3 without unnecessary parentheses @test
  • Given an AST representing (1 + 2) * 3, generates the code (1 + 2) * 3 with required parentheses @test

Handles operator precedence correctly

  • Given an AST with nested binary expressions involving addition and multiplication, generates code with correct precedence (e.g., a + b * c vs (a + b) * c) @test
  • Given an AST with exponentiation operator, generates code respecting right-associativity (e.g., 2 ** 3 ** 4 vs (2 ** 3) ** 4) @test

Supports optional parentheses mode

  • When configured for optional parentheses, given an AST representing 1 + 2, generates code with explicit parentheses like (1 + 2) for clarity @test
  • When configured to minimize parentheses, given the same AST, generates code 1 + 2 without optional parentheses @test

Handles special operator contexts

  • Given an AST with nullish coalescing operator mixed with logical operators, generates code with required parentheses (e.g., (a ?? b) && c or a ?? (b && c)) @test

Implementation

@generates

API

/**
 * Generates JavaScript code from an AST with proper parenthesization.
 *
 * @param {Object} ast - The Abstract Syntax Tree node (Mozilla Parser API format)
 * @param {Object} options - Generation options
 * @param {boolean} [options.parentheses] - If true, includes optional parentheses for clarity; if false, uses minimal parentheses
 * @returns {string} The generated JavaScript code
 */
function generateWithParentheses(ast, options) {
  // IMPLEMENTATION HERE
}

module.exports = {
  generateWithParentheses
};

Dependencies { .dependencies }

escodegen { .dependency }

Provides code generation from AST with operator precedence handling.

Install with Tessl CLI

npx tessl i tessl/npm-escodegen

tile.json