{"id":14156362,"url":"https://github.com/putoutjs/printer","last_synced_at":"2026-06-05T00:01:14.317Z","repository":{"id":142730968,"uuid":"613324810","full_name":"putoutjs/printer","owner":"putoutjs","description":"Easiest possible opinionated Babel AST printer","archived":false,"fork":false,"pushed_at":"2026-06-04T22:37:29.000Z","size":2109,"stargazers_count":20,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-06-04T23:14:52.270Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/putoutjs.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"coderaiser","open_collective":"cloudcmd","ko_fi":"coderaiser"}},"created_at":"2023-03-13T10:58:01.000Z","updated_at":"2026-06-04T22:37:30.000Z","dependencies_parsed_at":"2026-03-04T01:05:41.813Z","dependency_job_id":null,"html_url":"https://github.com/putoutjs/printer","commit_stats":{"total_commits":1647,"total_committers":1,"mean_commits":1647.0,"dds":0.0,"last_synced_commit":"576e551c3cdf4e1255b31d5647018d25dff52006"},"previous_names":[],"tags_count":837,"template":false,"template_full_name":null,"purl":"pkg:github/putoutjs/printer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/putoutjs%2Fprinter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/putoutjs%2Fprinter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/putoutjs%2Fprinter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/putoutjs%2Fprinter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/putoutjs","download_url":"https://codeload.github.com/putoutjs/printer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/putoutjs%2Fprinter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33924832,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-04T02:00:06.755Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-17T08:05:24.564Z","updated_at":"2026-06-05T00:01:14.304Z","avatar_url":"https://github.com/putoutjs.png","language":"JavaScript","funding_links":["https://github.com/sponsors/coderaiser","https://opencollective.com/cloudcmd","https://ko-fi.com/coderaiser"],"categories":["others"],"sub_categories":[],"readme":"# Printer [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]\n\n[NPMURL]: https://npmjs.org/package/@putout/printer \"npm\"\n[NPMIMGURL]: https://img.shields.io/npm/v/@putout/printer.svg?style=flat\u0026longCache=true\n[BuildStatusURL]: https://github.com/putoutjs/printer/actions/workflows/nodejs.yml \"Build Status\"\n[BuildStatusIMGURL]: https://github.com/putoutjs/printer/actions/workflows/nodejs.yml/badge.svg\n[LicenseURL]: https://tldrlegal.com/license/mit-license \"MIT License\"\n[LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat\n[CoverageURL]: https://coveralls.io/github/putoutjs/printer?branch=master\n[CoverageIMGURL]: https://coveralls.io/repos/putoutjs/printer/badge.svg?branch=master\u0026service=github\n\nPrints [**Babel AST**](https://github.com/coderaiser/estree-to-babel) to readable **JavaScript**.\nUse 🐊[**Putout**](https://github.com/coderaiser/putout) to parse your code.\n\nYou may also use [Babel 8](https://github.com/putoutjs/babel) with [`estree-to-babel`](https://github.com/coderaiser/estree-to-babel) for **ESTree** and **Babel AST** to put `.extra.raw` to `.raw` (which is simpler for transforms, no need to use [Optional Chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) and add extra values every time).\n\n- ☝️ Similar to **Recast**, but [twice faster](#speed-comparison), also simpler and easier in maintenance, since it supports only **Babel**.\n- ☝️ As opinionated as **Prettier**, but has more user-friendly output and works directly with **AST**.\n- ☝️ Like **ESLint** but works directly with **Babel AST**.\n- ☝️ Easily extendable with help of [Overrides](#overrides).\n\nSupports:\n\n- ✅ **ES2026**;\n- ✅ **JSX**;\n- ✅ [**TypeScript**](https://www.typescriptlang.org/);\n- ✅ [**JSON**](https://github.com/coderaiser/putout/tree/master/packages/processor-json#readme);\n\n## Install\n\n```\nnpm i @putout/printer\n```\n\n## 🐊 Support of Printer\n\n**Printer** has first class support from 🐊**Putout** with help of [`@putout/plugin-printer`](https://github.com/coderaiser/putout/tree/master/packages/plugin-printer#putoutplugin-printer-). So install:\n\n```sh\nnpm i @putout/plugin-printer -aD\n```\n\nAnd update `.putout.json`:\n\n```json\n{\n    \"printer\": \"putout\",\n    \"plugins\": [\"printer\"]\n}\n```\n\nTo benefit from it.\n\n## Example\n\n```js\nimport {print} from '@putout/printer';\nimport {parse} from 'putout';\n\nconst ast = parse('const a = (b, c) =\u003e {const d = 5; return a;}');\n\nprint(ast);\n// returns\n`\nconst a = (b, c) =\u003e {\n    const d = 5;\n    return a;\n};\n`;\n```\n\n## Overrides\n\nWhen you need to extend syntax of `@putout/printer` just pass a function which receives:\n\n- `path`, Babel Path\n- `print`, a function to output result of printing into token array;\n\nWhen `path` contains to dashes `__` and name, it is the same as: `write(path.get('right'))`, and this is\nactually `traverse(path.get('right'))` shortened to simplify read and process.\n\nHere is how you can override `AssignmentPattern`:\n\n```js\nconst ast = parse('const {a = 5} = b');\n\nprint(ast, {\n    format: {\n        indent: '    ',\n        newline: '\\n',\n        space: ' ',\n        splitter: '\\n',\n        quote: `'`,\n        endOfFile: '\\n',\n    },\n    semantics: {\n        comments: true,\n        maxSpecifiersInOneLine: 2,\n        maxElementsInOneLine: 3,\n        maxElementLengthInOneLine: 15,\n        maxLogicalsInOneLine: 3,\n        maxVariablesInOneLine: 4,\n        maxTypesInOneLine: 3,\n        maxPropertiesInOneLine: 2,\n        maxPropertiesLengthInOneLine: 15,\n        trailingComma: true,\n        escapeSingleQuote: true,\n        escapeDoubleQuote: false,\n        roundBraces: {\n            arrow: true,\n            sequence: true,\n            assign: false,\n            new: true,\n        },\n    },\n    visitors: {\n        AssignmentPattern(path, {print}) {\n            print('/* [hello world] */= ');\n            print('__right');\n        },\n    },\n});\n\n// returns\n'const {a/* [hello world] */= 5} = b;\\n';\n```\n\n### `format`\n\nOptions related to visuals and not related to logic of output can be changed with help of `format`,\nyou can override next options:\n\n```js\nconst overrides = {\n    format: {\n        indent: '    ',\n        newline: '\\n',\n        space: ' ',\n        splitter: '\\n',\n        endOfFile: '\\n',\n    },\n};\n```\n\n- `indent` - use two spaces, tabs, or anything you want;\n- `newline` - symbol used for line separation;\n- `space` - default symbol used for space character;\n- `splitter` - mandatory symbol that used inside of statements like this:\n\nDefault options produce:\n\n```js\nif (a \u003e 3)\n    console.log('ok');\nelse\n    console.log('not ok');\n```\n\nBut you can override them with:\n\n```js\nconst overrides = {\n    format: {\n        indent: '',\n        newline: '',\n        space: '',\n        splitter: ' ',\n    },\n};\n```\n\nAnd have minified code:\n\n```\nif(a\u003e3)console.log('ok');else console.log('not ok');\n```\n\n### Semantics\n\nOptions used to configure logic of output, similar to ESLint rules:\n\n- ✅ `maxElementsInOneLine` - count of `ArrayExpression` and `ArrayPattern` elements placed in one line.\n- ✅ `maxLogicalsInOneLine` - count of `LogicalExpression` elements placed in one line.\n- ✅ `maxVariablesInOneLine` - count of `VariableDeclarators` in one line.\n- ✅ `maxPropertiesInOneLine` - count of `ObjectProperties` in one line.\n- ✅ `maxPropertiesLengthInOneLine` - maximum length of `Object Property`, when violated splits event if `maxPropertiesInOneLine` satisfies;\n- ✅ `roundBraces` to output braces or not\n  - `arrow`: In a  single argument arrow function expressions enabled: `(a) =\u003e {}`, disabled: `a =\u003e {}`;\n  - `sequence`: In sequence expressions: enabled: `for(let e of l) (a(), b())`, disabled: `for(let e of l) a(), b()`;\n  - `assign`: In assignment expressions: enabled: `(e.o=w(e.o)`, disabled: `e.o=w(e.o)`;\n  - `new`: In new expressions: enabled: `new Date()`, disabled: `new Date`;\n\n## Visitors API\n\nWhen you want to improve support of existing visitor or extend **Printer** with a new ones, you need next base operations:\n\n### override\n\nWhen you need to override behavior of existing visitor use:\n\n```js\nimport {\n    print,\n    visitors as v,\n} from '@putout/printer';\n\nprint(ast, {\n    visitors: {\n        CallExpression(path, printer, semantics) {\n            const {print} = printer;\n            \n            if (!path.node.goldstein)\n                return v.CallExpression(path, printer, semantics);\n            \n            print('__goldstein');\n        },\n    },\n});\n```\n\n### `print`\n\nUsed in previous example `print` can be used for a couple purposes:\n\n- to write `string`;\n- to write `node` when `object` passed;\n- to write `node` when `string` started with `__`;\n\n```js\nprint(ast, {\n    visitors: {\n        AssignmentPattern(path, {print, maybe}) {\n            maybe.write.newline(path.parentPath.isCallExpression());\n            print('/* [hello world] */= ');\n            print('__right');\n        },\n    },\n});\n```\n\n### `maybe`\n\nWhen you need some condition use `maybe`. For example, to add newline only when parent node is `CallExpression` you\ncan use `maybe.write.newline(condition)`:\n\n```js\nprint(ast, {\n    visitors: {\n        AssignmentPattern(path, {write, maybe}) {\n            maybe.write.newline(path.parentPath.isCallExpression());\n            write(' /* [hello world] */= ');\n            write('__right');\n        },\n    },\n});\n```\n\n### `write`\n\nWhen you going to output string you can use low-level function `write`:\n\n```js\nprint(ast, {\n    visitors: {\n        BlockStatement(path, {write}) {\n            write('hello');\n        },\n    },\n});\n```\n\n### `indent`\n\nWhen you need to add indentation use `indent`, for example when you output body,\nyou need to increment indentation, and then decrement it back:\n\n```js\nprint(ast, {\n    visitors: {\n        BlockStatement(path, {write, indent}) {\n            write('{');\n            indent.inc();\n            indent();\n            write('some;');\n            indent.dec();\n            write('{');\n        },\n    },\n});\n```\n\n### `traverse`\n\nWhen you need to traverse node path, you can use `traverse`:\n\n```js\nprint(ast, {\n    visitors: {\n        AssignmentExpression(path, {traverse}) {\n            traverse(path.get('left'));\n        },\n    },\n});\n```\n\nThis is the same as `print('__left')` but more low-level, and supports only objects.\n\n## Speed Comparison\n\nAbout speed, for file `speed.js`:\n\n```js\nimport {readFileSync} from 'node:fs';\nimport {putout} from 'putout';\nimport parser from '@babel/parser';\n\nconst code = readFileSync('./lib/tokenize/tokenize.js', 'utf8');\nconst ast = parser.parse(code);\n\nspeed('recast');\nspeed('putout');\n\nfunction speed(printer) {\n    console.time(printer);\n    \n    for (let i = 0; i \u003c 1000; i++) {\n        putout(code, {\n            printer,\n            plugins: ['remove-unused-variables'],\n        });\n    }\n    \n    console.timeEnd(printer);\n}\n```\n\nWith contents of [`tokenize.js`](https://github.com/putoutjs/printer/blob/v1.69.1/lib/tokenize/tokenize.js), we have:\n\n![image](https://user-images.githubusercontent.com/1573141/234004942-8f890da3-a145-425f-9040-25924dcfba7b.png)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fputoutjs%2Fprinter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fputoutjs%2Fprinter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fputoutjs%2Fprinter/lists"}