How to Format JavaScript: AST-Driven Code Formatting, ASI Traps, and Prettier Standards
A comprehensive guide to JavaScript and TypeScript code formatting: AST parsers, Automatic Semicolon Insertion (ASI) edge cases, and Prettier/Biome configurations.
The Evolution of JavaScript Formatters
Modern JavaScript and TypeScript formatting has moved away from subjective stylistic debates toward automated, Abstract Syntax Tree (AST)-driven formatters like Prettier and Biome. These tools parse source code into an in-memory AST and regenerate the entire output from scratch according to mathematical print width rules (typically 80 or 100 characters).
Automatic Semicolon Insertion (ASI) Traps
JavaScript automatically inserts semicolons at line boundaries according to complex grammar rules. Omitting semicolons can lead to catastrophic bugs when lines start with parenthesis (, brackets [, or backticks `` ```:
// BUG SCENARIO: The return statement returns 'undefined'!
function getUserData() {
return
{
name: 'Alex Rivera', // ASI inserts ';' immediately after 'return'
role: 'Lead'
};
}
// BUG SCENARIO: Array indexing interpreted as function call
const a = 1
const b = 2
[a, b].forEach(console.log) // JavaScript evaluates as: const b = 2[a, b] -> TypeError!Destructuring & Async/Await Formatting Standards
When destructuring more than two properties or handling complex asynchronous chains, format entries on separate lines for visual clarity:
// Multiline object destructuring with TypeScript types
const {
sessionId,
peerPublicKey,
bandwidthThrottleKbps,
onProgressCallback
}: TransferSessionConfig = options;
// Clean Async/Await pipeline formatting
export async function initializePeerConnection(config: PeerConfig): Promise<RTCPeerConnection> {
const pc = new RTCPeerConnection(config.iceServers);
pc.onicecandidate = (event) => {
if (event.candidate) {
signalingSocket.send({ type: 'candidate', candidate: event.candidate });
}
};
return pc;
}Trailing Commas & Clean Git Diffs
Always configure multiline arrays, objects, and function parameters with trailing commas ("trailingComma": "all"). Adding a new property in a git commit modifies only the newly added line without touching previous lines, resulting in clean pull request diffs and zero merge conflicts.
Prettier & Biome Production Configurations
// .prettierrc
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"printWidth": 100,
"trailingComma": "all",
"arrowParens": "always",
"bracketSpacing": true
}ESLint vs Prettier: Defining Boundaries
| Tool | Scope of Responsibility | Examples |
|---|---|---|
| Prettier / Biome | Stylistic AST formatting only (Indentation, line wrapping, quotes, semicolons) | Print width, bracket spacing, trailing commas |
| ESLint / Oxlint | Code-quality and bug detection (Logic, types, unused variables, security traps) | `no-unused-vars`, `@typescript-eslint/no-explicit-any`, `react-hooks/exhaustive-deps` |
Code Formatter & Converter Suite
Validate, format, minify, and convert JSON, SQL, YAML, XML, and code dialects directly on your local machine.
Frequently Asked Questions
Should I use Biome instead of Prettier in 2026?
Biome is written in Rust and executes 25x-35x faster than Prettier while combining formatting with high-speed linting. For large monorepos, Biome offers substantial CI/CD speedup.
Why do team members experience formatting conflicts on Windows vs macOS?
Set `"endOfLine": "lf"` in your formatting config and configure `.gitattributes` with `* text=auto eol=lf` to standardize LF line endings across operating systems.
Conclusion
Read more tools and developer guides on CoShareX.
Related Articles
How to Format CSS: Specificity Management, Cascade Layers (@layer), and Stylelint Rules
Master CSS code formatting and architecture: logical property ordering, Cascade Layers (@layer), CSS Custom Properties, and automated Stylelint standards.
Developer ToolsHow to Format HTML: Semantic Hierarchy, Void Elements, and Accessibility Standards
Learn best practices for formatting HTML5 documents: semantic markup structure, void element handling, attribute ordering, and accessibility linting.
Developer ToolsHow to Format SQL Queries: CTEs, JOIN Alignment, Window Functions, and SQLFluff Standards
Master SQL query formatting: uppercase keyword standards, Common Table Expression (CTE) indentation, JOIN alignment, window functions, and SQLFluff linting.