{"id":15077861,"url":"https://github.com/prettier/prettier-printer","last_synced_at":"2025-10-05T12:31:51.843Z","repository":{"id":65982373,"uuid":"93664144","full_name":"prettier/prettier-printer","owner":"prettier","description":"Library for building and pretty printing text documents","archived":true,"fork":false,"pushed_at":"2017-06-09T19:14:18.000Z","size":241,"stargazers_count":23,"open_issues_count":0,"forks_count":5,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-29T22:37:35.116Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://prettier.github.io/prettier-printer/","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/prettier.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-07T17:57:55.000Z","updated_at":"2023-11-15T21:00:29.000Z","dependencies_parsed_at":"2023-02-19T18:30:51.996Z","dependency_job_id":null,"html_url":"https://github.com/prettier/prettier-printer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prettier%2Fprettier-printer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prettier%2Fprettier-printer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prettier%2Fprettier-printer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prettier%2Fprettier-printer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prettier","download_url":"https://codeload.github.com/prettier/prettier-printer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235375295,"owners_count":18979852,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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-09-25T04:33:11.608Z","updated_at":"2025-10-05T12:31:46.270Z","avatar_url":"https://github.com/prettier.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# prettier-printer\n\nThis is pretty printer library implemented as described by Wadler in \"[A prettier printer][]\" paper. The library is based on a single way to concatenate documents, which is associative and has a left and right unit.\n\n### Overview\n\n\nThis is very rough documentation of the formatting commands you can\nuse to build a printed version of something. This will be improved\nover time.\n\nThe printer should use the basic formatting abstractions provided to construct\na document. Parts of the API only exist to be compatible with recast's previous API to ease migration, but over time we can clean it up.\n\nThe following commands are available:\n\n### `concat`\n\nCombine an array into a single string.\n\n### `group`\n\nMark a group of items which the printer should try to fit on one line.\nThis is the basic command to tell the printer when to break. Groups\nare usually nested, and the printer will try to fit everything on one\nline, but if it doesn't fit it will break the outermost group first\nand try again. It will continue breaking groups until everything fits\n(or there are no more groups to break).\n\nA document can force parent groups to break by including `breakParent`\n(see below). A \"hard\" and \"literal\" line automatically include this so\nthey always break parent groups. Breaks are propagated to all parent\ngroups, so if a deeply nested expression has a hard break, everything\nwith break. This only matters for \"hard\" breaks, i.e. newlines that\nare printed no matter what and can be statically analyzed.\n\nFor example, an array will try to fit on one line:\n\n```js\n[1, \"foo\", { bar: 2 }]\n```\n\nHowever, if any of the items inside the array have a hard break, the\narray will *always* break as well:\n\n```js\n[\n  1,\n  function() {\n    return 2\n  },\n  3\n]\n```\n\nFunctions always break after the opening curly brace no matter what,\nso the array breaks as well for consistent formatting. See the\nimplementation of `ArrayExpression` for an example.\n\n### `conditionalGroup`\n\nThis should be used as **last resort** as it triggers an exponential complexity when nested. This will try to print the first argument, if it fit use it, otherwise go to the next one and so on.\n\n```js\nconditionalGroup([a, b, c])\n```\n\n### `fill`\n\nThis is an alternative type of group which behave like text layout: it's going to add a break whenever the next element doesn't fit in the line anymore. The difference with a typical group is that it's not going to break all the separators, just the ones that are at the end of lines.\n\n```js\nfill([\"I\", line, \"love\", line, \"prettier\"])\n```\n\n\n### `ifBreak`\n\nPrints something if the current group breaks and something else if it doesn't.\n\n```js\nifBreak(\";\", \" \")\n```\n\n### breakParent\n\nInclude this anywhere to force all parent groups to break. See `group`\nfor more info. Example:\n\n```js\ngroup(\n  concat([\n    \" \",\n    expr,\n    \" \",\n    breakParent\n  ])\n)\n```\n\n### `join`\n\nJoin an array of items with a separator.\n\n### `line`\n\nSpecify a line break. If an expression fits on one line, the line\nbreak will be replaced with a space. Line breaks always indent the\nnext line with the current level of indentation.\n\n### `softline`\n\nSpecify a line break. The difference from `line` is that if the\nexpression fits on one line, it will be replaced with nothing.\n\n### `hardline`\n\nSpecify a line break that is **always** included in the output, no\nmatter if the expression fits on one line or not.\n\n### `literalline`\n\nSpecify a line break that is **always** included in the output, and\ndon't indent the next line. This is used for template literals.\n\n### `lineSuffix`\n\nThis is used to implement trailing comments. In practice, it is not practical to find where the line ends and you don't want to accidentally print some code at the end of the comment. `lineSuffix` will buffer the output and flush it before any new line.\n\n```js\nconcat([\"a\", lineSuffix(\" // comment\"), \";\", hardline])\n```\n\nwill output\n\n```js\na; // comment\n```\n\n### `lineSuffixBoundary`\n\nIn cases where you embed code inside of templates, comments shouldn't be able to leave the code part. lineSuffixBoundary is an explicit marker you can use to flush code in addition to newlines.\n\n```js\nconcat([\"{\", lineSuffix(\" // comment\"), lineSuffixBoundary, \"}\", hardline])\n```\n\nwill output\n\n```js\n{ // comment\n}\n```\n\nand **not**\n\n```js\n{} // comment\n```\n\n### `indent`\n\nIncrease the level of indentation.\n\n### `align`\n\nThis is similar to indent but it increases the level of indentation by a fixed number. When using tabs, it's going to print spaces. You should prefer using `indent` whenever possible.\n\n### cursor\n\nThis is a placeholder value where the cursor is in the original input in order to find where it would be printed.\n\n## Example\n\nFor an example, here's the implementation of the `ArrayExpression` node type:\n\n```js\ngroup(\n  concat([\n    \"[\",\n    indent(\n      options.tabWidth,\n      concat([\n        line,\n        join(\n          concat([\",\", line]),\n          path.map(print, \"elements\")\n        )\n      ])\n    ),\n    line,\n    \"]\"\n  ])\n)\n```\n\nThis is a group with opening and closing brackets, and possibly\nindented contents. Because it's a `group` it will always be\nbroken up if any of the sub-expressions are broken.\n\n\n[A prettier printer]:http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprettier%2Fprettier-printer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprettier%2Fprettier-printer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprettier%2Fprettier-printer/lists"}