<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Lokesh Devasenapathi</title>
    <description>The latest articles on Forem by Lokesh Devasenapathi (@lokdevp).</description>
    <link>https://forem.com/lokdevp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F868068%2F9784cae7-3467-42b2-bc07-6658b0de8377.jpeg</url>
      <title>Forem: Lokesh Devasenapathi</title>
      <link>https://forem.com/lokdevp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lokdevp"/>
    <language>en</language>
    <item>
      <title>Adding ESLint to existing react project</title>
      <dc:creator>Lokesh Devasenapathi</dc:creator>
      <pubDate>Wed, 28 Sep 2022 14:38:45 +0000</pubDate>
      <link>https://forem.com/lokdevp/adding-eslint-to-existing-react-project-1nhd</link>
      <guid>https://forem.com/lokdevp/adding-eslint-to-existing-react-project-1nhd</guid>
      <description>&lt;p&gt;Adding ESLint to an existing project is complicated and disrupting for the git history as well the developers. We have to try to make the disruption as minimal as possible, so it is better to follow an incremental approach. I'll be laying out the approaches regardless of the ESLint config you will use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;package.json and ESLint config&lt;/li&gt;
&lt;li&gt;git pre-commit hooks or husky&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Deciding on the ESLint config
&lt;/h2&gt;

&lt;p&gt;Make the editor ready with a config and the red/yellow underlines visible. Also run lint in the console and share the results with the team. Then discuss with the team and list out the rules that needs to be disabled, added or converted into warning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rules&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no-unused-vars&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;warn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react/jsx-indent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;off&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to edit the deployment workflow config to ignore warnings. &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  The strategies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Disable ESLint for all files at the top&lt;/li&gt;
&lt;li&gt;Disable ESLint using the env variable&lt;/li&gt;
&lt;li&gt;Fix the most and disable the rest&lt;/li&gt;
&lt;li&gt;Convert all errors to warnings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  👉🏽 Disable ESLint for all files at the top
&lt;/h2&gt;

&lt;p&gt;In this case we just all the comment to the top of all the files, using sed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;find &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s2"&gt;"*.js"&lt;/span&gt; &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt; &lt;span class="s1"&gt;'1i\
/* eslint-disable */
'&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; + 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once this is implemented, make sure the comment is removed by the developer when they submit a pull request. This way the app will compile without issues and minimal git changes.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  👉🏽 Disable ESLint using the env variable
&lt;/h2&gt;

&lt;p&gt;In this case we will disable ESLint for a certain period of time but still have linting enabled in a pre-commit hook.&lt;/p&gt;

&lt;h3&gt;
  
  
  To disable ESLint
&lt;/h3&gt;

&lt;h4&gt;
  
  
  If you are using CRA
&lt;/h4&gt;

&lt;p&gt;In the .env file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;DISABLE_ESLINT_PLUGIN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  For ejected CRA or others using webpack
&lt;/h4&gt;

&lt;p&gt;In the webpack config, inside the rules you can comment out the part where the ESLint rule is enforced or add the env check.&lt;/p&gt;

&lt;p&gt;The ESLint resolver in webpack will look like the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;disableESLintPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DISABLE_ESLINT_PLUGIN&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;disableESLintPlugin&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ESLintPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="c1"&gt;// Plugin options&lt;/span&gt;
          &lt;span class="c1"&gt;// ....&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  👉🏽 Fix the most and disable the rest
&lt;/h2&gt;

&lt;p&gt;I won't recommend this method on a large code-base with many devs currently developing features on the same. It would suit in case you are doing a complete refactor of the code base, on an entirely new git repository.&lt;/p&gt;

&lt;p&gt;First if you are using a style formatter(i.e. like prettify), run it first. In case you have prettier cli, you can run it directly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;prettier &lt;span class="nt"&gt;--write&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or you can configure in package.json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"format"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"prettier --write &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;**/*.{js,jsx,json,md}&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then proceed with the ESLint fix command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"lint:fix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"eslint --fix src/**/*.{js,jsx}"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you might still end with lot of errors&lt;/p&gt;

&lt;h3&gt;
  
  
  Using auto suppressor
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/amanda-mitchell/suppress-eslint-errors"&gt;https://github.com/amanda-mitchell/suppress-eslint-errors&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This tool will disable every error line by line, i.e. it will add the&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;// eslint-disable-line&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 for every single error. This might be a lot of change, but you can choose the rules to disable. The suppress-eslint-errors project is a small and easy to understand code, you can just clone and modify for your purpose.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h2&gt;
  
  
  👉🏽 Convert all errors to warnings
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/bfanger/eslint-plugin-only-warn"&gt;https://github.com/bfanger/eslint-plugin-only-warn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install eslint-plugin-only-warn&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;eslint-plugin-only-warn &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add only-warn to the plugins section of your .eslintrc configuration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;plugins&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;only-warn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ESLint plugin will convert all the errors to warnings. You can slowly remove all the warnings. But make sure to add conditions in pre-commit hook to remove the warnings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lint&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eslint src/**/*.{js,jsx} --max-warnings=0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You can pick any one of the strategies or a combination of them. The best strategy would be to add ESLint config at the start of the project itself.&lt;/p&gt;

&lt;p&gt;Which ever strategy you pick, have a pre-commit solution like &lt;a href="https://www.npmjs.com/package/husky"&gt;Husky&lt;/a&gt; and have the lint check in CI pipeline too.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
    </item>
  </channel>
</rss>
