$ show beautifier/js-beautify
js-beautify is a production-grade code formatter for JavaScript, CSS, and HTML, available as both a Node.js library/CLI and a Python package. The core architecture is a tokenizer-pipeline: source text is scanned by an InputScanner, tokenized by language-specific tokenizers (JavaScript, CSS, HTML), and then processed by a Beautifier that manages indentation, line wrapping, brace styles, and operator positioning via a stateful flag system. The Output class provides efficient indentation caching and wrap-point management. The tool supports a wide range of configuration options (indent size, brace style, attribute wrapping, template languages) and includes built-in unpackers for obfuscated scripts (P.A.C.K.E.R, JavascriptObfuscator, etc.). A web interface is also provided, built with CodeMirror and RequireJS. The project maintains near-identical JavaScript and Python implementations, sharing test data generated from Mustache templates to ensure parity. The codebase is well-structured with clear separation of concerns (core, language-specific, CLI, web), extensive test coverage (including regression tests for hundreds of GitHub issues), and a mature build/release pipeline. However, the dual-codebase requirement increases maintenance burden, and some parts (e.g., the web UI, Debian packaging) show signs of drift. The large test data files and complex matrix-based test generation add brittleness.
The LLM identified 5 files worth highlighting for reuse:
The Output class manages formatted text output with efficient indentation (IndentStringCache), line wrapping at configurable widths, and preservation of existing newlines. It is self-contained and could be extracted for any code generation or formatting tool that needs to manage indentation and line breaks.
Extends the base Pattern class to handle multiple templating languages (Django, ERB, Handlebars, PHP, Smarty, Angular) by detecting template delimiters and adjusting tokenization. This is a reusable pattern for any tool that needs to parse or format code containing embedded template syntax.
Implements parsing of beautifier directives (e.g., /* beautify ignore:start */, /* beautify preserve:start */) embedded in source code. This is a self-contained module that could be reused in any code transformation tool that needs to support inline control annotations.
Provides reusable test input definitions for operator_position tests, including sanity checks, comprehensive cases, colon special cases, and catch-all patterns. Demonstrates a pattern for centralizing complex test data that is shared across multiple test groups, reducing duplication and improving maintainability.
A Mustache template used to generate the browser bundle for the beautifier library. It wraps the core modules into a single UMD-style export. This pattern of using templates to generate build artifacts is reusable for any project that needs to produce multiple distribution formats from a shared source.