{"id":963,"date":"2021-03-21T22:59:56","date_gmt":"2021-03-21T21:59:56","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=963"},"modified":"2021-03-21T22:59:56","modified_gmt":"2021-03-21T21:59:56","slug":"how-to-format-your-angular-code-with-prettier","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/03\/21\/how-to-format-your-angular-code-with-prettier\/","title":{"rendered":"How to format your Angular code with Prettier?"},"content":{"rendered":"\n<p>From project to project, code formatting rules can vary significantly and it is not always easy to follow them exactly.<\/p>\n\n\n\n<p>So, to encourage a particular formatting within your team, you can write a long contribution guide, but it will take a lot of energy to promote it and even more to make sure your teammates use it.<\/p>\n\n\n\n<p>Instead, there is a much &#8220;skinny&#8221; and efficient solution : An automatable code formatting tool: <em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Prettier<\/span><\/em>.<\/p>\n\n\n\n<h4>What is about  TSLint? <\/h4>\n\n\n\n<p>TSLint is part of the toolkit of any Angular application generated with Angular CLI. As a reminder, TSLint is a static analysis tool, which defines rules to increase the quality of TypeScript code in terms of <strong>readability<\/strong>, <strong>maintainability<\/strong> and<strong> functionality<\/strong>.<\/p>\n\n\n\n<p>Here is an example rule for each category:<\/p>\n\n\n\n<ul><li>Restrict the maximum number of characters per line (max-line-length) makes the code more readable<\/li><li>Limiting the number of<strong> &#8220;if bloc&#8221;<\/strong> nested in a function (cyclomatic-complexity) makes the code more maintainable.<\/li><li>Declaring variables only if they are used (no-unused-variable) ensures the correct functionality of the program<\/li><\/ul>\n\n\n\n<p><em>TSLint is able to identify formatting error<\/em>, <span class=\"has-inline-color has-vivid-red-color\"><strong>but<\/strong><\/span><span class=\"has-inline-color has-vivid-cyan-blue-color\"> <\/span><em><span class=\"has-inline-color has-black-color\">TSLint does not fully automate code formatting. For example, the <strong>max-line-length<\/strong> rule cannot be corrected automatically (we cannot run<strong> tslint &#8211;fix<\/strong> on the command line for this rule) where Prettier can take care of this very well. In addition TSLint only formats TypeScript files while Prettier formats other types of files such as HTML and CSS files for example.<\/span><\/em><\/p>\n\n\n\n<p>That\u2019s why we\u2019re going to keep TSLint for what it does best &#8211; finding functional errors and taking Prettier for automating code formatting.<\/p>\n\n\n\n<h4><br>Configure Prettier<\/h4>\n\n\n\n<p>Let&#8217;s add a <strong>.prettierrc<\/strong> file to the root of the workspace ( project):<\/p>\n\n\n\n<p>This configuration file will override the default Prettier configuration. We just need to add the specific properties that correspond to our need.<\/p>\n\n\n\n<p>Let&#8217;s use the single quotes with the singleQuote property and increase the line length to 140 characters, to stick closer to the original TSLint configuration.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\r\n \"singleQuote\": true, \r\n\"printWidth\": 140\r\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p> However, nothing prevents you from changing these rules to adapt them to your style.<\/p>\n\n\n\n<p>Next, let&#8217;s add another <strong>.prettierignore<\/strong> file to list the files we don&#8217;t want to format.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-6-1024x241.png\" alt=\"\" class=\"wp-image-967\" width=\"1008\" height=\"237\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-6-1024x241.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-6-300x71.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-6-768x181.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-6.png 1080w\" sizes=\"(max-width: 1008px) 100vw, 1008px\" \/><\/figure>\n\n\n\n<p>This configuration is based on the syntax of the .gitignore files. We indicate here that we must ignore all files in the workspace except \/ e2e, \/ projects and \/ src which contain our business code.<\/p>\n\n\n\n<p>Finally, inside the remaining folders, we ignore some folders (like assets \/) and some files (like package.json and package-lock.json, which formatting  is handled automatically by NPM).<\/p>\n\n\n\n<h4>Configure TSLint so that it does not interfere with Prettier<\/h4>\n\n\n\n<p>Prettier is now well configured, <span class=\"has-inline-color has-vivid-red-color\">but<\/span>  We still can have<strong> conflict<\/strong> between TSLint and Prettier . <span class=\"has-inline-color has-vivid-red-color\">Let us disable the code formatting rules specific to TSLint, in order to leave Prettier free to do this work<\/span>.<\/p>\n\n\n\n<p>For that, we are going to install the <strong>tslint-config-prettier<\/strong> package<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm i -D tslint-config-prettier<\/code><\/pre>\n\n\n\n<p>The contents of this package are very simple and this is what the configuration it provides looks like:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-7-1024x246.png\" alt=\"\" class=\"wp-image-968\" width=\"968\" height=\"232\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-7-1024x246.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-7-300x72.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-7-768x184.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-7.png 1083w\" sizes=\"(max-width: 968px) 100vw, 968px\" \/><\/figure>\n\n\n\n<p>As you can see, it just sets all the code formatting rules that TSLint supports to false.<\/p>\n\n\n\n<p>To use it, let&#8217;s add it to the <strong>tslint.json<\/strong> file at the root of our workspace, which allows us to configure TSLint:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-8-1024x333.png\" alt=\"\" class=\"wp-image-969\" width=\"1000\" height=\"325\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-8-1024x333.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-8-300x97.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-8-768x250.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-8.png 1077w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p><strong>Be careful<\/strong>, place the tslint-config-prettier configuration in the last position of the extends array.<\/p>\n\n\n\n<p>The <strong>rules<\/strong> section of that same tslint.json file has <strong>priority<\/strong> over what is defined in the <strong>extends<\/strong> section. We then have to remove the code formatting rules from this section (<strong>rules<\/strong>), without touching the rules for static analysis of Angular specific functional errors.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here are the rules that we must remove:<\/p>\n\n\n\n<ul><li><code>arrow-parens<\/code><\/li><li><code>max-line-length<\/code><\/li><li><code>no-consecutive-blank-lines<\/code><\/li><li><code>quotemark<\/code><\/li><li><code>trailing-comma<\/code><\/li><\/ul>\n\n\n\n<p>To identify these rules, we have many solutions. One of them is:<\/p>\n\n\n\n<ul><li>see the page<span class=\"has-inline-color has-vivid-cyan-blue-color\"> http:\/\/unpkg.com\/tslint-config-prettier\/lib\/index.json<\/span> which lists the rules in question.<\/li><\/ul>\n\n\n\n<p>Prettier and TSLint are now well configured in our Angular application.<\/p>\n\n\n\n<h4>Run Prettier on the workspace<\/h4>\n\n\n\n<p>To run Prettier, we now need to install it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm i -D prettier<\/code><\/pre>\n\n\n\n<p>Option -D is like &#8211;save-dev . Package will appear in your &#8216;devDependencies&#8217;.<\/p>\n\n\n\n<p>The executable is accessible from the <strong>.\/node_modules\/.bin\/prettier<\/strong> folder.<\/p>\n\n\n\n<p>We can run the command with the<strong> &#8211;check <\/strong>option (to list problematic files without modifying them) or with the <strong>&#8211;write <\/strong>option (to modify them). It is magic !<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-9-1024x489.png\" alt=\"\" class=\"wp-image-970\" width=\"967\" height=\"461\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-9-1024x489.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-9-300x143.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-9-768x367.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-9.png 1092w\" sizes=\"(max-width: 967px) 100vw, 967px\" \/><\/figure>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\">we can also add an NPM script to the<strong> package.json file<\/strong>, to make our life easier:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-10-1024x290.png\" alt=\"\" class=\"wp-image-971\" width=\"962\" height=\"272\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-10-1024x290.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-10-300x85.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-10-768x217.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-10.png 1067w\" sizes=\"(max-width: 962px) 100vw, 962px\" \/><\/figure>\n\n\n\n<h4>Integrate Prettier into our IDE<\/h4>\n\n\n\n<p>If  you use VSCode, I suggest you install the plugin Prettier &#8211; Code formatter. This will allow us to format a file opened in our IDE from the \u201cFormat document\u201d context menu, without going through the command line.<\/p>\n\n\n\n<p>We can even go further by configuring VSCode to format an open file as soon as we save it, with the editor.formatOnSave option:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-11-1024x150.png\" alt=\"\" class=\"wp-image-972\" width=\"1105\" height=\"161\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-11-1024x150.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-11-300x44.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-11-768x112.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-11.png 1054w\" sizes=\"(max-width: 1105px) 100vw, 1105px\" \/><\/figure>\n\n\n\n<h4>Run Prettier before every commit with husky and pretty-quick!<\/h4>\n\n\n\n<p>If  you use Git as your version-file manager, then I suggest you add a &#8220;hook&#8221; to run Prettier before each commit, on all modified files. As a reminder, &#8220;hooks&#8221; are quite simply scripts that run automatically when a specific action is triggered. also like  Taskrunner.<\/p>\n\n\n\n<p>Let&#8217;s install 2 other packages which will allow us to  simply run Prettier before each commit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm i -D husky pretty-quick<\/code><\/pre>\n\n\n\n<ul><li><strong>husky<\/strong>: allows to configure Git hooks.<\/li><li><strong>pretty-quick<\/strong>: allows to run Prettier only on files that have been modified.<\/li><\/ul>\n\n\n\n<p>Now let\u00b4s configure husky to run pretty-quick before each commit. For example, we can fill in this configuration in the <strong>package.json<\/strong> file:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-12-1024x318.png\" alt=\"\" class=\"wp-image-973\" width=\"1005\" height=\"312\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-12-1024x318.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-12-300x93.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-12-768x238.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/03\/image-12.png 1079w\" sizes=\"(max-width: 1005px) 100vw, 1005px\" \/><\/figure>\n\n\n\n<p>And that\u00b4s it. <\/p>\n\n\n\n<p><a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Customizing-Git-Git-Hooks\"> more about git hooks<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5>Reference<\/h5>\n\n\n\n<p><a href=\"https:\/\/github.com\/xebia-france\/angular-prettier\">https:\/\/github.com\/xebia-france\/angular-prettier<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>From project to project, code formatting rules can vary significantly and it is not always easy to follow them exactly. So, to encourage a particular formatting within your team, you can write a long contribution guide, but it will take a lot of energy to promote it and even more to make sure your teammates [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":974,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,83],"tags":[250,251,249,248],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/963"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=963"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/963\/revisions"}],"predecessor-version":[{"id":975,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/963\/revisions\/975"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/974"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}