<?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: Adrian Smijulj</title>
    <description>The latest articles on Forem by Adrian Smijulj (@doitadrian).</description>
    <link>https://forem.com/doitadrian</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%2F122657%2F1b03c492-93eb-4663-be29-797537ca869c.jpg</url>
      <title>Forem: Adrian Smijulj</title>
      <link>https://forem.com/doitadrian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/doitadrian"/>
    <language>en</language>
    <item>
      <title>Create custom ESLint rules in 2 minutes</title>
      <dc:creator>Adrian Smijulj</dc:creator>
      <pubDate>Mon, 23 Nov 2020 09:53:58 +0000</pubDate>
      <link>https://forem.com/webiny/create-custom-eslint-rules-in-2-minutes-122m</link>
      <guid>https://forem.com/webiny/create-custom-eslint-rules-in-2-minutes-122m</guid>
      <description>&lt;p&gt;ESLint is a great tool when it comes to code standardization. Maintained by the open-source community, and with a rich plugin-based ecosystem, you basically already have everything you need to produce a solid codebase.&lt;/p&gt;

&lt;p&gt;But in some cases, you might want to enforce one or more ESLint code rules that are specific to your particular project. Okay, first you take a look at NPM and check if there is an existing plugin. But after searching a bit, you didn’t find anything that would suit your specific needs and for that reason, you decided to create your own custom ESLint plugin.&lt;/p&gt;

&lt;p&gt;If that’s your case, then follow along because here we will show how to accomplish this in 5 simple steps.&lt;/p&gt;

&lt;p&gt;Before we continue, just wanted to mention this is not a tutorial on how to write ESLint rules/plugins. It’s just a quick guide on how to get per-project rules up and running quickly.&lt;/p&gt;

&lt;p&gt;So here we go!&lt;/p&gt;

&lt;h2&gt;
  
  
  5 steps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Inside your project folder, create a folder. I will name mine &lt;code&gt;eslint&lt;/code&gt;, but the exact location/name is not important at this point.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  2. Inside the folder, we create a &lt;code&gt;package.json&lt;/code&gt; file with the following content:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// eslint/package.json&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&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-plugin-my-project&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;version&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;1.0.0&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;main&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;index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Please note that that the package name must start with &lt;code&gt;eslint-plugin-&lt;/code&gt; prefix, as it is an ESLint requirement.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  3. Then we also create an &lt;code&gt;index.js&lt;/code&gt; file, which will contain all of the plugin rules. If you don’t know how to write ESLint rules, take a look at &lt;a href="https://astexplorer.net/"&gt;AST Explorer&lt;/a&gt; example, it’s not too complicated (at least for simple cases).
&lt;/h3&gt;

&lt;p&gt;The following example was copied from the AST explorer website, and it just prevents developers from using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals"&gt;template literals&lt;/a&gt;. Not very useful maybe, but for demonstration purposes of this guide, it will suffice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// eslint/index.js&lt;/span&gt;
&lt;span class="kr"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;rules&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-template-literals&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="na"&gt;create&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nx"&gt;TemplateLiteral&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Do not use template literals&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="p"&gt;};&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&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;h3&gt;
  
  
   4. Once you’re done, in your project root, add the plugin as a dependency using yarn(or npm):
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;dev&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;eslint&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the &lt;code&gt;file:./eslint&lt;/code&gt; part. &lt;a href="https://classic.yarnpkg.com/en/docs/cli/add/"&gt;This syntax&lt;/a&gt; will allow us to install a package that is on our local file system.&lt;/p&gt;

&lt;p&gt;After executing, inside the &lt;code&gt;node_modules&lt;/code&gt; folder, you should have the &lt;code&gt;eslint-plugin-my-project&lt;/code&gt; folder that contains both &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;package.json&lt;/code&gt; files.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Finally, add the plugin and rule in your &lt;code&gt;eslintrc&lt;/code&gt; file. You can do that like so:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// eslintrc.js
module.exports = {

    (...)
    plugins: ["my-project"],
    rules: {
        "flowtype/define-flow-type": 1,
        "my-project/no-template-literals": 1
    }
    (...)
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  We’re done!
&lt;/h2&gt;

&lt;p&gt;That wasn’t too bad, right? 😊&lt;/p&gt;

&lt;p&gt;Try adding a simple template literal in your code. If you’ve done everything correctly, you should get an ESLint warning. Now continue with the files we’ve just created and start defining your own custom rules!&lt;/p&gt;




&lt;p&gt;Thanks for reading! My name is Adrian and I work as a full-stack developer at Webiny. If you have any questions or comments, feel free to reach out to me via &lt;a href="https://twitter.com/doitadrian"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>eslint</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
