{"id":15645948,"url":"https://github.com/simonbs/prettier","last_synced_at":"2025-04-30T11:45:00.744Z","repository":{"id":40535615,"uuid":"441998681","full_name":"simonbs/Prettier","owner":"simonbs","description":"✨ Wrapper for the Prettier code formatter written in Swift","archived":false,"fork":false,"pushed_at":"2023-07-11T00:08:09.000Z","size":4017,"stargazers_count":52,"open_issues_count":0,"forks_count":8,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-13T08:16:09.060Z","etag":null,"topics":["formatter","ios","prettier","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simonbs.png","metadata":{"files":{"readme":"README.md","changelog":null,"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},"funding":{"github":"simonbs"}},"created_at":"2021-12-26T22:53:59.000Z","updated_at":"2025-01-02T01:33:19.000Z","dependencies_parsed_at":"2024-10-23T02:28:49.336Z","dependency_job_id":null,"html_url":"https://github.com/simonbs/Prettier","commit_stats":{"total_commits":40,"total_committers":2,"mean_commits":20.0,"dds":"0.025000000000000022","last_synced_commit":"266dac500bded844a56771bc1fb2abaa60f6e7b6"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonbs%2FPrettier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonbs%2FPrettier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonbs%2FPrettier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonbs%2FPrettier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simonbs","download_url":"https://codeload.github.com/simonbs/Prettier/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249500244,"owners_count":21282371,"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":["formatter","ios","prettier","swift"],"created_at":"2024-10-03T12:10:41.290Z","updated_at":"2025-04-18T13:33:51.240Z","avatar_url":"https://github.com/simonbs.png","language":"Swift","funding_links":["https://github.com/sponsors/simonbs"],"categories":[],"sub_categories":[],"readme":"# Prettier\n\nA wrapper for the [Prettier](https://prettier.io) code formatter written in Swift.\nThe package runs the code formatter in Apple's [JavaScriptCore](https://developer.apple.com/documentation/javascriptcore).\nAll the necessary JavaScript files are bundled with this package.\n\n### Can this package be used in production?\n\nProbably. The package uses stable versions of all dependencies and I intend to use it in one of my upcoming projects. Use it at your own risk though.\n\n## Installation\n\nPrettier is distributed using the [Swift Package Manager](https://www.swift.org/package-manager/). Install it in your project by adding it as a dependency in your Package.swift manifest or through \"Package Dependencies\" in your project settings.\n\n```swift\nlet package = Package(\n    dependencies: [\n        .package(url: \"git@github.com:simonbs/Prettier.git\", from: \"0.1.0\")\n    ]\n)\n```\n\n## Usage\n\nTo get started with Prettier you must first import the module.\n\n```swift\nimport Prettier\n```\n\nNow you can create an instance of PrettierFormatter.\n\n```swift\nlet formatter = PrettierFormatter(plugins: [BabelPlugin()], parser: BabelParser())\n```\n\nWe need to pass the plugins to use with Prettier. Some documents can be formatted with a single plugin and some may require multiple parsers if the document includes a mix of languages. An example of this is HTML documents that may contain HTML, CSS and JavaScript.\n\nNext we're passing the parser to use to use for formatting the code.\n\nTo format HTML we would create our PrettierFormatter as shown below.\n\n```swift\nlet formatter = PrettierFormatter(plugins: [HTMLPlugin(), PostCSSPlugin(), BabelPlugin()] , parser: HTMLParser())\n```\n\nSee the [Supported Languages](#supported-languages) for a list of all plugins and parsers.\n\nAfter creating an instance of PrettierFormatter, you must prepare the formatter before formatting code.\n\n```swift\nformatter.prepare()\n```\n\nThis prepares the instance by loading the bundled JavaScript files from disk and evaluating them in JavaScriptCore.\nIt is recommended to do this as early as possible so you're ready to format the code later.\n\nWith the PrettierFormatter instance prepared you can start formatting code.\n\n```swift\n// 💩 This is some poorly formatted JavaScript that we'll format.\nlet script = \"\"\"\nif(hello==\"world\"){\nreturn\"Hello world\"\n}\n\"\"\"\n// Earlier we have created and prepared our instance of Prettier. We're ready to format the JavaScript code.\nlet result = formatter.format(script)\nswitch result {\ncase .success(let formattedCode):\n    print(formattedCode)\n    // ✨ Here's our formatted code. Looks great!\n    // if (hello == \"world\") {\n    //   return \"Hello world\";\n    // }\ncase .failure(let error):\n    // Perform proper error handling.\n    print(error)\n}\n```\n\nIt's possible to format only a specific range in the input string by calling `-format(:limitedTo:)`.\n\n```swift\nlet result = formatter.format(script, limitedTo: 20 ... 39)\nswitch result {\ncase .success(let formattedCode):\n    break\ncase .failure(let error):\n    // Perform proper error handling.\n    print(error)\n}\n```\n\nYou can also specify a location to be translated from the unformatted code to the formatted code.\nThis is useful for moving the cursor when developing a text editor.\n\n```swift\nlet result = formatter.format(script, withCursorAtLocation: 38)\nswitch result {\ncase .success(let formatResult):\n    print(formatResult.formattedString)\n    print(formatResult.cursorOffset)\ncase .failure(let error):\n    // Perform proper error handling.\n    print(error)\n}\n```\n\nNote that the result returned by `-format(:withCursorAtLocation:)` is different that the result returned\nfrom both `-format()` and `-format(:limitedTo:)` because this one contains the location in the formatted string.\n\nThere are several properties that can be used to tweak the formatted code. These options map 1:1 to options in the JavaScript implementation of Prettier.\nFor more information on the options, please have a look at [PrettierFormatter.swift](https://github.com/simonbs/Prettier/blob/main/Sources/Prettier/PrettierFormatter.swift) as well as [the documentation for the JavaScript implementation of Prettier](https://prettier.io/docs/en/options.html).\n\n### Supported Languages\n\nThis package bundles the Prettier library along with support for the following languages and frameworks. The table below shows the language name and the parser to be used in order to format code in that language. A parser can support more languages.\n\n|Package|Parsers||\n|-|-|-|\n|PrettierBabel|BabelFlowParser, BabelParser, BabelTSParser, JSON5Parser, JSONParser, JSONStringifyParser|BabelParser can be used for parsing JavaScript.|\n|PrettierEspree|EspreeParser||\n|PrettierFlow|FlowParser||\n|PrettierGlimmer|GlimmerParser|Used for Handlebars.|\n|PrettierGraphQL|GraphQLParser||\n|PrettierHTML|AngularParser, HTMLParser, LWCParser, VueParser|Include BabelParser and CSSParser to parse entire HTML documents. HTMLParser can be used to parse Vue.|\n|PrettierMarkdown|MarkdownParser, MDXParser||\n|PrettierMeriyah|MeriyahParser||\n|PrettierPHP|PHPParser||\n|PrettierPostCSS|CSSParser, LessParser, SCSSParser||\n|PrettierTypeScript|TypeScriptParser||\n|PrettierYAML|YAMLParser||\n\n## Acknowledgements\n\nThe package uses the following dependencies.\n\n- [prettier/prettier](https://github.com/prettier/prettier)\n- [prettier/plugin-php](https://github.com/prettier/plugin-php)\n\n## License\n\n[MIT](https://github.com/simonbs/Prettier/blob/main/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonbs%2Fprettier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonbs%2Fprettier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonbs%2Fprettier/lists"}