{"componentChunkName":"component---src-templates-post-js","path":"/blog/posts/how-to-get-prettier-eslint-play-nicely-with-vscode/","result":{"data":{"header":{"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMCBf/EABUBAQEAAAAAAAAAAAAAAAAAAAEC/9oADAMBAAIQAxAAAAHjiagKm0wchP/EABwQAAMAAQUAAAAAAAAAAAAAAAECAwAQEhMxQf/aAAgBAQABBQKKclKrtbJsFNCug68z/8QAFREBAQAAAAAAAAAAAAAAAAAAIEH/2gAIAQMBAT8Bg//EABURAQEAAAAAAAAAAAAAAAAAACBB/9oACAECAQE/AaP/xAAcEAACAgIDAAAAAAAAAAAAAAABEQACECEgMWH/2gAIAQEABj8CFeomCPMbq5qq4//EABsQAQABBQEAAAAAAAAAAAAAAAEAEBEhMUFR/9oACAEBAAE/IcuA6sv44DQgdOMbXvc0Gk9U/9oADAMBAAIAAwAAABDb537/xAAVEQEBAAAAAAAAAAAAAAAAAAABEP/aAAgBAwEBPxACCf/EABYRAAMAAAAAAAAAAAAAAAAAAAERIP/aAAgBAgEBPxBmH//EAB0QAQABBAMBAAAAAAAAAAAAAAERABAxQSFRYaH/2gAIAQEAAT8QndiXQAmg3lAIlj6FYCpD3QEJaJ+OLB4RUEYcpm3/2Q==","aspectRatio":1,"src":"/static/5a125f11859148d0d0aadc1049ac37f7/82bc5/photo_2020-01-05_20-24-37.jpg","srcSet":"/static/5a125f11859148d0d0aadc1049ac37f7/b736e/photo_2020-01-05_20-24-37.jpg 25w,\n/static/5a125f11859148d0d0aadc1049ac37f7/b2767/photo_2020-01-05_20-24-37.jpg 50w,\n/static/5a125f11859148d0d0aadc1049ac37f7/82bc5/photo_2020-01-05_20-24-37.jpg 100w,\n/static/5a125f11859148d0d0aadc1049ac37f7/decd9/photo_2020-01-05_20-24-37.jpg 150w,\n/static/5a125f11859148d0d0aadc1049ac37f7/3dcee/photo_2020-01-05_20-24-37.jpg 200w,\n/static/5a125f11859148d0d0aadc1049ac37f7/883ec/photo_2020-01-05_20-24-37.jpg 640w","srcWebp":"/static/5a125f11859148d0d0aadc1049ac37f7/4226e/photo_2020-01-05_20-24-37.webp","srcSetWebp":"/static/5a125f11859148d0d0aadc1049ac37f7/12e7b/photo_2020-01-05_20-24-37.webp 25w,\n/static/5a125f11859148d0d0aadc1049ac37f7/ecb58/photo_2020-01-05_20-24-37.webp 50w,\n/static/5a125f11859148d0d0aadc1049ac37f7/4226e/photo_2020-01-05_20-24-37.webp 100w,\n/static/5a125f11859148d0d0aadc1049ac37f7/e8bcc/photo_2020-01-05_20-24-37.webp 150w,\n/static/5a125f11859148d0d0aadc1049ac37f7/1c9b5/photo_2020-01-05_20-24-37.webp 200w,\n/static/5a125f11859148d0d0aadc1049ac37f7/2420f/photo_2020-01-05_20-24-37.webp 640w","sizes":"(max-width: 100px) 100vw, 100px","presentationWidth":100}}},"page":{"fields":{"url":"/blog/posts/how-to-get-prettier-eslint-play-nicely-with-vscode/"},"frontmatter":{"title":"How to get ESlint and Prettier to play nice in VS Code?","subtitle":"Set up eslint to run after prettier, or even before, you decide","keywords":[],"template":"post","canonical_url":"","noindex":null,"date":"2020-04-28T18:30:00.000Z","tweet":null,"github":"rohit-gohri/vscode-format-code-action","content_img_path":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='450'%20height='300'%20viewBox='0%200%20450%20300'%20preserveAspectRatio='none'%3e%3cpath%20d='M73%2029l-2%202%2018%2020%2019%2019-18%2017-18%2019c-1%203%201%206%203%206%203%200%2043-40%2043-43%200-2-40-42-42-42l-3%202m68%2018c-2%201-2%202-2%204%202%205%2018%204%2018-1%200-4-12-6-16-3m38%201c-3%204-1%206%208%206%207%200%208-1%208-3%200-4-2-5-9-5-5%200-6%200-7%202m39%200c-4%203-1%206%207%206%206%200%209-1%209-4%200-2-3-4-9-4-4%200-6%201-7%202m38-1l-1%204c0%202%201%203%208%203%209%200%2011-2%208-6-1-3-13-3-15-1m39%201c-4%204-1%206%208%206%207%200%207%200%208-3s-2-5-9-5c-4%200-6%201-7%202m39-1c-2%201-2%203-2%204%201%204%2015%204%2017%201%201-2%201-3-1-4-2-2-12-3-14-1M140%2086c-4%204%200%207%208%207s10-2%208-6c-1-2-14-3-16-1m38%201c-2%204%200%206%208%206%207%200%209-1%209-5%200-2-2-3-10-3-5%200-6%200-7%202m39-1c-4%204%200%207%208%207%207%200%207%200%208-2%202-5%200-6-8-6l-8%201m39%200v6c3%203%2017%201%2017-3%200-3-2-4-9-4l-8%201m38%201c-2%204%200%206%208%206%208-1%2011-2%208-6-1-3-14-3-16%200m39-1c-4%204%200%207%208%207s10-2%208-6c-1-2-14-3-16-1m-193%2040c-2%205-8%205%20100%205%2095%200%2099%200%2099-2%202-5%208-5-100-5-95%200-99%200-99%202m0%2037l-1%203c0%204-2%204%20120%204%20116%200%20118%200%20119-2s1-3-1-4c-2-3-235-3-237-1m-1%2041c-1%205-2%205%2062%205%2062%200%2062%200%2062-4s-1-4-63-4h-60l-1%203'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1,"src":"/static/065f6be0f4125d89e1ee80e788d1d416/2ffe0/format-code-action.png","srcSet":"/static/065f6be0f4125d89e1ee80e788d1d416/f973b/format-code-action.png 113w,\n/static/065f6be0f4125d89e1ee80e788d1d416/20493/format-code-action.png 225w,\n/static/065f6be0f4125d89e1ee80e788d1d416/2ffe0/format-code-action.png 450w,\n/static/065f6be0f4125d89e1ee80e788d1d416/bfa90/format-code-action.png 675w,\n/static/065f6be0f4125d89e1ee80e788d1d416/ce4a2/format-code-action.png 700w","srcWebp":"/static/065f6be0f4125d89e1ee80e788d1d416/4fe5d/format-code-action.webp","srcSetWebp":"/static/065f6be0f4125d89e1ee80e788d1d416/74efb/format-code-action.webp 113w,\n/static/065f6be0f4125d89e1ee80e788d1d416/5da3e/format-code-action.webp 225w,\n/static/065f6be0f4125d89e1ee80e788d1d416/4fe5d/format-code-action.webp 450w,\n/static/065f6be0f4125d89e1ee80e788d1d416/f9eb5/format-code-action.webp 675w,\n/static/065f6be0f4125d89e1ee80e788d1d416/86d6b/format-code-action.webp 700w","sizes":"(max-width: 450px) 100vw, 450px","presentationWidth":450}}}}}},"pageContext":{"url":"/blog/posts/how-to-get-prettier-eslint-play-nicely-with-vscode/","relativePath":"blog/posts/how-to-get-prettier-eslint-play-nicely-with-vscode.md","relativeDir":"blog/posts","base":"how-to-get-prettier-eslint-play-nicely-with-vscode.md","name":"how-to-get-prettier-eslint-play-nicely-with-vscode","html":"<p>Do you have your project setup with both Prettier and Eslint? Do you want them to run in a specific order instead of <a href=\"https://prettier.io/docs/en/integrating-with-linters.html#use-eslint-to-run-prettier\" target=\"_blank\" rel=\"noopener noreferrer\">using ESLint to run prettier?</a> Here's how to set your own order in VS Code with this extension:</p>\n<h3><a href=\"https://marketplace.visualstudio.com/items?itemName=rohit-gohri.format-code-action\" target=\"_blank\" rel=\"noopener noreferrer\">Visual Studio Code Market Place: Format Code Action</a></h3>\n<p><img src=\"https://img.shields.io/visual-studio-marketplace/v/rohit-gohri.format-code-action\" alt=\"Version\">   <img src=\"https://img.shields.io/visual-studio-marketplace/r/rohit-gohri.format-code-action\" alt=\"Ratings\"></p>\n<p>This extension will register a new <a href=\"https://code.visualstudio.com/updates/v1_23#_run-code-actions-on-save\" target=\"_blank\" rel=\"noopener noreferrer\">\"codeAction\"</a> which will run 'Format Document' using the default formatter (prettier in our case) in VS Code.</p>\n<pre><code>source.formatDocument\n</code></pre>\n<p>Using this and the <code>editor.codeActionsOnSave</code> setting in VS Code you can specify the order in which to run the eslint codeAction, <code>source.fixAll.eslint</code>, and format document.</p>\n<h2>Install</h2>\n<p><strong>Requires: VS Code 1.44+</strong></p>\n<p>Install through VS Code extensions:</p>\n<p><a href=\"https://marketplace.visualstudio.com/items?itemName=rohit-gohri.format-code-action\" target=\"_blank\" rel=\"noopener noreferrer\">Visual Studio Code Market Place: Format Code Action</a></p>\n<p>Can also be installed in VS Code: Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.</p>\n<pre><code>ext install rohit-gohri.format-code-action\n</code></pre>\n<h2>Usage</h2>\n<p>You will need both the <a href=\"https://github.com/prettier/prettier-vscode\" target=\"_blank\" rel=\"noopener noreferrer\"><code>prettier-vscode</code></a> and <a href=\"https://github.com/microsoft/vscode-eslint\" target=\"_blank\" rel=\"noopener noreferrer\"><code>vscode-eslint</code></a> extensions installed for this config to work.\nDisbale <code>formatOnSave</code> and use the <code>source.formatDocument</code> codeAction in whatever order you want with VS Code settings:</p>\n<pre><code class=\"language-json\">  \"editor.formatOnSave\": false,\n  \"editor.defaultFormatter\": \"esbenp.prettier-vscode\",\n  \"editor.codeActionsOnSave\": [\n    \"source.formatDocument\",\n    \"source.fixAll.eslint\"\n  ]\n</code></pre>\n<p>This runs 'Format Document' with the default formatter (in this case prettier) and then <code>eslint --fix</code> for all document types.</p>\n<h3>Or for a specific lanuage only</h3>\n<pre><code class=\"language-json\">  \"editor.formatOnSave\": true,\n  \"editor.defaultFormatter\": \"esbenp.prettier-vscode\",\n  \"[javascript]\": {\n    \"editor.formatOnSave\": false,\n    \"editor.codeActionsOnSave\": [\n        \"source.formatDocument\",\n        \"source.fixAll.eslint\"\n    ]\n  },\n</code></pre>\n<p>This would run prettier by default, but for javascript files would run prettier and then eslint. If you want to reverse the order then just reverse the array.</p>\n<h2>Motivation</h2>\n<p>I wanted to use prettier and eslint together, to override some of prettier's mpre opinionated style rules with a eslint config (particularly <a href=\"https://github.com/prettier/eslint-config-prettier/issues/26\" target=\"_blank\" rel=\"noopener noreferrer\">brace-style</a>).</p>\n<p>Instead of using workarounds like <a href=\"https://github.com/prettier/prettier-eslint\" target=\"_blank\" rel=\"noopener noreferrer\"><code>prettier-eslint</code></a> which don't work for all eslint configurations as it hasn't been updated to work with <a href=\"https://github.com/prettier/prettier-eslint/issues/222\" target=\"_blank\" rel=\"noopener noreferrer\">eslint v6 properly</a>. We can simply run prettier and eslint one by one like one would do on the command line. Here is how that looks like in my <code>package.json</code>:</p>\n<pre><code class=\"language-json\">  \"scripts\": {\n    \"format\": \"npm run prettier:fix &#x26;&#x26; npm run lint:fix\",\n    \"lint\": \"eslint src --ext=js\",\n    \"lint:fix\": \"npm run lint -- --fix\",\n    \"prettier\": \"prettier -c\",\n    \"prettier:fix\": \"npm run prettier -- --write\"\n  }\n</code></pre>\n<p>The <code>npm run format</code> command allows me to fix some of the prettier formatting with eslint by running them both in an order. I wanted to replicate this same behavior with my editor's \"Format on Save\" functionality.</p>\n<p>VS Code only allows setting one default formatter. So I could either run Prettier or run ESLint on save. That was until it introduced <a href=\"https://code.visualstudio.com/updates/v1_23#_run-code-actions-on-save\" target=\"_blank\" rel=\"noopener noreferrer\">\"codeActionsOnSave\"</a>. This separated formatters and \"Source\" fixers like <code>vscode-eslint</code>. This is how one would enable both prettier and eslint in VS Code:</p>\n<pre><code class=\"language-json\">\"editor.formatOnSave\": true,\n\"editor.defaultFormatter\": \"esbenp.prettier-vscode\",\n\"editor.codeActionsOnSave\": {\n  \"source.fixAll.eslint\": true\n}\n</code></pre>\n<p>But this would often lead to prettier being run after eslint, and eslint errors still being present as this defined no order for formatters and coder actions.</p>\n<p>Since the <a href=\"https://code.visualstudio.com/updates/v1_44#_explicit-ordering-for-code-actions-on-save\" target=\"_blank\" rel=\"noopener noreferrer\">March 2020 (v1.44)</a> update, VS Code allows setting <code>codeActionsOnSave</code> as an array. This allows setting the order of the code actions.</p>\n<p>This extension uses the <a href=\"https://code.visualstudio.com/api/references/vscode-api#CodeActionProvider\" target=\"_blank\" rel=\"noopener noreferrer\"><code>CodeActionProvider</code></a> api to implement a simple code action that runs 'Format Document' on the current file. This allows us to disable <code>formatOnSave</code> and use it as a codeAction instead in a specific order with other extensions.</p>","headerImg":"photo_2020-01-05_20-24-37.jpg"}},"staticQueryHashes":["1161966454","1673585186","1866307108","219491723","2948329147","3269044456","629512913"]}