{"name":"@putout/printer","version":"18.40.1","keywords":["putout","printer","AST","babel","api","traverse","generate"],"author":{"url":"https://github.com/coderaiser","name":"coderaiser","email":"mnemonic.enemy@gmail.com"},"license":"MIT","_id":"@putout/printer@18.40.1","maintainers":[{"name":"coderaiser","email":"mnemonic.enemy@gmail.com"}],"homepage":"https://github.com/putoutjs/printer#readme","bugs":{"url":"https://github.com/putoutjs/printer/issues"},"dist":{"shasum":"adeec2eb1b6c76dd21399ba129edea05126f703d","tarball":"http://registry-npm.ywwl.com/@putout/printer/download/@putout/printer-18.40.1.tgz","fileCount":209,"integrity":"sha512-fSsjPDS6TvUrzq+Q0ctKBJ3/HGwZt0RsOvzg/6b9qVOZlXCp19pVLWjSlrnuyLoBOMNX7xOUBIKLI0uXS54YQA==","signatures":[{"sig":"MEYCIQDnE3XJutK4ItRcNTcye1kwlexbx6PsNq80P5cIpq6gXgIhALqWdiqn+qpIGOYTes5Jw4KzuZW2fvQoEkyoZF+Y/Py3","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":342520,"size":75237,"noattachment":false,"key":"/@putout/printer/-/@putout/printer-18.40.1.tgz"},"main":"./lib/printer.js","type":"module","types":"./lib/printer.d.ts","engines":{"node":">=22.13"},"exports":{".":"./lib/printer.js","./is":"./lib/tokenize/is.js","./params":"./lib/tokenize/expressions/function/params.js","./type-checker":"./lib/tokenize/type-checker/type-checker.js","./type-checker/report":"./lib/tokenize/type-checker/report.js","./type-checker/instrument":"./lib/tokenize/type-checker/instrument.js","./type-checker/when-tests-ends":"./lib/tokenize/type-checker/when-tests-ends.js"},"gitHead":"cbc6d2f830b13b0d4663b98e98c08a7d9097ed15","imports":{"#is":"./lib/tokenize/is.js","#mark":"./lib/tokenize/mark.js","#test":"./test/create-test.js","#types":"./lib/types.js","#printer":"./lib/printer.js","#comments":"./lib/tokenize/comments/comments.js","#maybe-parens":"./lib/tokenize/maybe/maybe-parens.js","#print-params":"./lib/tokenize/expressions/function/params.js","#test/fixture":"./test/fixture.js","#test/printer":"./test/print-extension/print-extension.js","#type-checker":"./lib/tokenize/type-checker/type-checker.js","#maybe-declare":"./lib/tokenize/maybe/maybe-declare.js","#is-concatenation":"./lib/tokenize/expressions/binary-expression/is-concatenation.js","#import-attributes":"./lib/tokenize/statements/import-declaration/import-attribute.js","#type-checker/report":"./lib/tokenize/type-checker/report.js","#type-checker/instrument":"./lib/tokenize/type-checker/instrument.js","#type-checker/when-tests-ends":"./lib/tokenize/type-checker/when-tests-ends.js"},"scripts":{"lint":"madrun lint","test":"madrun test","fresh":"madrun fresh","report":"madrun report","wisdom":"madrun wisdom","coverage":"madrun coverage","fix:lint":"madrun fix:lint","test:dts":"madrun test:dts","test:check":"madrun test:check","watch:test":"madrun watch:test","lint:putout":"madrun lint:putout","lint:redlint":"madrun lint:redlint","coverage:html":"madrun coverage:html","fix:lint:putout":"madrun fix:lint:putout","fix:lint:redlint":"madrun fix:lint:redlint"},"_npmUser":{"name":"coderaiser","email":"mnemonic.enemy@gmail.com"},"repository":{"url":"git+https://github.com/putoutjs/printer.git","type":"git"},"_npmVersion":"11.2.0","description":"Simplest possible opinionated Babel AST printer for 🐊Putout","directories":{},"_nodeVersion":"26.7.0","dependencies":{"jessy":"^5.0.0","rendy":"^5.0.0","fullstore":"^4.0.0","@putout/babel":"^5.0.0","@putout/compare":"^19.0.1","@putout/operate":"^15.0.0","just-snake-case":"^3.2.0","@putout/operator-json":"^3.1.0","parse-import-specifiers":"^1.0.1"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"acorn":"^8.8.2","eslint":"^10.0.0","madrun":"^13.0.0","montag":"^2.0.1","putout":"^42.0.1","escover":"^6.0.0","nodemon":"^3.0.1","redlint":"^6.0.0","samadhi":"^4.0.2","superc8":"^12.2.4","check-dts":"^1.0.0","goldstein":"^7.0.0","supertape":"^13.2.0","try-catch":"^4.0.6","typescript":"^6.0.2","@babel/parser":"^8.0.0","@putout/eslint":"^6.0.0","estree-to-babel":"^12.0.0","just-kebab-case":"^4.2.0","@putout/eslint-flat":"^4.0.0","eslint-plugin-putout":"^31.0.0","@putout/plugin-minify":"^12.0.0","@putout/plugin-printer":"^8.0.0","@putout/plugin-promises":"^20.0.0","@supertape/operator-strip":"^1.0.0","@putout/plugin-react-hooks":"^9.0.0","@putout/plugin-react-hook-form":"^6.0.0"},"_npmOperationalInternal":{"tmp":"tmp/printer_18.40.1_1787341042324_0.3726348011383105","host":"s3://npm-registry-packages-npm-production"},"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&longCache=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&service=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) => {const d = 5; return a;}');\n\nprint(ast);\n// returns\n`\nconst a = (b, c) => {\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 {print} = printer;\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        maxArgsInOneLine: 100,\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// 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 > 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>3)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\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\nconst {print} = printer;\n\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\nconst {print} = printer;\n\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\nconst {print} = printer;\n\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\nconst {print} = printer;\n\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\nconst {print} = printer;\n\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 < 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","publish_time":1787341042485,"_cnpm_publish_time":1787341042485,"dist-tags":{"latest":"18.49.2"}}