pretty-format-ast
raw JSON → 1.0.1 verified Sat Apr 25 auth: no javascript
A plugin for pretty-format2 that prints Abstract Syntax Trees (ASTs) in a human-readable, indented format. Version 1.0.1 is the current stable release. It provides a structured view of AST nodes with their properties, types, and positions. Compared to alternatives like util.inspect, it produces a much more concise and visually organized output, specifically designed for AST inspection. The package works with any AST that follows a similar node structure and requires pretty-format2 as a peer dependency.
Common errors
error TypeError: prettyFormat2(...) is not a function ↓
cause CommonJS require of pretty-format2 may return different export depending on version. In v1.x, it is a default export function, but in v2.x, it may be exported differently.
fix
Use import: import prettyFormat2 from 'pretty-format2'; or ensure you have v1.x installed.
error Error: Cannot find module 'pretty-format2' ↓
cause pretty-format2 is a peer dependency and must be installed separately.
fix
Run: npm install pretty-format2
error SyntaxError: Unexpected token 'export' ↓
cause Using ESM imports in a CommonJS script or without proper package.json type: 'module'.
fix
Use require syntax, or set "type": "module" in package.json.
error TypeError: plugins is not iterable ↓
cause Passing a non-array value to the 'plugins' option, such as an object.
fix
Wrap the plugin in an array: plugins: [prettyFormatAST]
Warnings
gotcha Plugin must be passed as an array to the 'plugins' option. Passing a single plugin object without wrapping in an array will be silently ignored. ↓
fix Use { plugins: [prettyFormatAST] } instead of { plugins: prettyFormatAST }.
deprecated pretty-format2 v1.x is still used, but there is a newer major version v2.x. The plugin may not be compatible with v2.x without updates. ↓
fix Check compatibility with pretty-format2 v2.0.0 before upgrading.
breaking Starting from pretty-format2 v2.0.0, the plugin API changed. pretty-format-ast v1.0.1 is not updated and may not work with v2. ↓
fix Wait for an updated version of pretty-format-ast or pin pretty-format2 to v1.x.
gotcha The plugin only works with AST nodes that have a 'type' property. Objects without 'type' will not be formatted specially. ↓
fix Ensure your AST objects have a 'type' string field for proper formatting.
Install
npm install pretty-format-ast yarn add pretty-format-ast pnpm add pretty-format-ast Imports
- default wrong
const prettyFormatAST = require('pretty-format-ast')correctimport prettyFormatAST from 'pretty-format-ast' - prettyFormat2 wrong
const { format } = require('pretty-format2')correctimport prettyFormat2 from 'pretty-format2' - plugins wrong
prettyFormat2(val, { plugin: prettyFormatAST })correctprettyFormat2(val, { plugins: [prettyFormatAST] })
Quickstart
const prettyFormat2 = require('pretty-format2');
const prettyFormatAST = require('pretty-format-ast');
const ast = {
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: { type: 'Literal', value: 42 },
},
],
};
console.log(prettyFormat2(ast, { plugins: [prettyFormatAST] }));
// Output:
// Node "Program"
// body: Array [
// Node "ExpressionStatement"
// expression: Node "Literal"
// value: 42,
// ]