{"id":1915,"date":"2022-05-22T12:52:37","date_gmt":"2022-05-22T10:52:37","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1915"},"modified":"2022-05-22T14:19:36","modified_gmt":"2022-05-22T12:19:36","slug":"migration-from-tslint-to-eslint","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/05\/22\/migration-from-tslint-to-eslint\/","title":{"rendered":"Migration from TSLint to ESLint"},"content":{"rendered":"\n<p>Linting is a process which checks your code for adherence to a defined set of style rules. Common linting rules are restrictions on line length or whitespace, as well as syntax checks like semicolons at the end of lines.<\/p>\n\n\n\n<p>Before Angular 11, linting was supported via a library called&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/palantir\/tslint\" target=\"_blank\">TSLint<\/a>. However, the TSLint team&nbsp;deprecated the project in 2019 and Angular followed suit in november 2020. Fortunately, thanks to tools from the Angular ecosystem migrating to&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/eslint.org\/\" target=\"_blank\">ESLint<\/a>&nbsp;is easier than you think.<\/p>\n\n\n\n<h4><strong>Why Replace TSLint?<\/strong><\/h4>\n\n\n\n<p>TSLint has been around for years, why are things changing now? A big reason: ESLint is compatible with both JavaScript and TypeScript. This makes jumping between the two languages a bit easier, especially for new developers. The core concept of TypeScript is a 100% compatible superset of JavaScript, so parity in tooling keeps the two languages in step.<\/p>\n\n\n\n<p>ESLint\u2019s config structure is a bit easier to read. TSLint also has a dependency on&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.npmjs.com\/package\/codelyzer\" target=\"_blank\">Codelyzer<\/a>, where ESLint does not<\/p>\n\n\n\n<h4><strong>Migrating an Existing Angular 10+ Project<\/strong><\/h4>\n\n\n\n<p>&nbsp;We\u2019ll use&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/angular-eslint\/angular-eslint\" target=\"_blank\">angular-eslint<\/a>&nbsp;to make migration a breeze.<\/p>\n\n\n\n<p>The community-driven library&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/angular-eslint\/angular-eslint\" target=\"_blank\">angular-eslint<\/a>&nbsp;provides an awesome set of&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/angular.io\/guide\/schematics\" target=\"_blank\">schematics<\/a>&nbsp;for migrating your existing Angular projects to ESLint. Currently,&nbsp;<strong>only Angular 10.1.0 is supported<\/strong>. If your project is running an older version of Angular, then  you\u2019ve to upgrade it first.<\/p>\n\n\n\n<p>The biggest practical difference between <em><strong>TSLint<\/strong><\/em> and <em><strong>ESLint<\/strong><\/em> is the format of their configuration file, which stores the rules the linter uses to validate your code. <em><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>We\u2019ll also need to update Angular to use ESLint when we run \u201c<\/strong><\/span><strong><span class=\"has-inline-color has-luminous-vivid-orange-color\">ng lint<\/span><\/strong><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u201d<\/strong><\/span><\/em>. Fortunately, angular-eslint\u2019s schematic handles both of these changes!<\/p>\n\n\n\n<p><strong><em>ps: <\/em><\/strong>&nbsp;<em>the changes below will update your angular.json file. Unless you have your project configuration memorized, I recommend backing up this file or making these changes in a dedicated branch, just in case!&nbsp;<\/em>For testing purposes, you can also write a few lines of code that will intentionally throw linting errors.<\/p>\n\n\n\n<p>To install the schematics, navigate to the root of your project and run the following&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/angular.io\/cli\" target=\"_blank\">Angular CLI&nbsp;<\/a>command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng add @angular-eslint\/schematics<\/code><\/pre>\n\n\n\n<p>Next, run the following to generate a new ESLint file based on the contents of your project\u2019s existing TSLint config:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g @angular-eslint\/schematics:convert-tslint-to-eslint<\/code><\/pre>\n\n\n\n<p>If you see any errors in the terminal, it is likely that ESLint was&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/angular-eslint\/angular-eslint#step-2---run-the-convert-tslint-to-eslint-schematic-on-a-project\" target=\"_blank\">unable to map a rule<\/a>. Your experience may vary depending on the degree of customization in your TSLint configuration.<\/p>\n\n\n\n<p>Once complete, you should see a new \u201c<em><strong>.eslintrc.js<\/strong><\/em><strong><em>o<\/em>n<\/strong>\u201d file at the root of your project and your angular.json <em><strong>\u201clint\u201d <\/strong><\/em>sections should now reference <em><strong>@angular-eslint<\/strong><\/em>.<\/p>\n\n\n\n<p>To test, run the following command from your project.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng lint<\/code><\/pre>\n\n\n\n<p>It will probably find some issues, like trailing spaces, and naming convention rules. Most of these can be automatically fixed with the following command. Is your working tree clean? Run this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng lint --fix<\/code><\/pre>\n\n\n\n<p>You should now see some changed files. ESLint fixes everything it can but it won&#8217;t change everything for you. That&#8217;s ok; you shouldn&#8217;t have too many new things to fix. <em>If you have some false positives, don&#8217;t worry, these won&#8217;t prevent your build from succeeding.<\/em><\/p>\n\n\n\n<p>That\u2019s it! You should see linting run against your codebase using the same rules previously used by TSLint.<\/p>\n\n\n\n<h4><strong>Wait, why is this new ESLint config so short?<\/strong><\/h4>\n\n\n\n<p>If you\u2019re used to using the tslint.json shipped with Angular, the newly generated ESLint config file may seem suspiciously short. This is mostly a result of the angular-eslint library, which provides you with an \u201coverride\u201d linting file for adding project level rules while tucking away most of the boilerplate Angular config.&nbsp;<\/p>\n\n\n\n<p>More info on properly customizing ESLint can be found on the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/angular-eslint\/angular-eslint\" target=\"_blank\">angular-eslint GitHub page<\/a>.<\/p>\n\n\n\n<p>When you\u2019re confident that your new ESLint configuration is to your liking, feel free to remove TSLint and Codelyzer from your project.<\/p>\n\n\n\n<h4><strong><span class=\"has-inline-color has-vivid-red-color\">Using ESLint for a New Angular Project<\/span><\/strong><\/h4>\n\n\n\n<p>The Angular CLI still configures new projects for use with TSLint. If you\u2019d like to create a new project with ESLint instead, you can leverage angular-eslint\u2019s schematics using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng new --collection=@angular-eslint\/schematics<\/code><\/pre>\n\n\n\n<p>This schematic runs after your new project is created, mapping the default TSLint configuration file to a new ESLint file and updating your project\u2019s angular.json (it may take a few seconds to complete).<\/p>\n\n\n\n<h4>FAZIT<\/h4>\n\n\n\n<p>Linting is an indispensable tool for producing quality code, and the migration to ESLint ensures our Angular projects will continue to stay neat and tidy as the TypeScript ecosystem evolves. Future versions of Angular and Typescript will no longer be supported by TSLint.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4><strong>Using ESLint with Angular in VScode<\/strong><\/h4>\n\n\n\n<p>First, install&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/Microsoft\/vscode-eslint\" target=\"_blank\">this ESLint extension<\/a>: it&#8217;s the top result if you search &#8220;eslint&#8221;. The author is a guy named Dirk but it is a Microsoft maintained package.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-1.png\" alt=\"\" class=\"wp-image-1919\" width=\"149\" height=\"132\"\/><\/figure><\/div>\n\n\n\n<p>Next, open the command palette (cmd\/ctrl + shift + P) and type &#8220;ESLint: Manage Library Execution&#8221;.<\/p>\n\n\n\n<p>You will be prompted by VScode to allow ESLint to run. You can choose &#8220;Allow&#8221; to just have it run in this project. Don&#8217;t choose &#8220;Allow Everywhere&#8221; unless you plan to convert&nbsp;<strong><em>all<\/em><\/strong>&nbsp;your Angular projects to ESLint right away.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-2.png\" alt=\"\" class=\"wp-image-1920\" width=\"560\" height=\"171\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-2.png 730w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-2-300x92.png 300w\" sizes=\"(max-width: 560px) 100vw, 560px\" \/><\/figure><\/div>\n\n\n\n<p><strong><em>PS<\/em><\/strong>: <em>Note that if you would like ESLint to work on your html templates, you will need to make the following change to your VScode settings:<\/em><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-3.png\" alt=\"\" class=\"wp-image-1921\" width=\"399\" height=\"338\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-3.png 494w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-3-300x254.png 300w\" sizes=\"(max-width: 399px) 100vw, 399px\" \/><\/figure><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><em>Here you can learn more about TypeScript ESLint Rules : <\/em> <a href=\"https:\/\/typescript-eslint.io\/rules\/\" data-type=\"URL\" data-id=\"https:\/\/typescript-eslint.io\/rules\/\">https:\/\/typescript-eslint.io\/rules\/<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linting is a process which checks your code for adherence to a defined set of style rules. Common linting rules are restrictions on line length or whitespace, as well as syntax checks like semicolons at the end of lines. Before Angular 11, linting was supported via a library called&nbsp;TSLint. However, the TSLint team&nbsp;deprecated the project [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1918,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,37],"tags":[546,252,248],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1915"}],"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=1915"}],"version-history":[{"count":4,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1915\/revisions"}],"predecessor-version":[{"id":1924,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1915\/revisions\/1924"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1918"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}