or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-features.mdast-nodes.mdcode-generation.mdformatting-options.mdindex.mdsource-maps.md
tile.json

formatting-options.mddocs/

Formatting Options

Comprehensive configuration options for controlling the output style and formatting of generated JavaScript code.

Capabilities

Whitespace Control

Options for controlling whitespace, indentation, and code density.

interface WhitespaceOptions {
  /** Minimize whitespace - true (minimal), false (normal), "auto" (context-dependent) */
  compact?: boolean | "auto";
  
  /** Full minification mode - removes all unnecessary whitespace and shortens identifiers where possible */
  minified?: boolean;
  
  /** Reduce unnecessary whitespace while maintaining readability */
  concise?: boolean;
  
  /** Preserve original line numbers when possible (useful for debugging) */
  retainLines?: boolean;
}

Usage Examples:

import generate from "babel-generator";

// Compact output
const minified = generate(ast, { compact: true });
// Output: const x=1;const y=2;

// Normal formatting  
const readable = generate(ast, { compact: false });
// Output: const x = 1;\nconst y = 2;

// Full minification
const minified = generate(ast, { minified: true });
// Output: const a=1,b=2;

// Auto-compact (compact for small code, normal for large)
const auto = generate(ast, { compact: "auto" });

Quote Style

Configure string quote preferences and JSON compatibility.

interface QuoteOptions {
  /** Quote style for string literals */
  quotes?: "single" | "double";
  
  /** Generate JSON-compatible strings (always double quotes, escape sequences) */
  jsonCompatibleStrings?: boolean;
}

Usage Examples:

// Single quotes (default)
const single = generate(ast, { quotes: "single" });
// Output: const msg = 'hello';

// Double quotes  
const double = generate(ast, { quotes: "double" });
// Output: const msg = "hello";

// JSON-compatible strings
const json = generate(ast, { jsonCompatibleStrings: true });
// Output: const msg = "hello\nworld"; (with proper escaping)

Comment Handling

Control inclusion and filtering of comments in generated code.

interface CommentOptions {
  /** Include comments in output */
  comments?: boolean;
  
  /** Function to filter which comments to include */
  shouldPrintComment?: (comment: string) => boolean;
  
  /** Comment to add at the beginning of generated code */
  auxiliaryCommentBefore?: string;
  
  /** Comment to add at the end of generated code */
  auxiliaryCommentAfter?: string;
}

Usage Examples:

// Include all comments
const withComments = generate(ast, { comments: true });

// Filter comments
const filtered = generate(ast, {
  comments: true,
  shouldPrintComment: (comment) => !comment.includes('@private')
});

// Add auxiliary comments
const documented = generate(ast, {
  auxiliaryCommentBefore: '/* Generated by Babel Generator */',
  auxiliaryCommentAfter: '/* End of generated code */'
});

Function Expression Handling

Control parentheses around function expressions.

interface FunctionOptions {
  /** Keep parentheses around function expressions to preserve semantics */
  retainFunctionParens?: boolean;
}

Usage Examples:

// With parentheses retained (safer)
const safe = generate(ast, { retainFunctionParens: true });
// Output: (function() { return 42; })();

// Without parentheses (more compact)
const compact = generate(ast, { retainFunctionParens: false });
// Output: function() { return 42; }();

Flow-Specific Options

Special formatting options for Flow type annotations.

interface FlowOptions {
  /** Use commas instead of semicolons in Flow type definitions */
  flowCommaSeparator?: boolean;
}

Usage Examples:

// Default semicolon separators
const defaultFlow = generate(ast, { flowCommaSeparator: false });
// Output: type Props = { x: number; y: string; };

// Comma separators  
const commaFlow = generate(ast, { flowCommaSeparator: true });
// Output: type Props = { x: number, y: string, };

Debug and Development Options

Options helpful for debugging and development workflows.

interface DebugOptions {
  /** Filename for error messages and source maps */
  filename?: string;
  
  /** Preserve original line numbers when possible */
  retainLines?: boolean;
}

Usage Examples:

// With filename for better error messages
const debugFriendly = generate(ast, {
  filename: 'input.js',
  retainLines: true
});

// Error messages will reference 'input.js' and maintain line numbers

Complete Options Interface

Full interface combining all formatting options.

interface FormattingOptions {
  // Whitespace control
  compact?: boolean | "auto";
  minified?: boolean;
  concise?: boolean;
  retainLines?: boolean;
  
  // Quote preferences
  quotes?: "single" | "double";
  jsonCompatibleStrings?: boolean;
  
  // Comment handling
  comments?: boolean;
  shouldPrintComment?: (comment: string) => boolean;
  auxiliaryCommentBefore?: string;
  auxiliaryCommentAfter?: string;
  
  // Function handling
  retainFunctionParens?: boolean;
  
  // Flow-specific
  flowCommaSeparator?: boolean;
  
  // Debug options
  filename?: string;
}

Formatting Presets

Common combinations of formatting options for typical use cases.

Usage Examples:

// Development preset - readable with comments
const development = generate(ast, {
  compact: false,
  comments: true,
  quotes: "single"
});

// Production preset - minified without comments
const production = generate(ast, {
  minified: true,
  comments: false
});

// Debug preset - preserve structure for debugging
const debug = generate(ast, {
  retainLines: true,
  retainFunctionParens: true,
  filename: 'debug.js'
});