<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ eslint - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ eslint - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 06 May 2026 17:00:20 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/eslint/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Tutorial – How to Set Up a Front End Development Project ]]>
                </title>
                <description>
                    <![CDATA[ Let’s say you plan to build a website. Before you start, you want to set up a few tools to make your life easier. But which tools should you have? The JavaScript ecosystem is changing so fast that it can be overwhelming to pick the best tools to use.... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-set-up-a-front-end-development-project/</link>
                <guid isPermaLink="false">66c4c80d6e4b60b5b844529e</guid>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Front-end Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Prettier ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Hunor Márton Borbély ]]>
                </dc:creator>
                <pubDate>Wed, 12 Feb 2025 21:44:03 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1739318785959/23632d35-1d5a-4797-8c7d-fbad6c80a879.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Let’s say you plan to build a website. Before you start, you want to set up a few tools to make your life easier. But which tools should you have?</p>
<p>The JavaScript ecosystem is changing so fast that it can be overwhelming to pick the best tools to use. To solve this problem, in this article, I’m going to walk you through how to set up a front-end project from scratch.</p>
<p>We'll cover things like must-have editor extensions, adding JavaScript libraries to your project, why you'll use Node.js even if you want to do front-end development, and setting up an application bundler that will generate a live preview as you code in your browser.</p>
<p>You can also <a target="_blank" href="https://www.youtube.com/watch?v=BiBjuphZQxA">watch this article as a video</a> on YouTube. Let's dive in.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-how-to-choose-a-code-editor">How to Choose a Code Editor</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-auto-format-your-code-in-vs-code">How to Auto-format Your Code in VS Code</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-do-you-need-node-for-a-front-end-project">Why Do You Need Node for a Front-End Project?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-run-your-project">How to Run Your Project</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-add-libraries-to-your-javascript-project">How to Add Libraries to Your JavaScript Project</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-get-coding-tips-while-you-code">How to Get Coding Tips While You Code</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-initialize-a-project-with-vite">Initialize a Project with Vite</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-summary">Summary</a></p>
</li>
</ol>
<h2 id="heading-how-to-choose-a-code-editor"><strong>How to Choose a Code Editor</strong></h2>
<p>Let’s start with the foundations. As a web developer, you mostly edit text, so you need a good editor. So which one should you use?</p>
<p>Picking an editor is highly based on personal preference, as most editors have very similar features.</p>
<p>If you don’t have a personal preference, I highly recommend <a target="_blank" href="https://vscode.dev/">VS Code</a>. Lately, it has become the de facto standard editor for web development.</p>
<p><a target="_blank" href="https://survey.stackoverflow.co/2024/technology#1-integrated-development-environment"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738945424232/9f41d802-e672-4cd7-ada3-5e8d54000446.png" alt="VS Code is by far the most used editor" class="image--center mx-auto" width="3840" height="2160" loading="lazy"></a></p>
<p>One of the greatest features of all the mainstream editors is that you can add extensions to them. Let’s walk through two extensions that are must-haves.</p>
<h2 id="heading-how-to-auto-format-your-code-in-vs-code"><strong>How to Auto-format Your Code in VS Code</strong></h2>
<p>Prettier is an extension that makes your code more readable and more consistent.</p>
<p>Let’s say you copy-pasted some code, and it’s hard to read. The tabulation is off, a line is too long, and so on. Then you just save the file, and magically, everything looks as it should be.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738948709710/c64a427d-b868-4704-87db-338ebf079c67.png" alt="Prettier formats the code based on best practices" class="image--center mx-auto" width="2280" height="1280" loading="lazy"></p>
<p>This is what Prettier does. It formats the code based on best practices. It doesn't just fix tabulation and wrap the lines. It also adds parentheses to improve code readability, makes sure you are consistent with quotation marks, and many more.</p>
<p>To make it work in VS Code, we must first install the Prettier extension. To do so, go to the extensions panel in VS Code, search for Prettier, and then install it.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738955063143/7e8378e5-cf70-4527-9951-b822a745f4bf.png" alt="To install Prettier, go to the Extensions panel, search for Prettier, and install it" class="image--center mx-auto" width="2260" height="1280" loading="lazy"></p>
<p>Installing this extension doesn't format your files automatically on save by default. The default behavior is that once you install this extension, you can right-click within a file and select <strong>Format Document</strong>. You can also select part of a file and choose <strong>Format Selection</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738955102891/2e55eaa7-ec2a-4aca-ad79-b1df82d31e5c.png" alt="Right-click within a file and select Format Document to format it" class="image--center mx-auto" width="2260" height="1280" loading="lazy"></p>
<p>The first time you do this, you need to select the default formatter. VS Code already has a formatter, but it isn’t as powerful as Prettier. Now that you have two formatters, you have to let VS Code know that you want to use Prettier for formatting in the future.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738955119781/d0ef8bc3-54d0-4428-9731-c8857171f3e7.png" alt="The first time, you need to select the default formatter" class="image--center mx-auto" width="2260" height="1280" loading="lazy"></p>
<p>If you wish to auto-format your files when you save them, you need to change the settings. Go to Settings in your VS Code preferences and search for the <strong>Format on Save</strong> option. By default, this is false, so make sure that you tick this checkbox. With this, Prettier formats your files every time you save them. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738955201893/fd3fbec6-5265-4c9b-adab-c1a9d524a635.png" alt="Set the Format On Save option in Settings" class="image--center mx-auto" width="2260" height="1280" loading="lazy"></p>
<p>Formatting can be controversial, though. I highly recommend the default settings, especially for beginners. But if you prefer a different style, you can customize things.</p>
<p>You can indicate with comments to <a target="_blank" href="https://prettier.io/docs/en/ignore.html">ignore specific lines</a> and create a config file to list your preferences.</p>
<p>In the root folder of your project, you can create a file called <strong>.prettierrc</strong> and add a few options. A typical option could be if you prefer single quotes instead of double quotes in your files. Or if you don't want to have semi-colons at the end of your lines.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738955259640/d430386f-de2a-49f3-b252-aee4a9bc1089.png" alt="Adding a custom Prettier configuration" class="image--center mx-auto" width="2260" height="1280" loading="lazy"></p>
<p>With this configuration, you will have a different format once you save your files.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"singleQuote"</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">"semi"</span>: <span class="hljs-literal">false</span>
}
</code></pre>
<p>There are many more options, of course. If you want to dig deeper, check out <a target="_blank" href="https://prettier.io/docs/en/configuration.html">Prettier's documentation</a><a target="_blank" href="https://prettier.io/docs/en/configuration.html">.</a></p>
<h2 id="heading-why-do-you-need-node-for-a-front-end-project"><strong>Why Do You Need Node for a Front-End Project?</strong></h2>
<p>Before we get to the second must-have extension, we need to set up a few other things. First, we need to talk about Node.js. What is Node, and why do you need it even if you work as a front-end developer?</p>
<p>Node is often associated with backend development, but that's not its only job. Node is a JavaScript runtime – this means it runs JavaScript files outside of the browser.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738949060189/11f39633-76a1-4c38-92ef-88846ffdeb8f.png" alt="You can run JavaScript as part of a website in your browser" class="image--center mx-auto" width="2200" height="1220" loading="lazy"></p>
<p>There are two ways of running JavaScript code. You can either have it as part of a website and run the entire website in a browser, or run only the Javascript file with a runtime like Node.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738949071655/68552f47-63ba-4cfc-9ba0-abb9218a4857.png" alt="You can run JavaScript on its own with Node" class="image--center mx-auto" width="2200" height="1220" loading="lazy"></p>
<p>In the example below, we have a very simple Javascript file that prints "Hello World" to the console. If we have Node installed, we can go to the terminal, navigate to the folder where this file is, and then run it with Node like this. You can see that the file was executed, and the result is in the console.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738947073105/9b56af05-e925-4cd1-9475-b3e49898dd39.png" alt="Node can run JavaScript files on their own" class="image--center mx-auto" width="1280" height="720" loading="lazy"></p>
<p>That's what Node really is: a tool that runs JavaScript files on their own.</p>
<p>JavaScript mostly behaves the same way in both environments. But there are also differences in what JavaScript can do in a browser vs when it runs with Node.</p>
<p>For instance, when running in the browser, JavaScript can access and modify HTML elements. That's the main point of having JavaScript in the first place.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738949133299/b1c6d0a6-ba11-4659-a51e-b72e4bbfde68.png" alt="In the browser, JavaScript can access and modify your HTML elements" class="image--center mx-auto" width="2200" height="1220" loading="lazy"></p>
<p>In Node, there's no HTML file. JavaScript runs on its own. On the other hand, in Node, JavaScript has access to your file system and can read and write your files.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738949113586/e8702a9c-91bf-4096-8768-cf06e4e41082.png" alt="With Node, JavaScript can access and modify your File system" class="image--center mx-auto" width="2200" height="1220" loading="lazy"></p>
<p>For instance, you can run scripts on your machine to initialize a project. We are going to do that. You can run checks on your files and automatically correct the mistakes. Or you can run your test files.</p>
<p>In short, Node lets you run some tools that make your life much easier as a developer.</p>
<p>To install Node, go to <a target="_blank" href="http://nodejs.org">nodejs.org</a> and install it. If you are unsure if you already have Node, you can also go to your terminal and run <strong>node -v</strong> to check. If you get a version number, you have Node.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738949153110/c9e63db3-d52b-4122-a31c-6b871ed2e4b5.png" alt="The website of Node.js" class="image--center mx-auto" width="2200" height="1220" loading="lazy"></p>
<p>So, why do people associate Node primarily with backend development? If the backend code is in JavaScript, the servers must run it somehow without a browser. So yes, if you are a backend developer using JavaScript, then you're most probably going to use Node. But Node is much more than that.</p>
<h2 id="heading-how-to-run-your-project"><strong>How to Run Your Project</strong></h2>
<p>Now that we have Node, we can use a live server to see our site live in the browser as we develop it. Without this, you need to manually refresh the browser window every time you make a change.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738949223269/97bc1d15-4ab6-4204-88d6-257fa471f95d.png" alt="A bundler creates a package that you can run in the browser" class="image--center mx-auto" width="2248" height="1220" loading="lazy"></p>
<p>These tools are called bundlers because they take all your files and turn them into a neat package you can run in the browser. So why do you need them?</p>
<ul>
<li><p>They update your site live in the browser with hot reloading. When you save a file, you immediately see the result in your browser.</p>
</li>
<li><p>As web development tools have evolved, the browser won't understand your files when you use anything more advanced. For instance, are you using React? Then, you're using the JSX syntax – the one that looks like HTML. The JSX syntax is not part of JavaScript. You need a tool to convert it into plain JavaScript. Otherwise, it won't run in your browser. Or are you using TypeScript? You also need to turn that into JavaScript. Or, if you're using SCSS or any other CSS dialect, you need to convert it to plain CSS.</p>
</li>
<li><p>If you import libraries using the JavaScript module system, you need a live server to avoid CORS issues in your browser.</p>
</li>
</ul>
<p>This is what bundlers do. They make sure that you can use modern-day tooling while you're developing your application, and they can also create a final production build that you can publish on the internet.</p>
<p>How do you pick a bundler? There are several options, and essentially, they all do the same thing. The difference between them is in their performance, configuration options, and ease of use.</p>
<p>The most used bundler is still <a target="_blank" href="https://webpack.js.org/">webpack</a>, one of the earliest bundlers in the field. But the one that seems to have taken over the throne and gained more and more popularity is <a target="_blank" href="https://vite.dev/">Vite</a>. Here's a chart from the latest edition of the State of JavaScript survey.</p>
<p><a target="_blank" href="https://2024.stateofjs.com/en-US/libraries/#all_tools_experience"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738947476843/195adbd5-5aba-427e-9b00-c507542235d2.png" alt="The sentiment towards WebPack and Vite" class="image--center mx-auto" width="1540" height="900" loading="lazy"></a></p>
<p>This chart shows that while most developers have used Webpack, they don’t necessarily love it. At the same time, Vite's popularity is rising while still maintaining a positive sentiment.</p>
<p>If you haven't checked out the <a target="_blank" href="https://stateofjs.com/en-US">State of JavaScript</a> survey before, I highly recommend going through it. It gives you an excellent overview of the latest trends with JavaScript. You can learn which tools and libraries people love to use and which they will abandon soon. If you feel overwhelmed by all the changes in the JavaScript ecosystem, the results of this survey can be a great guide.</p>
<p>Once we have a folder for our project, let's navigate to it using our terminal. The easiest way to do this is to open the folder in VS Code and then use the built-in terminal. VS Code will open the terminal with the correct folder.</p>
<p>Then, you can run the project in the terminal with the following command. npx is a command line tool that comes with Node. This is one of the reasons we installed Node: to be able to run commands like this.</p>
<pre><code class="lang-bash">npx vite
</code></pre>
<p>The first time you run this script, it will ask you to install Vite. Say yes. Then, it will show you the URL of a local server, which you can open in a browser to view your project.</p>
<pre><code class="lang-bash">  VITE v6.1.0  ready <span class="hljs-keyword">in</span> 162 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h + enter to show <span class="hljs-built_in">help</span>
</code></pre>
<p>Now, if you update a file and save the changes, the new version appears in the browser immediately. It generates a live preview of your site until you stop the script or close the terminal. You can keep it running while you're developing your site.</p>
<p>Once you have finished, you can press <strong>Ctrl+C</strong> to stop the script. If it gets desynchronized or you break it with an error, restart it by pressing <strong>Ctrl+C</strong> to stop it and rerunning the same script. So that's how you run a project with Vite.</p>
<h2 id="heading-how-to-add-libraries-to-your-javascript-project"><strong>How to Add Libraries to Your JavaScript Project</strong></h2>
<p>Now that we have Node, we can also use npm or Note Package Manager to add libraries to our project. npm is another tool included with Node. So how does it work?</p>
<p>First, I will walk you through setting things up step by step the manual way so it's clear how the different parts come together. Then, I will show you how to automate most of these steps.</p>
<p>Navigate to your current folder in the terminal and run the following command to initialize the project. This command initializes a package.json file with some metadata.</p>
<pre><code class="lang-bash">npm init --yes
</code></pre>
<p>At this point, this file is not very interesting. It contains the project name, description, version number, and so on. You can change these values.</p>
<p>Now, we can add libraries to our package with the npm install command. In a <a target="_blank" href="https://www.freecodecamp.org/news/render-3d-objects-in-browser-drawing-a-box-with-threejs/">previous article</a>, we used Three.js to render 3D boxes in the browser.</p>
<p>So, as an example, let's install <a target="_blank" href="https://threejs.org/">Three.js</a>. Go to your terminal again, make sure you are in the correct folder, and run the following command:</p>
<pre><code class="lang-bash">npm install three
</code></pre>
<p>This command will install Three.js. But how do you know that the keyword is three here, not Three.js?</p>
<p>When you don’t know the package name, you can just google npm and the name of the library you need. Or, if you don't even know the library name, you can also just search for an npm 3D library and see what Google comes up with.</p>
<p>We can go through each package one by one and pick one based on their capabilities and other info. These packages mostly come with descriptions and quick examples to give you an idea of what the library can do for you.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738949563130/ce677d97-20c4-48ed-a168-b99eb01c84d4.png" alt="How to pick a library to use" class="image--center mx-auto" width="2260" height="1260" loading="lazy"></p>
<p>Another indicator you might want to look for is the weekly downloads and the date of the last update to ensure you select an actively maintained library that people still use.</p>
<p>Once you find the package you are looking for, you can see the command to install it at the top right corner: <code>npm i three</code>. The <code>i</code> here is just shorthand for install. Another way to learn how to install Three.js is to go to its official documentation and check the <a target="_blank" href="https://threejs.org/docs/index.html#manual/en/introduction/Installation">installation guide</a>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738952468171/7a54e0bb-d88d-428b-911c-cf7a71676562.png" alt="The package.json file after initializing the project and installing Three.js" class="image--center mx-auto" width="2260" height="1260" loading="lazy"></p>
<p>When we install a package, three things happen:</p>
<ul>
<li><p>It adds the latest version of Three.js in our package.json file as a project dependency.</p>
</li>
<li><p>It also creates a package-lock file, which NPM uses to keep track of the dependencies. You should never edit the dependency section of your package.json file or the package-lock file manually. Instead, you should always use commands like npm install and uninstall to add, remove, or update packages.</p>
</li>
<li><p>Finally, the node_modules folder gets created. This folder contains the source code of Three.js. When we import Three.js in our project, it looks for it in this folder. The content of this folder is also something that you should never change. You can look into it if you're interested in the source code of the library that you're using, but you shouldn't change it.</p>
</li>
</ul>
<p>If something goes wrong and you have an error with your dependencies that you can't figure out, then you can always safely delete the node_modules folder and the package-lock file and reinstall your dependencies based on the package.json file. This is not something that you should do, but you can always go back to having a clean slate.</p>
<p>Now that we have installed Three.js, we can create a simple website that displays a 3D box. It's a simple HTML file and a JavaScript file with the code for the 3D box. The key here is that we import Three.js with the import statement in the JavaScript file. This import will use the package that we just installed.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738952825715/9828ea05-e75a-4fdf-b1a3-39bd3aa8e380.png" alt="Using Three.js in a sample project" class="image--center mx-auto" width="2546" height="1444" loading="lazy"></p>
<p>Then, we can run the project with Vite. Using imports means that we use the module system now. Running a project with the module syntax can be a bit tricky, as the browser gives you CORS errors by default. However, as we are using Vite to run our project, it works seamlessly without any questions. That’s one of the reasons we use Vite.</p>
<p>If you want to learn more about building 3D games with Three.js, check out my <a target="_blank" href="https://www.freecodecamp.org/news/three-js-tutorial/">earlier article</a> on building a minimalistic car in the browser.</p>
<h2 id="heading-how-to-get-coding-tips-while-you-code"><strong>How to Get Coding Tips While You Code</strong></h2>
<p>The second must-have editor extension is ESLint. While Prettier formatted the code, ESLint gives you coding tips.</p>
<p>It helps you catch basic mistakes and avoid patterns that can cause bugs or be misleading when you try to understand the code.</p>
<p>Here’s a simple example where you declare a variable, but then you have a typo, and you try to use another variable that doesn't exist. ESLint will highlight this for you. It will give you an error both at the variable declaration, saying that you created a variable you don't use, and add the usage, saying that you're trying to use a variable that is not declared. With ESLint, it's easy to spot that you made a typo.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738953388793/c081f406-f964-4c4c-95ef-83cdc8e403df.png" alt="c081f406-f964-4c4c-95ef-83cdc8e403df" class="image--center mx-auto" width="1928" height="1244" loading="lazy"></p>
<p>ESLint, of course, is much more complex than just being able to catch simple errors. There are also less obvious use cases where you might not understand why ESLint is complaining. Then, you can always click the link in the error popup for more details explaining why this pattern is harmful and what you can do to avoid it.</p>
<p>So how can we use ESLint in our projects? This time, we need to have an extension and a configuration. First, as we did with Prettier, we must install the ESLint extension. Go to your extensions, search for ESLint, and install it.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738953594259/a6f02e96-069d-4463-8322-86a23c523df3.png" alt="To install ESLint go to the Extensions panel, search for ESLint, and install it" class="image--center mx-auto" width="2260" height="1280" loading="lazy"></p>
<p>We also need to set up ESLint for our project. Before we do that, we need to make sure that the project already has a package.json file. If you don't already have a package.json file, we first have to run <code>npm init --yes</code> to initialize the project. Then, we can generate an ESLint config with the following command:</p>
<pre><code class="lang-bash">npm init @eslint/config@latest
</code></pre>
<p>This script will ask you a few questions. Based on your answers, it will customize the configuration and the rules to check. For most cases, you can use the default option.</p>
<ul>
<li><p>The first time, it will ask for permission to install ESLint. Say yes.</p>
</li>
<li><p>Then, it will ask you whether to use ESLint only for syntax checks or to find problems as well. Choose the second option to get the most help from ESLint.</p>
</li>
<li><p>Then select that you’ll use it with JavaScript modules. Modern web development projects use the JavaScript module system. We use JavaScript modules if we have imports and exports in our code.</p>
</li>
<li><p>Then, it will ask what framework we are using. If we select a framework, it will add framework-specific rules to our project. For instance, using it with React will force us to define the prop types. If we don't use a front-end framework, just vanilla JavaScript, then select "None of these".</p>
</li>
<li><p>Then, it will ask if the project is using TypeScript. Choose based on your preference.</p>
</li>
<li><p>It asks where you run the code. As we have a front-end project, select "Browser".</p>
</li>
<li><p>Then, it will ask if you want to install the additional dependencies that are required for the rules based on your selections. Select yes.</p>
</li>
<li><p>It will also ask what package manager we’re using. We haven't talked a lot about this, but there are multiple package managers that we can use. Select the default: npm.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738953735596/a69f62ac-b5b4-459e-8e78-6956442adfe4.png" alt="Installing ESLint" class="image--center mx-auto" width="2640" height="1624" loading="lazy"></p>
<p>These were a lot of questions. Let's see what happened after we ran this command.</p>
<p>After this step, we have ESLint and some other dependencies based on the answers in the package.json file as development dependencies. Development dependency means that ESLint won't be part of your website's final code, but we need it during development.</p>
<p>ESLint also became part of our node_modules folder, and there are many more packages here now. This is because a dependency can have other dependencies, and the dependencies of the dependencies will also be part of the node_modules folder.</p>
<p>ESLint also created a config file that sets up the rules based on your answers. We will see how to customize the rules.</p>
<p>Now that ESLint is working, you should also see errors in the code once something is off. If you go to your JavaScript file and try to use an undeclared variable, ESLint will highlight the issue.</p>
<p>ESLint is also highly customizable. For instance, we might not want to mark an unused variable as an error. First, we go to the error popup and select the identifier of this type of error. Then, we go to the ESLint config and override this error as follows. Here, we can reduce the severity to a warning or completely turn off this rule.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738953834462/387e36f3-81ee-461c-ae02-3ca614c7b765.png" alt="Turning off ESLint rules in the config" class="image--center mx-auto" width="2640" height="1624" loading="lazy"></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> globals <span class="hljs-keyword">from</span> <span class="hljs-string">"globals"</span>;
<span class="hljs-keyword">import</span> pluginJs <span class="hljs-keyword">from</span> <span class="hljs-string">"@eslint/js"</span>;


<span class="hljs-comment">/** @type {import('eslint').Linter.Config[]} */</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> [
  {<span class="hljs-attr">languageOptions</span>: { <span class="hljs-attr">globals</span>: globals.browser }},
  pluginJs.configs.recommended,
  {
    <span class="hljs-attr">rules</span>: {
      <span class="hljs-string">"no-unused-vars"</span>: <span class="hljs-string">"off"</span>, <span class="hljs-comment">// Turn off the No Unused Variables rule</span>
    }
  }
];
</code></pre>
<p>But if you're a beginner, I recommend following the rules that ESLint has by default. Sometimes, it might be annoying to fix all the seemingly harmless issues, but all these rules are based on industry best practices, so it's good to follow them. For more details, check out <a target="_blank" href="https://eslint.org/docs/latest/use/configure/rules">ESLint's documentation</a>.</p>
<h2 id="heading-initialize-a-project-with-vite">Initialize a Project with Vite</h2>
<p>We walked through the step-by-step process of setting up a project. We used <strong>npm init</strong> to initialize the project, manually set up ESLint, and ran our project with Vite. Vite can also initialize the project with a sample application and all the necessary files, which is especially handy when we set up a React project. </p>
<p>Let's see how to set up a vanilla JavaScript and a React project. Let's navigate to a folder in the terminal that will contain our project. We don't need to create a project folder this time because the script will make it for us. Then, run the following command to initialize a project:</p>
<pre><code class="lang-bash">npm create vite@latest
</code></pre>
<p>This command asks you a few questions.</p>
<ul>
<li><p>First, it will ask you for the project name, which will also be the name of the folder created as the project root.</p>
</li>
<li><p>Then, it will ask what framework you use. If you don't use any framework and want plain old JavaScript, choose "Vanilla." If you use React, choose React.</p>
</li>
<li><p>Then, it will ask you if you want to use TypeScript or JavaScript. Here, the default is TypeScript. If you're a beginner in web development, choose JavaScript. If you are more confident with your skills, then go with TypeScript. TypeScript is more complicated, but it has become the industry standard in web development, and most jobs require you to know it. As a beginner, you can go with JavaScript.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738953943915/884e22b0-f096-4338-a392-588b032834f4.png" alt="Creating a project with Vite" class="image--center mx-auto" width="1364" height="966" loading="lazy"></p>
<p>Now, we can navigate to the new folder created and check out what we have here. If you choose a vanilla JavaScript project, you can see it generated a simple application with HTML, CSS, and some JavaScript files. You can change or even delete these. You'll need an HTML file as an entry point, but you can replace the rest.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738954107608/0234f409-6f78-4039-b5d1-133c8cd09530.png" alt="Vite can create a sample application with an HTML, CSS, and JavaScript file" class="image--center mx-auto" width="2624" height="1822" loading="lazy"></p>
<p>We can run this project with <strong>npx vite</strong> as we did before, but there’s a better way. For a real project, we want to add Vite as a development dependency to ensure consistency with the version we are using.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"my-project"</span>,
  <span class="hljs-attr">"private"</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"0.0.0"</span>,
  <span class="hljs-attr">"type"</span>: <span class="hljs-string">"module"</span>,
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"dev"</span>: <span class="hljs-string">"vite"</span>,
    <span class="hljs-attr">"build"</span>: <span class="hljs-string">"vite build"</span>,
    <span class="hljs-attr">"preview"</span>: <span class="hljs-string">"vite preview"</span>
  },
  <span class="hljs-attr">"devDependencies"</span>: {
    <span class="hljs-attr">"vite"</span>: <span class="hljs-string">"^6.1.0"</span>
  }
}
</code></pre>
<p>The package.json file shows that Vite has been added as a development dependency. To use this hardcoded version, we first have to install it via <code>npm install</code>. This command installs all the dependencies listed in the package.json file.</p>
<pre><code class="lang-bash">npm install
</code></pre>
<p>This package.json file now also has a scripts section. This section can define scripts to run your app locally, create a production build, or test your application. You can run them with <strong>npm run</strong>. So, for instance, to run the application, you can open the terminal with the correct folder and run the following command.</p>
<pre><code class="lang-bash">npm run dev
</code></pre>
<p>This runs the script labeled as “dev” in the scripts section, which will run the Vite version we just installed with <strong>npm install</strong>.</p>
<p>Vite does not install ESLint when you create a vanilla project, but you can always install it manually, as we did before with <strong>npm init @eslint/config@latest</strong>.</p>
<p>If you choose React as a framework when we initialize the project, we will have a couple more files. For instance, we have an ESLint config with the recommended React settings. We also have a Vite config that enables us to use React and a sample application that we can run.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738954174340/26c5a9a5-b983-4ed7-8e43-fbab4400e08b.png" alt="Vite can also create a React project for you" class="image--center mx-auto" width="2624" height="1822" loading="lazy"></p>
<p>To run this app, we need to install the dependencies. So, let's go to the terminal and run <code>npm install</code>. This will install all the dependencies, including React. Then, we can run this app with <code>npm run dev</code>, and we will have a working React application.</p>
<h2 id="heading-summary">Summary</h2>
<p>In this article, we set up and run a front-end project with Vite. We also covered how to find and add dependencies, how to have consistent and automatic formatting with Prettier, and how to avoid bugs with ESLint.</p>
<p>What happens once you finish developing your app? How do you upload it to the web and share it with the world? That's the topic of a future article.</p>
<h3 id="heading-subscribe-for-more-tutorials-on-web-development"><strong>Subscribe for more tutorials on Web Development:</strong></h3>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/BiBjuphZQxA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Set Up ESLint, Prettier, StyleLint, and lint-staged in Next.js ]]>
                </title>
                <description>
                    <![CDATA[ A linter is a tool that scans code for potential issues. This is invaluable with a programming language like JavaScript which is so loosely typed. Even for TypeScript, which is a strongly typed language whose compiler does a great job of detecting er... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-set-up-eslint-prettier-stylelint-and-lint-staged-in-nextjs/</link>
                <guid isPermaLink="false">66e88a49bd6e8dcd7dc5a8a2</guid>
                
                    <category>
                        <![CDATA[ Next.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Prettier ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Stylelint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Lint-Staged ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Naveed Ausaf ]]>
                </dc:creator>
                <pubDate>Mon, 16 Sep 2024 19:43:05 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1726515328917/f3ecbc80-6d06-45ee-b307-89ed1a7bc854.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>A <strong>linter</strong> is a tool that scans code for potential issues. This is invaluable with a programming language like JavaScript which is so loosely typed.</p>
<p>Even for TypeScript, which is a strongly typed language whose compiler does a great job of detecting errors at compile time, linters such as ESLint have plugins that catch problems which are not caught by the compiler.</p>
<p>When you generate a new app using the Next.js CLI (<code>npx create-next-app</code>), ESLint is configured by default. But there are several problems with the linting setup generated by <code>create-next-app</code>:</p>
<ul>
<li><p>If you choose SCSS for styling, you should use <a target="_blank" href="https://stylelint.io/">Stylelint</a> in the build process to lint CSS or SCSS stylesheets. But it’s not set up automatically.</p>
</li>
<li><p>If instead you opt for Tailwind for styling, you should set up the Tailwind plugin for ESLint. But again, this isn’t done in the generated ESLint configuration.</p>
</li>
<li><p>If you choose TypeScript, then in Next.js v14 and below, TypeScript-specific ESLint rules are not configured, contrary to <a target="_blank" href="https://nextjs.org/docs/pages/building-your-application/configuring/eslint#typescript">what the documentation states</a>. While a Next.js v15 app has these set up, I would still tweak the setup further with the more powerful linting rules provided by the <a target="_blank" href="https://typescript-eslint.io/">typescript-eslint project</a>. These include the <a target="_blank" href="https://typescript-eslint.io/rules/no-floating-promises">no-floating-promises rule</a> which points out if you have forgotten to <code>await</code> a method that returns a <code>Promise</code>. These and other rules from typescript-eslint are going to save you lots and lots of times as you write TypeScript code.</p>
</li>
<li><p>And finally, Prettier is not set up. Prettier is a code formatting tool. It can prevent inconsistently formatted code from getting into the code repository, which would make comparisons between different versions of the same file difficult. Also, nicely formatted code is easier to work with. So this is a pretty big omission.</p>
</li>
</ul>
<p>In this tutorial, I'll show you how I set up linting and formatting in my Next.js projects in a way that addresses the issues above. I’ll also teach you how to install and configure some related VS Code extensions for coding assistance.</p>
<p>To follow along, you can either use a Next.js project you already have, or generate a new app by running <code>npx create-next-app</code> on the terminal.</p>
<p>If you’re scaffolding a new app, your choices are up to you (defaults are fine) but make sure to choose YES in response to the question about whether you’d like to use ESLint:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725930860799/fb38b3b2-5592-4eb4-b8d0-153aeb2d749d.png" alt="Terminal window in which Next.js scaffolder, create-next-app, is showing code generation options to the user" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>If you are following along with an existing app rather than a new one, upgrade it by running the <a target="_blank" href="https://nextjs.org/docs/pages/building-your-application/upgrading/version-14">following command</a> in app root:</p>
<pre><code class="lang-bash">npm i next@latest react@latest react-dom@latest eslint-config-next@latest
npm i --save-dev eslint
</code></pre>
<p>This will avoid versioning conflicts down the line.</p>
<p>If you cannot upgrade to the latest version, you’ll need to specify versions for packages that will be installed in this tutorial to get around any version conflicts. Be warned that this can be frustrating.</p>
<p>Now you’re ready to open up the app in your code editor and proceed as follows.</p>
<p><strong>MONOREPO NOTE:</strong> A monorepo is a Git repository that contains multiple projects, each in its own subfolder. For example <a target="_blank" href="https://github.com/naveedausaf/flowmazondotnet">this repo</a> is for a full-stack solution that contains two projects: a Next.js app in folder <code>flowmazonfrontend</code> and a .NET Core API project in folder <code>flowmazonbackend</code>:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1743632328084/670c756d-a5f9-4333-80dc-446b3a5975bb.png" alt="Repo screenshot" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>If your Next.js app is contained in a monorepo that contains both a Next.js app and other projects, <strong>I</strong> recommend that you do all of the setup below within the Next.js app's folder. This is the folder you should open in your code editor and <code>cd</code> into on the terminal. There is one bit of setup (lint-staged and Husky) that will need to be done in the monorepo root folder. I shall point it out in due course.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>For this tutorial, I assume that you know how to:</p>
<ul>
<li><p>write a basic Next.js app with two or more pages.</p>
</li>
<li><p>install additional NPM packages into your app</p>
</li>
</ul>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-introduction">Introduction</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-table-of-contents">Table of Contents</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-set-up-prettier">Set Up Prettier</a></p>
<ul>
<li><a class="post-section-overview" href="#heading-a-note-on-line-endings-in-prettier">A note on line endings in Prettier</a></li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-set-up-eslint">Set Up ESLint</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-basics-of-eslint-configuration">Basics of ESLint configuration</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-eslint-setup-for-typescript">ESLint Setup for TypeScript</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-eslint-setup-for-tailwind">ESLint Setup for Tailwind</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-eslint-setup-for-prettier">ESLint Setup for Prettier</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-set-up-stylelint">Set Up Stylelint</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-set-up-packagejson-scripts">Set Up package.json Scripts</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-set-up-lint-staged">Set Up lint-staged</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-set-up-vs-code-extensions">Set UP VS Code Extensions</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-final-checks-and-troubleshooting">Final Checks and Troubleshooting</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-set-up-prettier">Set Up Prettier</h2>
<p><a target="_blank" href="https://prettier.io/">Prettier</a> is an opinionated code formatter that can format pretty much any file (<code>.html</code>, <code>.json</code>, <code>.js</code>, <code>.ts</code>, <code>.css</code>, <code>.scss</code> and so on).</p>
<p>Set it up in yuor app as follows:</p>
<ol>
<li><p>Install Prettier:</p>
<pre><code class="lang-bash"> npm install --save-dev prettier
</code></pre>
</li>
<li><p>If you chose Tailwind for styling when generating the app, then install <code>prettier-plugin-tailwindcss</code>:</p>
<pre><code class="lang-bash">   npm install --save-dev prettier-plugin-tailwindcss
</code></pre>
<p> This package is a Prettier plugin and provides rules for reordering of Tailwind classes used in a <code>class</code> or <code>className</code> attribute according to a canonical ordering. It helps keep the ordering of Tailwind classes used in the markup consistent.</p>
<p> %[https://youtu.be/tQkBJXwzY8A?autoplay=1] </p>
</li>
<li><p>Create <code>.prettierrc.json</code> in youyr project root. If you’re using SCSS for styling, paste the following snippet into this file:</p>
<pre><code class="lang-json"> {
   <span class="hljs-attr">"singleQuote"</span>: <span class="hljs-literal">true</span>,
   <span class="hljs-attr">"jsxSingleQuote"</span>: <span class="hljs-literal">true</span>
 }
</code></pre>
<p> If you’re using Tailwind instead, paste the following into <code>.prettierrc.json</code>:</p>
<pre><code class="lang-json"> {
   <span class="hljs-attr">"plugins"</span>: [<span class="hljs-string">"prettier-plugin-tailwindcss"</span>],
   <span class="hljs-attr">"singleQuote"</span>: <span class="hljs-literal">true</span>,
   <span class="hljs-attr">"jsxSingleQuote"</span>: <span class="hljs-literal">true</span>
 }
</code></pre>
</li>
<li><p>Create <code>.prettierignore</code> file in the app root, with the following content:</p>
<pre><code class="lang-plaintext"> node_modules
 .next
 .husky
 coverage
 .prettierignore
 .stylelintignore
 .eslintignore
 stories
 storybook-static
 *.log
 playwright-report
 .nyc_output
 test-results
 junit.xml
 docs
</code></pre>
<p> This file ensure that files which are not app code (that is, those which are not <code>.js</code>, <code>.ts</code>, <code>.css</code> files and so on.) do not get formatted. Otherwise Prettier will end up spending too much time processing files whose formatting you don't really care about.</p>
<p> <code>'prettierignore</code> (the file we just created), <code>.eslintignore</code>, and <code>.stylelintignore</code> have been ignored because these are plain text files with no structure so Prettier would complain that it cannot format them.</p>
</li>
<li><p>Finally, <strong>I recommend</strong> that you follow the steps in <a target="_blank" href="https://nausaf.hashnode.dev/lf-vs-crlf-configure-git-and-vs-code-on-windows-to-use-unix-line-endings">this post</a> to set LF as the EOL character, both in the repo and in your VS Code settings. Reasoning for this is given in the following subsection.</p>
</li>
</ol>
<h3 id="heading-a-note-on-line-endings-in-prettier">A note on line endings in Prettier</h3>
<p>Prettier <a target="_blank" href="https://prettier.io/docs/en/options#end-of-line">defaults to LF (Line Feed character) for line endings</a>. This means that when it formats files, it will change all occurrences of the CRLF character sequence, if any, to LF.</p>
<p>LF is also the default in text editors and other tools in Unix-based systems (Linux, MacOS etc.). But on Windows, the default for line endings is CRLF (Carriage Return character, followed immediately by Line Feed character).</p>
<p>Windows tooling such as text and code editors can easily handle LF as line ending. But CRLF can be problematic for tools on Unix-based systems such as Linux and various flavours of Unix. Therefore it makes sense to only use LF as line endings in code as this would work on both Windows and Unix-based systems.</p>
<p>Configuring LF as the EOF character in Git repo and in code editors will bring your tooling in line with Prettier's default. It will also ensure that all files in the Git repo consistently have LF line endings. Thus if a contributor to your repo is on Windows which uses CRLF as EOL character, the code they add or modify in the repo would still use LF: the code editor would default new code files to LF; <code>git</code> commit` would convert any CRLFs to LF when committing.</p>
<p>Finally, setting LF as the line endings for the whole repo would avoid strange things that happen when on Windows, Prettier retains its default of LF but Git and your code editor continue to use their default of CRLF for line endings:</p>
<ul>
<li><p>When VS Code Prettier extension formats a file (for example, when the extension is set up to "autoformat on save"), it does not change CRLF line endings. But formatting the same file by running Prettier on the command line <strong>does change line endings to LF</strong>. This discrepancy can be annoying.</p>
</li>
<li><p>Git may show warnings like this when when you run <code>git add .</code>:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725930986122/b5630966-e8dd-4f47-bb58-eed6eb023ea6.png" alt="Warnings shown by git add command when some of the files being added contain LF but the repo's line ending default is CRLF" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
</li>
</ul>
<h2 id="heading-set-up-eslint">Set Up ESLint</h2>
<h3 id="heading-basics-of-eslint-configuration">Basics of ESLint configuration</h3>
<p>ESLint comes with a number of linting rules out of the box. But you can also supplement these with ESLint plugins.</p>
<p>An <strong>ESLint plugin</strong> defines some linting rules. For example, if you look in the <a target="_blank" href="https://github.com/vercel/next.js/tree/canary/packages/eslint-plugin-next">GitHub repo for Next's ESLint plugin</a>, eslint-plugin-next, each file in the <a target="_blank" href="https://github.com/vercel/next.js/tree/canary/packages/eslint-plugin-next/src/rules"><code>src/rules</code></a> folder defines a linting rule as a TypeScript function. The <a target="_blank" href="https://github.com/vercel/next.js/blob/canary/packages/eslint-plugin-next/src/index.ts"><code>index.js</code></a> of the package then exports these rule functions in the <code>rules</code> object in its default export:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">module</span>.<span class="hljs-built_in">exports</span> = {
  rules: {
    <span class="hljs-string">'google-font-display'</span>: <span class="hljs-built_in">require</span>(<span class="hljs-string">'./rules/google-font-display'</span>),
    <span class="hljs-string">'google-font-preconnect'</span>: <span class="hljs-built_in">require</span>(<span class="hljs-string">'./rules/google-font-preconnect'</span>),
    <span class="hljs-string">'inline-script-id'</span>: <span class="hljs-built_in">require</span>(<span class="hljs-string">'./rules/inline-script-id'</span>),
    ...
</code></pre>
<p>The basic way to use these rules in your app is to install the plugin package, then reference it in the ESLint configuration file in the app's root folder.</p>
<p>For example, we can use rules from the <code>eslint-plugin-next</code> mentioned above by running <code>npm install --save-dev eslint-plugin-next</code>, then placing the following content in the ESLint config file <code>.eslintrc.json</code> in the app root:</p>
<pre><code class="lang-json">{
    plugins: [<span class="hljs-string">"next"</span>],
    <span class="hljs-attr">"rules"</span>: {
        <span class="hljs-attr">"google-font-display"</span>: <span class="hljs-string">"warning"</span>,
        <span class="hljs-attr">"google-font-preconnect"</span>: <span class="hljs-string">"warning"</span>,
        <span class="hljs-attr">"inline-script-id"</span>: <span class="hljs-string">"error"</span>,
    }
}
</code></pre>
<p>If you now run <code>npx eslint .</code> in your app's root folder, ESLint will lint every JavaScript file in the app against each of the three rules configured above.</p>
<p>There are three severities you can assign to a rule when configuring it for use: <code>off</code>, <code>warning</code> and <code>error</code>. As the snippet above shows, you enable a rule by assigning to it a severity of <code>warning</code> or <code>error</code> in the app's <code>.eslintrc.json</code>.</p>
<p>When referencing a plugin in your app's ESLint configuration file, the prefix <code>eslint-plugin-</code> in the plugin's package name is omitted. This is why the package that contains linting rules for Next.js, <code>eslint-plugin-next</code>, is referenced only as <code>"next"</code> in the snippet above.</p>
<p>Since it is quite cumbersome to configure a severity level - <code>off</code>, <code>warning</code> or <code>error</code> - for every single rule from every plugin that you want to use, the norm is to reference an <a target="_blank" href="https://eslint.org/docs/latest/extend/shareable-configs">ESLint sharable config</a> , or <strong>config</strong> for short, that is exported by an NPM package. This is a JavaScript object that declares plugins and configures rules from these with severity levels just as we did above.</p>
<p>For example, the default export from <code>eslint-plugin-next</code> also contains several ESLint configs. Here is a another snippet from <a target="_blank" href="https://github.com/vercel/next.js/blob/canary/packages/eslint-plugin-next/src/index.ts"><code>index.js</code> of the plugin</a>, this time showing exported ESLint configs in addition to the <code>rules</code> object for exporting rule functions:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">module</span>.<span class="hljs-built_in">exports</span> = {
  rules: {
    <span class="hljs-string">'google-font-display'</span>: <span class="hljs-built_in">require</span>(<span class="hljs-string">'./rules/google-font-display'</span>),
    <span class="hljs-string">'google-font-preconnect'</span>: <span class="hljs-built_in">require</span>(<span class="hljs-string">'./rules/google-font-preconnect'</span>),
    <span class="hljs-string">'inline-script-id'</span>: <span class="hljs-built_in">require</span>(<span class="hljs-string">'./rules/inline-script-id'</span>),
    ...
},    
configs: {
    recommended: {
      plugins: [<span class="hljs-string">'@next/next'</span>],
      rules: {
        <span class="hljs-comment">// warnings</span>
        <span class="hljs-string">'@next/next/google-font-display'</span>: <span class="hljs-string">'warn'</span>,
        <span class="hljs-string">'@next/next/google-font-preconnect'</span>: <span class="hljs-string">'warn'</span>,
        ...

        <span class="hljs-comment">// errors</span>
        <span class="hljs-string">'@next/next/inline-script-id'</span>: <span class="hljs-string">'error'</span>,
        <span class="hljs-string">'@next/next/no-assign-module-variable'</span>: <span class="hljs-string">'error'</span>
        ...

      }
    },
    <span class="hljs-string">'core-web-vitals'</span>: {
      plugins: [<span class="hljs-string">'@next/next'</span>],
      <span class="hljs-keyword">extends</span>: [<span class="hljs-string">'plugin:@next/next/recommended'</span>],
      rules: {
        <span class="hljs-string">'@next/next/no-html-link-for-pages'</span>: <span class="hljs-string">'error'</span>,
        <span class="hljs-string">'@next/next/no-sync-scripts'</span>: <span class="hljs-string">'error'</span>,
      },
    },
}
</code></pre>
<p>As you can see, in addition to the rules (there are many more than those shown above), the plugin also exports two configs - <code>recommended</code> and <code>core-web-vitals</code> - that enable different selections of the rules defined in the plugin by assigning severity levels of <code>error</code> or <code>warning</code> to them.</p>
<p>The config that is normally used in Next.js projects is <code>core-web-vitals</code>. We can use this config object in our app’s ESLint configuration file (<code>.eslintrc.json</code> in app root) as follows:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"extends"</span>: [<span class="hljs-string">"plugin:next/core-web-vitals"</span>]
}
</code></pre>
<p>Thus is much simpler than declaring the plugin in <code>plugins</code> object and then assigning a severity level of <code>error</code> or <code>warning</code> to each rule from the plugin that we want to use.</p>
<p>Note the difference between <em>Configuration File</em> – this is  the ESLint configuration file named <code>.eslintrc.json</code> - and <em>Shareable Config</em> (or <em>config</em> for short) – this is an object that configures some rules from an ESLint plugin for use in a client project by assigning severities to selected rules.</p>
<p>Contents of the configuration file are themselves a config. But in configuration files, we do not typically import a plugin and configure all rules from it that we want to use. Instead we almost always import a well-known/trusted config object that is exported by an NPM package. Such a config object - one that is exported by an NPM package for use in ESLint configuration files (in other packages/apps) - is also known as a <a target="_blank" href="https://eslint.org/docs/latest/extend/shareable-configs">shareable config</a>.</p>
<p>Typically, plugins - these define ESLint rules as JavaScript/TypeScript functions - also bundle their rules into one or more shareable configs. The <code>recommended</code> config from plugin <code>eslint-plugin-next</code> that we used above is just one such config.</p>
<p>Shareable configs do not only come from plugin packages, although it is customary for plugins to also export one or more shareable configs composed of their own rules. Other packages, whose names begin with <code>eslint-config-</code> (as opposed to <code>eslint-plugin-</code>) can provide one or more named configs.</p>
<p>Next.js provides one such package named <code>eslint-config-next</code>. This re-exports configs <code>recommended</code> and <code>core-web-vitals</code> from the plugin. It also re-exports (in v15 and above of the package) a config of TypeScript linting rules from plugin <code>typescript-eslint/eslint-plugin</code>. So instead of using <code>recommended</code> config from the plugin like we have done above:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"extends"</span>: [<span class="hljs-string">"plugin:next/core-web-vitals"</span>]
}
</code></pre>
<p>we could have installed the package <code>eslint-config-next</code> and used that in <code>.eslintrc.json</code>:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"extends"</span>: [<span class="hljs-string">"next/core-web-vitals"</span>]
}
</code></pre>
<p>Since the package's name is not prefixed with <code>plugin:</code>, ESLint considers it to be a config package, reconstructing the name as <code>eslint-config-next</code> rather than as <code>eslint-plugin-next</code>. Notice how with config packages also, we delete the canonical prefix <code>eslint-config-</code> when referencing it in the ESLint configuration file.</p>
<p><strong>NOTE:</strong> In order to reference a shareable config provided by a package that only provides such configs but does not define or export linting rules, such as <code>eslint-config-next</code>, all you need to do is install the package and add the shareable config to <code>"extends"</code> in your <code>.eslintrc.js</code>. This is shown in the snippet above.</p>
<p>But if you want to reference a shareable config from an ESLint <strong>plugin</strong>, for example the package <code>eslint-plugin-storybook</code> which exports a shareable config named <code>recommended</code>, but you do NOT want to configure individual rules exported by the plugin, then I am not sure whether you need to declare the package in <code>"plugins"</code> array in your <code>.eslintrc.js</code> or not. I would declare it in <code>"plugins"</code> anyway as it may actually be necessary but wouldn't do any harm if it's not. Almost every ESLint example that I have come across, such as <a target="_blank" href="https://typescript-eslint.io/getting-started/legacy-eslint-setup#step-2-configuration">this one</a><a target="_blank" href="https://typescript-eslint.io/getting-started/legacy-eslint-setup#step-2-configuration">, does t</a>he same. </p>
<p>For example, if I want to use shareable config <code>recommended</code> from plugin <code>eslint-plugin-storybook</code>, and even though I would not be configuring any individual linting rules provided by this plugin, I would both reference the config in <code>"extends"</code> and, just to be safe, declare the plugin itself in <code>"plugins"</code> (after installing the plugin via command <code>npm install -D eslint-plugin-storybook</code>):</p>
<pre><code class="lang-json">{
    <span class="hljs-attr">"plugins"</span>: [<span class="hljs-string">"storybook"</span>],
    <span class="hljs-attr">"extends: ["</span>plugin:storybook/recommended<span class="hljs-string">",]
}</span>
</code></pre>
<p>It is possible to reference multiple shareable configs in <code>extends</code>. In this case, all the rules from all configs are used - except where there are multiple configs that each provide a rule with the same name. In this case the last config, proceeding left to right, wins. This is to say where there is a naming conflict, ESLint will use the rule from the last config on the list.</p>
<p>It is possible to use <a target="_blank" href="https://eslint.org/docs/latest/use/configure/configuration-files-deprecated">ESLint configuration file formats other than JSON</a>. You can provid<a target="_blank" href="https://typescript-eslint.io/getting-started/legacy-eslint-setup#step-2-configuration">e the sa</a>me information as in an <code>.eslintrc.json</code> file in a JavaScript (<code>.eslintrc.js</code> or <code>.eslintrc.cjs</code>) or yaml (<code>.eslintrc.yml</code> or <code>.eslintrc.yaml</code>) file instead.</p>
<p>Also, ESLint has a new configuration file format often called <a target="_blank" href="https://eslint.org/docs/latest/use/configure/configuration-files">flat config</a> (which I haven't used here) where the config files are either JavaScript or TypeScript files.</p>
<p>Armed with an understanding of how to configure ESLint for use, you are ready to set up ESLint in your Next.js project. The sections below shows you how to do this.</p>
<h3 id="heading-eslint-setup-for-typescript">ESLint Setup for TypeScript</h3>
<p><strong>If your app uses TypeScript</strong>, modify the ESLint configuration file (.esilntrc.json) as follows:</p>
<ol>
<li><p>On the terminal, in app's root folder, run the following command:</p>
<pre><code class="lang-bash"> npm install --save-dev @typescript-eslint/parser @typescript-eslint/eslint-plugin typescript
</code></pre>
<p> <code>@typescript-eslint/eslint-plugin</code> provides a number of linting rules for TypeScript files, as well as shareable configs, that augment the checking that the TypeScript compiler does.</p>
<p> <code>@typescript-eslint/parser</code> is a parser that allows ESLint to parse TypeScript files (by default it can only parser JavaScript files).</p>
<p> I am adding TypeScript compiler as a package - <code>typescript</code> - because <a target="_blank" href="https://typescript-eslint.io/getting-started"><code>typescript-eslint</code> Getting Started instructions</a> do the same.</p>
</li>
<li><p>In app root folder, rename <code>.eslintrc.json</code> to <code>.eslintrc.js</code>. Then Replace contents of <code>.eslintrc.js</code> in app root with the following:</p>
<pre><code class="lang-javascript"> <span class="hljs-comment">/* eslint-env node */</span>
 <span class="hljs-built_in">module</span>.exports = {
   <span class="hljs-attr">root</span>: <span class="hljs-literal">true</span>,
   <span class="hljs-attr">extends</span>: [
     <span class="hljs-string">'next/core-web-vitals'</span>,
   ],
   <span class="hljs-attr">plugins</span>: [<span class="hljs-string">'@typescript-eslint'</span>, <span class="hljs-string">'tailwindcss'</span>],
   <span class="hljs-attr">parser</span>: <span class="hljs-string">'@typescript-eslint/parser'</span>,
   <span class="hljs-attr">overrides</span>: [
     {
       <span class="hljs-attr">files</span>: [<span class="hljs-string">'*.ts'</span>, <span class="hljs-string">'*.tsx'</span>],
       <span class="hljs-attr">parserOptions</span>: {
         <span class="hljs-attr">project</span>: [<span class="hljs-string">'./tsconfig.json'</span>],
         <span class="hljs-attr">projectService</span>: <span class="hljs-literal">true</span>,
         <span class="hljs-attr">tsconfigRootDir</span>: __dirname,
       },
       <span class="hljs-attr">extends</span>: [
         <span class="hljs-string">'next/core-web-vitals'</span>,
         <span class="hljs-string">'plugin:@typescript-eslint/recommended'</span>,
         <span class="hljs-comment">//'plugin:@typescript-eslint/recommended-type-checked',</span>
         <span class="hljs-comment">// 'plugin:@typescript-eslint/strict-type-checked',</span>
         <span class="hljs-comment">// 'plugin:@typescript-eslint/stylistic-type-checked',</span>
       ]
     },
   ],
 };
</code></pre>
<p> <strong>This is what the various lines of this file do</strong>:</p>
<p> <code>/* eslint-env node */</code> stops ESLint from complaining that this is a CommonJS module. We have had to put this in because ESLint, as we have configured it here, does not allow CommonJS modules (which <code>.eslintrc.js</code> is, see <code>module.exports = ...</code> at the top) and expects modules in the project to be ES6.</p>
<p> <code>root: true</code> says this is the topmost ESLint configuration file even though there may be nested ESLint configs in subfolders.</p>
<p> <code>extends:</code> specifies various ESLint configs, each of which enables a collection of linting rules.</p>
<p> <code>'next/core-web-vitals'</code> is a config provided by <code>eslint-config-next</code> that bundles Next.js-specific rules (both for JavaScript and TypeScript, from an inspection of its code on GitHub).</p>
<p> The <code>recommended-type-checked</code> config (used in a nested <code>extends</code> within <code>overrides</code> object - this is explained shortly) is provided by <code>@typescript-eslint/eslint-plugin</code>. This plugin is part of the <a target="_blank" href="https://typescript-eslint.io/">typescript-eslint</a> project that publishes packages for linting rules and parsers to support linting of TypeScript files by ESLint.</p>
<p> The configs used is <a target="_blank" href="https://typescript-eslint.io/users/configs#recommended-configurations">described here</a>. It is a superset of the non-type checked versions of the config, <code>recommended</code>. It adds linting rules which use TypeScript's type checking API for additional type information. These rules are more powerful than those contained in the base, non-type-checked <code>recommended</code> config that only rely on the ESLint parser for TypeScript - package <code>@typescript-eslint/parser</code>.</p>
<p> You might prefer to use the <code>strict-type-checked</code> and <code>stylistic-type-checked</code> configs, also provided by <code>@typescript-eslint/eslint-plugin</code>. These are stricter than what I have used.</p>
<p> The least strict choice for TypeScript linting would probably be the <code>recommended</code> config. This is what is re-exported by <code>eslint-plugin-next</code> as config named <code>typescript</code> and is referenced in <a target="_blank" href="https://nextjs.org/docs/app/building-your-application/configuring/eslint#typescript">Next.js instructions for setting up ESLint with TypeScript</a> as <code>next/typescript</code> (at least as of the time of this writing, September 2024). I prefer the config I have used instead.</p>
<p> <code>parser: '@typescript-eslint/parser'</code> specifies the ESLint TypeScript parser to be used instead of the default Espree parser which cannot parser TypeScript files.</p>
<p> <code>parserOptions:</code> tells the parser where to find the <code>tsconfig.json</code> file. This information allows the rules in the type-checked config used above - <code>recommended-type-checked</code> - to use TypeScript type checking APIs.</p>
<p> If we were using non-type-checked rules contained in other configs exported by the plugin, such as the <code>recommended</code> config, we would not need to provide this information.</p>
<p> <code>plugins: ['@typescript-eslint']</code> : I don't know what the purpose of this line is. It shouldn't be necessary and I have tested that the given ESLint configuration works fine without it. But it doesn't do any harm and was contained in <a target="_blank" href="https://typescript-eslint.io/getting-started/typed-linting">an example</a> in the plugin's documentation from which I adapted the above config. So I have kept it.</p>
<p> The <code>overrides</code> section ensures that the TypeScript parser options that we’ve had to configure in order to support type-checked configs apply only to <code>.ts</code> and <code>.tsx</code> extensions (from <a target="_blank" href="https://stackoverflow.com/questions/58510287/parseroptions-project-has-been-set-for-typescript-eslint-parser">this excellent StackOverflow answer</a>). Otherwise, if <code>parser</code> and <code>parserOptions</code> objects had been at the top level, then running ESLint on the project would throw errors on <code>.js</code> files.</p>
<p> This is a problem as we have several <code>.js</code> config files including the <code>.eslintrc.js</code> itself, so there will be linting errors. We can avoid these errors by using the override.</p>
</li>
</ol>
<h3 id="heading-eslint-setup-for-tailwind">ESLint Setup for Tailwind</h3>
<p><strong>If your app uses Tailwind,</strong> modify the config as follows:</p>
<ol>
<li><p>On the terminal, in app's root folder, run <code>npm install --save-dev eslint-plugin-tailwindcss</code></p>
</li>
<li><p>In ESLint config, add <code>"plugin:tailwindcss/recommended"</code> to the END of <code>extends</code>:</p>
<pre><code class="lang-json"> {
   <span class="hljs-attr">"extends"</span>: [<span class="hljs-string">"next/core-web-vitals"</span>, ..., <span class="hljs-string">"plugin:tailwindcss/recommended"</span>],

 }
</code></pre>
</li>
<li><p>In the ESLint config, add <code>"tailwindcss"</code> to <code>plugins</code> and add a <code>rules</code> object as shown below:</p>
<pre><code class="lang-json"> {
   <span class="hljs-attr">"plugins"</span>: [..., <span class="hljs-string">"tailwindcss"</span>],
   <span class="hljs-attr">"rules"</span>: {
     <span class="hljs-attr">"tailwindcss/classnames-order"</span>: <span class="hljs-string">"off"</span>
   },          
 }
</code></pre>
</li>
<li><p><strong>If your app uses TYPESCRIPT,</strong> then also add <code>"plugin:tailwindcss/recommended"</code> to inner <code>extends</code> inside <code>overrides</code> and duplicate the <code>rules</code> object inside <code>overrides</code>:</p>
<pre><code class="lang-javascript">   {
     ...
     overrides: [
     {
        <span class="hljs-attr">extends</span>: [<span class="hljs-string">"next/core-web-vitals"</span>, ..., <span class="hljs-string">"plugin:tailwindcss/recommended"</span>],
        <span class="hljs-attr">rules</span>: {
          <span class="hljs-string">'tailwindcss/classnames-order'</span>: <span class="hljs-string">'off'</span>,
        },
     }
   }
</code></pre>
</li>
</ol>
<p>In the Tailwind setup steps above, we have installed the package for the ESLint plugin for Tailwind, <code>eslint-plugin-tailwindcss</code>, and used the config <code>recommended</code> provided by the plugin.</p>
<p><code>eslint-plugin-tailwind</code> provides some useful linting rules for Tailwind CSS classes used in HTML or JSX/TSX markup. The biggest one for me is that if a class used in code is not a Tailwind class, there would be a linting error. This makes sense as when I am using Tailwind, I only use Tailwind-generated classes and do not define my own CSS classes.</p>
<p>The plugin also has a rule that checks that the sequence of Tailwind class names used in the <code>class</code> or <code>className</code> attribute in markup follows a canonical ordering. But we installed <code>prettier-plugin-tailwindcss</code> in our Prettier configuration above which also reorders Tailwind class names. So we don’t need this rule in ESLint and it might conflict with what Prettier does in our workflow.</p>
<p>We’ll turn this rule off, which is named <code>tailwindcss/classnames-order</code>, in the configuration above by declaring the plugin in <code>plugins</code> object, then setting the rule to <code>off</code> in the <code>rules</code> object.</p>
<h3 id="heading-eslint-setup-for-prettier">ESLint Setup for Prettier</h3>
<ol>
<li><p>On the terminal run:</p>
<pre><code class="lang-bash"> npm install --save-dev eslint-config-prettier
</code></pre>
</li>
<li><p>In ESLint config, add <code>"prettier"</code> to the END of <code>extends</code>:</p>
<pre><code class="lang-javascript"> {
   <span class="hljs-string">"extends"</span>: [<span class="hljs-string">"next/core-web-vitals"</span>, ..., <span class="hljs-string">"prettier"</span>]          
 }
</code></pre>
</li>
<li><p><strong>If your app uses TypeScript</strong>, then also add <code>"plugin:tailwindcss/recommended"</code> to the inner <code>extends</code> inside <code>overrides</code> also:</p>
<pre><code class="lang-javascript">  {
    ...
    overrides: [
    {
       <span class="hljs-string">"extends"</span>: [<span class="hljs-string">"next/core-web-vitals"</span>, ..., <span class="hljs-string">"prettier"</span>],
    }
  }
</code></pre>
<p> In the Prettier setup steps above, the config referenced as <code>prettier</code> is the name of the NPM package <a target="_blank" href="https://www.npmjs.com/package/eslint-config-prettier">eslint-config-prettier</a> with <code>eslint-config-</code> deleted. The default export from the package is an entire ESLint config object and this is the config we want to use.</p>
<p> So in this case, we do not suffix the name <code>prettier</code> with <code>/&lt;name of config&gt;</code> as we have done when referencing the named config <code>core-web-vitals</code> from package <code>eslint-config-next</code> when we referenced is as <code>next/core-web-vitals</code> (see step 1 above).</p>
<p> This config switches off those rules in ESLint that conflict with the code formatting done by Prettier. This should be the last config in <code>extends</code>.</p>
</li>
<li><p>Create <code>.eslintignore</code> in the project root. It doesn't need to have any content for now, but will come in handy in the future if ever you need to add folders or files that should be ignored by ESLint (see the final section of this post for an example).</p>
</li>
</ol>
<h2 id="heading-set-up-stylelint">Set Up Stylelint</h2>
<p>Stylelint is a linter for CSS and SCSS stylesheets.</p>
<p><strong>If you are using SCSS and NOT Tailwind,</strong> then set up Stylelint by following the instructions below. This set up will work for both CSS and SCSS files:</p>
<ol>
<li><p>On the terminal in project root run this command:</p>
<pre><code class="lang-bash"> npm install --save-dev sass
</code></pre>
<p> Next.js has <a target="_blank" href="https://nextjs.org/docs/basic-features/built-in-css-support#sass-support">built-in SASS/SCSS support</a> (so the Webpack config knows how to handle <code>.scss</code> and <code>.sass</code> files). But you still need to install a version of the <code>sass</code> package yourself, which is what we did above.</p>
</li>
<li><p>Next, install packages for Stylelint and its rule configs:</p>
<pre><code class="lang-bash"> npm install --save-dev stylelint stylelint-config-standard-scss stylelint-config-prettier-scss
</code></pre>
<p> Of these three packages:</p>
<ul>
<li><p><a target="_blank" href="https://stylelint.io/"><code>stylelint</code></a> is the linter.</p>
</li>
<li><p><a target="_blank" href="https://github.com/stylelint-scss/stylelint-config-standard-scss"><code>stylelint-config-standard-scss</code></a> is a Stylelint config that provides linting rules. It uses the Stylelint plugin <a target="_blank" href="https://www.npmjs.com/package/stylelint-scss">stylelint-css</a> and extends configs <a target="_blank" href="https://github.com/stylelint/stylelint-config-standard">stylelint-config-standard</a> which defines rules for vanilla CSS, and <a target="_blank" href="https://github.com/stylelint-scss/stylelint-config-recommended-scss">stylelint-config-recommended-scss</a> which defines SCSS specific rules. As a result, extending from this one config is enough to get linting support for both CSS and SCSS files.</p>
</li>
<li><p><a target="_blank" href="https://www.npmjs.com/package/stylelint-config-prettier-scss"><code>stylelint-config-prettier-scss</code></a> extends <a target="_blank" href="https://www.npmjs.com/package/stylelint-config-prettier">stylelint-config-prettier</a> and turns off those Stylint rules that conflict with Prettier's code formatting. This should be declared last in <code>extends:</code> array in <code>.stylelintrc.json</code> (as shown below).</p>
</li>
</ul>
</li>
<li><p>Now, create <code>.stylelintrc.json</code> in project root with the following contents:</p>
<pre><code class="lang-json"> {
   <span class="hljs-attr">"extends"</span>: [
     <span class="hljs-string">"stylelint-config-standard-scss"</span>,
     <span class="hljs-string">"stylelint-config-prettier-scss"</span>
   ],
   <span class="hljs-attr">"rules"</span>: {
     <span class="hljs-attr">"selector-class-pattern"</span>: <span class="hljs-literal">null</span>
   }
 }
</code></pre>
<p> The <code>"extends"</code> section declares the two Stylelint configs whose NPM packages we installed in the previous step.</p>
<p> The <code>"rules"</code> section is used to configure stylints rules. Here you can turn on or off, or configure the behavior of, individual Stylelint rules.</p>
<p> You can turn off a rule by setting it to <code>null</code>, as I have done for <code>"selector-class-pattern"</code>. I turned it off because it insists on having CSS classes in the so called kebab case (for example, <code>.panel-quiz</code> instead of <code>.panelQuiz</code>). I find it inconvenient for various reasons so I turned it off.</p>
</li>
<li><p>Next, create <code>.stylelintignore</code> in the project root with the following contents:</p>
<pre><code class="lang-json"> styles/globals.css
 styles/Home.module.css
 coverage
</code></pre>
<p> I created this file so that the two stylesheets generated by the Next.js CLI which do not comply with the linting rules can get ignored (there might be a better way of doing this but this works for me). Also, files in <code>coverage</code> folder do not need to be linted and would likely throw up errors.</p>
</li>
</ol>
<h2 id="heading-set-up-packagejson-scripts">Set Up <code>package.json</code> Scripts</h2>
<ol>
<li><p>The most important script is <code>"build"</code>. The default command for this script, <code>next build</code>, runs ESLint but not Prettier or (if you are using SCSS) Stylelint. So modify it in <code>package.json</code> file as follows:</p>
<p> <strong>If your app uses Tailwind, then:</strong></p>
<pre><code class="lang-json"> {
   <span class="hljs-attr">"scripts"</span>: {
     <span class="hljs-attr">"build"</span>: <span class="hljs-string">"prettier --check . &amp;&amp; next build"</span>,
     ...
</code></pre>
<p> <strong>Otherwise, if your app uses SCSS, then:</strong></p>
<pre><code class="lang-json"> {
   <span class="hljs-attr">"scripts"</span>: {
     <span class="hljs-attr">"build"</span>: <span class="hljs-string">"prettier --check . &amp;&amp; stylelint --allow-empty-input \"**/*.{css,scss}\" &amp;&amp; next build"</span>,
     ...
</code></pre>
<p> With this tweak to the existing <code>build</code> script, we can run <code>npm run build</code> either locally or in a CI/CD pipeline and it will fail not only on ESLint failure (this was the case before) but also on Prettier formatting or Stylelint failure.</p>
<p> Indeed if you <a target="_blank" href="https://nextjs.org/learn/basics/deploying-nextjs-app/deploy">deploy your app to Vercel</a>, the default pipeline there also calls <code>npm run build</code>. So when I introduced an error in one of my stylesheets, then deployed to Vercel, I got the following Stylelint error during deployment:</p>
<p> <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/om6228598f4pvb2iuwj9.png" alt="Stylelint errors when build script is run to build the app in Vercel's deployment pipeline" width="600" height="400" loading="lazy"></p>
<p> Note that I used the <code>--check</code> flag with <code>prettier</code> in the script (that is, I used the command <code>prettier --check .</code>). This runs Prettier in check mode, so it only checks for correct formatting and does not change the formatting.</p>
<p> I did this because the <code>build</code> script is what Vercel's deployment pipeline calls by default to build the code, and I don't want formatting to change during a CI build (nor do I want to tinker with Vercel defaults unless I absolutely have to).</p>
<p> To run Prettier locally to actually format the codebase, I define a separate <code>build:local</code> script which is same as <code>build</code> but runs Prettier without the <code>--check</code> flag, as well as a separate <code>format</code> script just to format with Prettier (but not build). These are set up below.</p>
</li>
<li><p>Set up the <code>"format"</code> script in your <code>package.json</code>. This formats the codebase with Prettier and comes in handy every now and then:</p>
<pre><code class="lang-json"> {
   <span class="hljs-attr">"scripts"</span>: {
     ...
     <span class="hljs-attr">"format"</span>: <span class="hljs-string">"prettier --write ."</span>
</code></pre>
</li>
<li><p>I recommend setting up a <code>build:local</code> script as follows:</p>
<p> <strong>If your app uses Tailwind,</strong> then:</p>
<pre><code class="lang-json"> <span class="hljs-string">"build:local"</span>: <span class="hljs-string">"prettier --write . &amp;&amp; next build"</span>
</code></pre>
<p> <strong>Otherwise, if your app uses SCSS,</strong> then:</p>
<pre><code class="lang-json"> <span class="hljs-string">"build:local"</span>: <span class="hljs-string">"prettier --write . &amp;&amp; stylelint --allow-empty-input \"**/*.{css,scss}\" &amp;&amp; next build"</span>
</code></pre>
<p> Since we cannot format the code with Prettier prior to executing <code>next build</code> in the existing <code>build</code> script (for reasons described above), we can use this script locally to format code then lint and build in one go.</p>
</li>
</ol>
<h2 id="heading-set-up-lint-staged">Set Up lint-staged</h2>
<p><a target="_blank" href="https://github.com/okonet/lint-staged">lint-staged</a> is a package that you can use to run formatting and linting commands on staged files in a Git repo. Staged files are those that have been added to the Git index using <code>git add .</code>. These are the files that have changed since the last commit and will get committed when you next run <code>git commit</code>.</p>
<p><a target="_blank" href="https://github.com/typicode/husky">Husky</a> is the typical choice in Node.js packages for registering commands to run in Git hooks. For example, registering the command <code>npx lint-staged</code> with Husky to run in the Git pre-commit hook means lint-staged will run automatically whenever you execute <code>git commit</code>.</p>
<p>At that time, the formatter (Prettier) and linters (ESLint or Stylelint) that have been configured to run in the lint-staged configuration file will run on the staged files. If there are any errors during formatting checks or linting, the commit will fail.</p>
<p>Whenever <code>git commit</code> fails due to linting errors, we can fix those, then run <code>git add .</code> and <code>git commit</code> again. Thus code only ever gets into the repo after it has been consistently formatted and verified to be free of linting errors. This is particularly advantageous in a team setting.</p>
<p>I prefer to only run <code>prettier --check .</code> on staged files. In particular, I do not change formatting of staged files and do not lint during a commit, for the following reasons:</p>
<p><strong>Reason for not formatting code:</strong> I almost always build and test my code before committing. Any code formatting should have happened prior to or during this local build and test.</p>
<p>I find the idea that code going into my repo should change automatically just as it is being committed <em>after</em> I have ascertained that any code changes are good to go, a little bit unappealing.</p>
<p><strong>Reason for not linting code:</strong> With TypeScript code, the compiler can catch a huge number of issues in code. The additional linting rules provided by <code>eslint-typescript/eslint-plugin</code> <a target="_blank" href="https://typescript-eslint.io/troubleshooting/faqs/typescript#why-dont-i-see-typescript-errors-in-my-eslint-output">only supplement the checks made by the TypeScript compiler</a>. So if I am linting code in staged files at commit time, I should build as well (so that the TypeScript compiler runs).</p>
<p>But building can be very time consuming on a large codebase. Besides, I almost always build and test before committing. Doing so implicitly runs linting (<code>next build</code> runs ESLint and my build scripts in <code>package.json</code>, as set up above, run any other linting that is necessary e.g. StyleLint). So I don’t feel the need to repeat lint or build on staged files via lint-staged.</p>
<p>Any lint- or build errors that might occasionally slip through into the Git repo would be pointed out when my Continuous Delivery pipeline builds the codebase (let's say in GitHub Actions on in Vercel's build pipeline). I can fix them then, in the pull request, before merge to <code>main</code>.</p>
<p>So, my personal preference is only to check for formatting on staged files, and neither reformat nor lint the code. This prevents inconsistently formatted code from getting into the Git repo where inconsistent formatting would make comparisons between different versions of the same file difficult.</p>
<p><strong>So now, set up lint-staged and Husky as follows:</strong></p>
<p><strong>Note</strong>: These tools need to be set up in the root of the Git repo. If you have a monorepo that contains multiple projects, including the Next.js app in which you performed the setup above, then open the root folder of this monorepo in your code editor and also <code>cd</code> into it on the terminal. Otherwise stay in the folder of your Next.js app.</p>
<ol>
<li><p>If you have a monorepo and its root folder does NOT have a <code>package.json</code> file, initialize an NPM package in the root:</p>
<pre><code class="lang-bash"> npm init -y
</code></pre>
</li>
<li><p>Install the lint-staged package:</p>
<pre><code class="lang-bash"> npm install --save-dev lint-staged
</code></pre>
</li>
<li><p>Create a file named <code>lint-staged.config.js</code> with the following contents:</p>
<pre><code class="lang-javascript"> <span class="hljs-comment">/* eslint-env node */</span>
 <span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'path'</span>);
 <span class="hljs-keyword">const</span> formatCommand = <span class="hljs-string">'prettier . --check'</span>;

 <span class="hljs-built_in">module</span>.exports = {
   <span class="hljs-string">'*'</span>: formatCommand,
 };
</code></pre>
</li>
<li><p>If you are in a monorepo, the <code>lint-staged.config.js</code> created above needs needs to be tweaked because in this, Prettier would be invoked from multi-repo root but the files it needs to format, as well as its config file (<code>.prettierrc.json</code>) and its ignore file (<code>.prettierignore</code>), are all in the subfolder that contains the Next.js app. Do the following:</p>
<ul>
<li><p><code>npm install --save-dev prettier prettier-plugin-tailwindcss</code></p>
</li>
<li><p>Replace the contents of <code>lint-staged.config.js</code> with the following. Make sure to substitute <code>&lt;Next.js app subfolder&gt;</code> with the actual name of your Next.js app folder:</p>
<pre><code class="lang-bash">  /* eslint-env node */
  const path = require(<span class="hljs-string">"path"</span>);
  const formatCommand =
    <span class="hljs-string">"prettier --check --config ./&lt;Next.js app subfolder&gt;/.prettierrc.json --ignore-path ./&lt;Next.js app subfolder&gt;/.prettierignore"</span>;

  module.exports = {
    <span class="hljs-string">"./&lt;Next.js app subfolder&gt;/**/*"</span>: formatCommand,
  };
</code></pre>
</li>
</ul>
</li>
<li><p>Install the Husky NPM package.</p>
<pre><code class="lang-bash"> npm install --save-dev husky
</code></pre>
</li>
<li><p>Run the following on the terminal in app root to configure Husky to run <code>lint-staged</code> whenever <code>git commit</code> runs (in Git's pre-commit hook):</p>
<pre><code class="lang-bash"> npx husky init
 <span class="hljs-built_in">echo</span> <span class="hljs-string">"npx lint-staged"</span> &gt; .husky/pre-commit
</code></pre>
<p> You should now have a file <code>.husy/pre-commit</code> in your app's folder (or in your monorepo root folder, if you're  working in a monorepo) with only one line: <code>npx lint-staged</code>.</p>
</li>
</ol>
<h2 id="heading-set-up-vs-code-extensions">Set Up VS Code Extensions</h2>
<p>If you use VS Code as your code editor, you can install the following VS Code extensions to provide linting and formatting on file save and syntax highlight on linting errors:</p>
<ul>
<li><p><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">ESLint extension</a></p>
</li>
<li><p><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode">Prettier extension</a></p>
</li>
<li><p><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=stylelint.vscode-stylelint">Stylelint extension</a> (if you're using SCSS and not Tailwind)</p>
</li>
<li><p><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss">TaliwindCSS extension</a> (if you are using Tailwind and not SCSS)</p>
</li>
</ul>
<p><strong>Put the following in a</strong> <code>settings.json</code> <strong>file in the</strong> <code>.vscode</code> <strong>folder in the project</strong> (you can of course put these settings in you User Preferences file also. You can access it from Command Palette <strong>Ctrl + P</strong>).</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"[javascript]"</span>: {
    <span class="hljs-attr">"editor.defaultFormatter"</span>: <span class="hljs-string">"esbenp.prettier-vscode"</span>
  },
  <span class="hljs-attr">"[typescript]"</span>: {
    <span class="hljs-attr">"editor.defaultFormatter"</span>: <span class="hljs-string">"esbenp.prettier-vscode"</span>
  },
  <span class="hljs-attr">"[typescriptreact]"</span>: {
    <span class="hljs-attr">"editor.defaultFormatter"</span>: <span class="hljs-string">"esbenp.prettier-vscode"</span>
  },
  <span class="hljs-attr">"[javascriptreact]"</span>: {
    <span class="hljs-attr">"editor.defaultFormatter"</span>: <span class="hljs-string">"esbenp.prettier-vscode"</span>
  },
  <span class="hljs-attr">"[scss]"</span>: {
    <span class="hljs-attr">"editor.defaultFormatter"</span>: <span class="hljs-string">"esbenp.prettier-vscode"</span>
  },
  <span class="hljs-attr">"stylelint.validate"</span>: [<span class="hljs-string">"css"</span>, <span class="hljs-string">"scss"</span>],
  <span class="hljs-attr">"editor.formatOnSave"</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">"eslint.useFlatConfig"</span>: <span class="hljs-literal">false</span>
}
</code></pre>
<p>As they’re set up, the extensions will lint and format on Save.</p>
<p><code>"eslint.useFlatConfig": false</code> ensures that the ESLint extension is able to pick up your <code>.eslintrc.js</code> and doesn't look for <code>eslint.config.js</code> which is the filename for a config file in ESLint's new <a target="_blank" href="https://eslint.org/blog/2022/08/new-config-system-part-2/">flat config format</a> that I have NOT used in this post.</p>
<p>If you encounter any problems in getting the ESLint extension to work, turn on the extension's debug output by adding line <code>"eslint.debug": true</code> to the end of the <code>.vscode/settings.json</code> you just created. Then restart VS Code and look at ESLint extension's output in the OUTPUT window:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1746569174032/3e705307-1588-4dd2-bd28-98c2d118a07a.png" alt="3e705307-1588-4dd2-bd28-98c2d118a07a" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<h2 id="heading-final-checks-and-troubleshooting">Final Checks and Troubleshooting</h2>
<p>Now it’s time to build and commit:</p>
<pre><code class="lang-bash">npm run format
npm run build
git add .
git commit -m <span class="hljs-string">"fix: set up linting and formatting"</span>
</code></pre>
<p>Building and committing is a good sanity check for the setup we just did.</p>
<p>If anything had not been set up correctly, you might get errors either during build or at commit.</p>
<p>If you already had some code in the project, then there might be a few errors when you commit. Typically, these can be resolved by:</p>
<ul>
<li><p>Adding folders or files to one of the <code>*ignore</code> files. For example, I already had some code in my project with <a target="_blank" href="https://storybook.js.org/">Storybook</a> installed. So I had to add folders <code>.storybook</code> and <code>storybook-static</code> to each of <code>.stylelintignore</code>, <code>.eslintignore</code> and <code>.prettierignore</code> as all three tools complained about them.</p>
<pre><code class="lang-bash">  stories
  storybook-static
</code></pre>
</li>
<li><p>If Prettier complains (shows a <code>[warn]</code> or <code>[error]</code> on the terminal) about some files not being formatted properly, you either need to add them to <code>.prettierignore</code> or format them by running <code>npm run format</code> in the folder of your Next.js app.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1743632684491/fa5f073f-4de4-4b55-be00-184f8a36c802.png" alt="fa5f073f-4de4-4b55-be00-184f8a36c802" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
</li>
<li><p>Adding plugins for specific file types. For example, I had Gherkin <code>.feature</code> files in my project to describe integration tests. Prettier couldn't format these. So I added the <a target="_blank" href="https://www.npmjs.com/package/prettier-plugin-gherkin?activeTab=readme">prettier-plugin-gherkin</a> by simply running:</p>
<pre><code class="lang-plaintext">  npm install  prettier-plugin-gherkin --save-dev
</code></pre>
<p>  Note that <a target="_blank" href="https://prettier.io/docs/en/plugins.html">usually it is enough to install the package for a Prettier plugin</a> for Prettier to locate it and additional configuration is not required.</p>
<p>  Likewise, ESLint complained when it encountered <code>.cy.ts</code> files containing Cypress interaction tests for my app. To resolve this linting error, I installed the NPM package for Cypress ESLint plugin and configured it <a target="_blank" href="https://github.com/cypress-io/eslint-plugin-cypress#installation">as described here</a> (unlike Prettier, to get this ESLint package to work, some configuration was required).</p>
</li>
<li><p>The typescript config might be too strict and there might be a lot of errors when you build, such as:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725931098287/9618aff2-844b-4b15-9b14-2c8d377bf8c8.png" alt="ESLint error that occur on build when an ESLint config for TypeScript is used that is too strict." width="600" height="400" loading="lazy"></p>
<p>  If you do not want to fix individual errors in your existing codebase, and they are too many to disable specific rules at error locations using ESLint comments (see below), then the simplest solution would be to disable the <code>@typescript-eslint/recommended-type-checked</code> config by commenting it out in <code>.eslintrc.js</code> and uncommenting <code>@typescript-eslint/recommended</code> which is less strict.</p>
</li>
<li><p>Sometimes it is safe to turn off a linting rule at a specific line or for a whole file. While I am always wary of doing this, in a (deliberately bad) experimental code file, I had many instances of an error that VS Code ESLint extension pointed. This was not caught before but was now being pointed out because strict TypeScript linting rules had been enabled:</p>
<p>  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sg2ne9v2w8dbquv9ytad.png" alt="Intellisense in VS Code showing an ESLint error on a lint of code in a file." width="600" height="400" loading="lazy"></p>
<p>  So I pressed <code>Ctrl + .</code> to Show Code Actions (I could instead have clicked the yellow lighbulb icon shown next to the issue), then selected “Disable <code>@typescript/no-non-null-assertion</code> for the entire file”.</p>
<p>  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqume7pod3jzflvblrus.png" alt="VS Code shows helpful tooltips when you click the bulb icon in the gutter. One of these allows y uoto disable an ESLint rule that is causing an error on the currently selected line of code." width="600" height="400" loading="lazy"></p>
<p>  This placed the comment <code>/* eslint-disable @typescript-eslint/no-non-null-assertion */</code> on top of my file to disable all instances of that particular error within the file:</p>
<p>  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1d2kml4o11xpqaenk93b.png" alt="Special &quot;eslint-disable&quot; comment on top of a code file that disable a specific ESLint rule through the file." width="600" height="400" loading="lazy"></p>
</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This tutorial showed you how to configure linting and formatting tools in your Next.js app. I hope that it also gave you the background necessary both to understand the configurations given, and to customize them as needed.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Add ESLint to Your React Project ]]>
                </title>
                <description>
                    <![CDATA[ By Jacob Isah  As a React developer, maintaining code quality is essential for building robust and maintainable applications. Fortunately, there's a powerful tool called ESLint that can significantly improve the quality of your React projects. In thi... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-add-eslint-to-your-react-project/</link>
                <guid isPermaLink="false">66d45f313a8352b6c5a2aa5d</guid>
                
                    <category>
                        <![CDATA[ Code Quality ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 08 Aug 2023 16:36:10 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/08/How-To-Add-ESLint-To-Your-React-Project-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Jacob Isah </p>
<p>As a React developer, maintaining code quality is essential for building robust and maintainable applications. Fortunately, there's a powerful tool called ESLint that can significantly improve the quality of your React projects.</p>
<p>In this article, we will explore how ESLint works and create a sign-up button component to demonstrate its benefits.</p>
<h2 id="heading-what-is-eslint">What is ESLint?</h2>
<p>ESLint is a popular open-source JavaScript linting utility. It analyzes your code for potential errors, and enforces coding standards while improving code quality. </p>
<p>It can also help you as a developer to identify and fix common mistakes, use best practices, and maintain consistency across your codebase. </p>
<p>You can integrate ESLint seamlessly into your React projects, providing real-time feedback and improving your overall code quality.</p>
<h2 id="heading-how-to-set-up-your-project">How to Set Up Your Project</h2>
<p>Let's start by setting up a new React project and installing ESLint. To demonstrate this, we will create a signup app in React. </p>
<p>Imagine that we want to store our project on the desktop, so first we will set up our file structure. Let's start by creating a root directory for our project on the desktop (<code>eslintExample</code> in this case).</p>
<pre><code class="lang-javascript">mkdir eslintExample 
cd eslintExample
</code></pre>
<h3 id="heading-install-the-react-app">Install the React app</h3>
<p>Now we'll use create-react-app to set up our React app.</p>
<pre><code class="lang-javascript">npx create-react-app signup-app
</code></pre>
<h3 id="heading-install-eslint">Install ESLint</h3>
<p>And you can use the following command to set up ESLint:</p>
<pre><code class="lang-javascript">npm install eslint eslint-plugin-react eslint-plugin-react-hooks --save-dev
</code></pre>
<p>Your directory structure should look like this:</p>
<pre><code class="lang-javascript">eslintExample/
  └── signup-app/
      └── node_modules/
      └── public/
      └── src/
          └── App.css
          └── App.js
          └── App.test.js
          └── SignupButton.js
          └── index.css
          └── logo.svg
          └── reportWebVitals.js
          └── setupTests.js
      └── .eslintrc.json
      └── .gitignore
      └── package-lock.json
      └── package.json
      └── README.md
</code></pre>
<h2 id="heading-how-to-configure-eslint-in-a-react-project">How to Configure ESLint in a React Project</h2>
<p>To work with ESLint in our React project, we need to configure it. To do this, the first step is to create a <strong>.eslintrc.json</strong> file in the project's root directory and add the following code:</p>
<pre><code class="lang-javascript">{
    <span class="hljs-string">"env"</span>: {
      <span class="hljs-string">"browser"</span>: <span class="hljs-literal">true</span>,
      <span class="hljs-string">"es2021"</span>: <span class="hljs-literal">true</span>
    },
    <span class="hljs-string">"extends"</span>: [<span class="hljs-string">"eslint:recommended"</span>, <span class="hljs-string">"plugin:react/recommended"</span>, <span class="hljs-string">"plugin:react-hooks/recommended"</span>],
    <span class="hljs-string">"parserOptions"</span>: {
      <span class="hljs-string">"ecmaVersion"</span>: <span class="hljs-number">12</span>,
      <span class="hljs-string">"sourceType"</span>: <span class="hljs-string">"module"</span>,
      <span class="hljs-string">"ecmaFeatures"</span>: {
        <span class="hljs-string">"jsx"</span>: <span class="hljs-literal">true</span>
      }
    },
    <span class="hljs-string">"plugins"</span>: [<span class="hljs-string">"react"</span>, <span class="hljs-string">"react-hooks"</span>],
    <span class="hljs-string">"rules"</span>: {
      <span class="hljs-comment">//add customize rules here as per your project's needs</span>
    }
}
</code></pre>
<p>The above configuration sets up ESLint to work with React and React Hooks using the recommended configurations. You can add or customize rules according to your project's specific requirements.</p>
<p><img src="https://lh5.googleusercontent.com/nEAJArNZ75TidB33XK-0_p4BQea9aTppnnFrk0u7znVKIpsmCortCwaZ0KFYbw_1OOkz_QcXC0cr1WGih89a1OY2REUCSIWKckpaGESNchz8xakqTAntBbpgDXabbrcf6kWZzwxpAZ14PC5xQb9h_A" alt="How to Add ESLint to Your React Project" width="600" height="400" loading="lazy">
<em>Created the.eslintrc.json file and added the configuration that allows ESLint to work with React.</em></p>
<h2 id="heading-how-to-create-the-signup-button-component">How to Create the Signup Button Component</h2>
<p>Now, let's create a simple Signup Button component (SignupButton.js) inside the "src" folder. </p>
<p>Inside the src file, create the SignupButton.js file. It should look like this: <code>src/SignupButton.js</code>. Inside your SignupButton.js, paste the following code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">const</span> SignupButton = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> handleSignup = <span class="hljs-function">() =&gt;</span> {
    alert(<span class="hljs-string">"Sign up successful!"</span>);
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleSignup}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"signup-button"</span>&gt;</span>
      Sign Up
    <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
  );
};
</code></pre>
<p>The above component is a basic button that triggers an alert when clicked, simulating the signup process. Now you can run this command:</p>
<pre><code class="lang-javascript">npm start
</code></pre>
<p>This will start our React app at the root of the project. Then you should see this error below:</p>
<p><img src="https://lh3.googleusercontent.com/O1FnEf68qsAo7FOqdNzFrcauVoHs3oKnQsbV4cNUAwFQbAjZZ9XhIHVhDSNiyZ3buIVF4-uCKnrkLjru6xRgjJo3tLJVq8vXn5s_GcdgnMoHCPPnDk0H2IGnhXVGPBZYABP3fbaoOe_GtYA60hU6Hw" alt="How to Add ESLint to Your React Project" width="600" height="400" loading="lazy">
<em>We started the local server, and we are seeing an error coming from the app.js file. React needs to be imported in app.js file. We need to export our button component.</em></p>
<p>This error might confuse you, because you may not know where it's coming from. We are seeing this error because we set up ESLint in our project, and it scans through our project to tell us that React needs to be imported in the <strong>app.js</strong> file.</p>
<p>But let’s run ESLint to see where the exact error is coming from.</p>
<h2 id="heading-how-to-run-eslint">How to Run ESLint</h2>
<p>With ESLint configured, let's run it to analyze our SignupButton component for potential issues. Open your terminal and run the following command at the root of your project:</p>
<pre><code class="lang-javascript">npx eslint src/SignupButton.js
</code></pre>
<p>ESLint will analyze the SignupButton.js file and display any problems it finds below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/08/image-101.png" alt="How to Add ESLint to Your React Project" width="600" height="400" loading="lazy">
<em>We run the <code>eslint</code> command to show us where we are having error: in the signup button and app.js</em></p>
<h2 id="heading-how-to-fix-the-eslint-issues">How to Fix the ESLint Issues</h2>
<p>From the above, you can see that ESLint identifies where we are having errors. We did not import React in our App.js file, and we did not export our signup button component. Let’s fix that.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/08/image-100.png" alt="How to Add ESLint to Your React Project" width="600" height="400" loading="lazy">
<em>We imported React in the app.js file and exported SignupButton component.</em></p>
<p>As you can see, our code was now built successfully.</p>
<p>We were able to export our SignupButton component and imported React in App.js component. This solves our problem.</p>
<p>If ESLint identifies any further issues in our SignupButton component, it will display them along with suggestions on how to fix them. </p>
<p>For example, ESLint might detect missing semicolons, unused variables, or violations of React best practices like the import React from “react” in the app.js file.</p>
<p>By addressing the issues highlighted by ESLint, we can ensure that our code adheres to best practices, is easier to read, and has fewer potential bugs.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>ESLint is an indispensable tool for React developers to maintain code quality and improve productivity. By integrating ESLint into your React projects, you can catch errors early, follow coding standards, and foster collaboration within your team.</p>
<p>In this article, I have explained how to set up ESLint in a React project and demonstrate its benefits with a simple Signup Button component. By utilizing ESLint effectively, you can write cleaner, more consistent code and deliver higher-quality React applications.</p>
<p>So, why wait? Start using ESLint in your React projects and watch your code quality soar. Happy coding! 🚀</p>
<p>Your feedback is highly appreciated.<br>you can follow me on <a target="_blank" href="https://twitter.com/_jayky">twitter</a> and <a target="_blank" href="https://www.linkedin.com/in/isahjacob/">linkedIn</a>  </p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use Linters and Code Formatters in Your Projects ]]>
                </title>
                <description>
                    <![CDATA[ Hi everyone! In this article we're going to take a look at two very useful tools we can use to make our lives easier when writing code: linting tools and code formatters. We're going to talk about what these tools are, how they work, why are they use... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/using-prettier-and-jslint/</link>
                <guid isPermaLink="false">66d45f27052ad259f07e4ad8</guid>
                
                    <category>
                        <![CDATA[ clean code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Prettier ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ German Cocca ]]>
                </dc:creator>
                <pubDate>Mon, 10 Apr 2023 22:02:17 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/04/alysa-bajenaru-88IV5AtWjB8-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Hi everyone! In this article we're going to take a look at two very useful tools we can use to make our lives easier when writing code: linting tools and code formatters.</p>
<p>We're going to talk about what these tools are, how they work, why are they useful, and finally see how we can implement them in a basic React project.</p>
<p>Let's go!</p>
<h1 id="heading-table-of-contents">Table of Contents</h1>
<ul>
<li><p><a class="post-section-overview" href="#heading-about-linting-tools">About linting tools</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-are-linting-tools">What are Linting Tools?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-are-linting-tools-useful">Why are Linting Tools Useful?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-main-linting-tools-in-the-market">Main Linting Tools in the Market</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-about-code-formatters">About code formatters</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-are-code-formatters">What are Code Formatters?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-are-code-formatters-useful">Why are Code Formatters Useful?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-main-code-formatters-available">Main Code Formatters Available</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-implement-eslint-and-prettier">How to Implement ESLint and Prettier</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-how-to-install-eslint">How to Install ESLint</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-install-prettier">How to Install Prettier</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-wrapping-up">Wrapping up</a></p>
</li>
</ul>
<h1 id="heading-about-linting-tools">About Linting Tools</h1>
<p>In the world of web development, linting tools have become an essential part of the developer's toolkit.</p>
<p>Linting tools are used to analyze source code for potential errors or stylistic issues, making it easier to maintain code quality and consistency across a project.</p>
<h2 id="heading-what-are-linting-tools">What are Linting Tools?</h2>
<p>Linting tools are automated tools that analyze source code to detect potential errors, security vulnerabilities, or coding style issues.</p>
<p>They are designed to help developers catch mistakes before they become a problem, and to promote best practices in coding.</p>
<p>The term "lint" comes from the name of the first lint tool, which was developed in the early 1970s by a team of Bell Labs researchers led by Stephen C. Johnson.</p>
<p>The original lint tool was designed to analyze C source code for potential errors and stylistic issues.</p>
<p>Since then, linting tools have evolved to work with a variety of programming languages, including JavaScript, Python, and Ruby.</p>
<h2 id="heading-why-are-linting-tools-useful">Why are Linting Tools Useful?</h2>
<p>Linting tools are useful for a number of reasons. Firstly, they help you catch errors early in the development process, when they are easier and cheaper to fix.</p>
<p>Secondly, they can help promote coding standards and best practices within a development team, ensuring that code is consistent and maintainable.</p>
<p>Finally, they can help you identify potential security vulnerabilities in your code, reducing the risk of a breach.</p>
<h2 id="heading-main-linting-tools-in-the-market">Main Linting Tools in the Market</h2>
<p>There are several linting tools available in the market today. Here are some of the most popular ones:</p>
<ol>
<li><p><strong>ESLint:</strong> <a target="_blank" href="https://eslint.org/">ESLint</a> is a widely used and highly configurable linter for JavaScript and TypeScript. It can be extended using plugins and supports various rule sets, making it a flexible tool for enforcing coding standards and preventing errors.</p>
</li>
<li><p><strong>JSHint:</strong> <a target="_blank" href="https://jshint.com/">JSHint</a> is a popular linter that has been around since 2010. It offers a simple configuration and a wide range of built-in rules to help developers avoid common pitfalls and improve code quality.</p>
</li>
<li><p><strong>JSLint:</strong> <a target="_blank" href="https://www.jslint.com/">JSLint</a> was one of the first linters to be developed for JavaScript, and it still sees some use today. It is known for its strictness and for enforcing a particular style of code, which can be helpful for maintaining consistency across a team.</p>
</li>
<li><p><strong>StandardJS:</strong> <a target="_blank" href="https://standardjs.com/">StandardJS</a> is a popular linter that aims to provide a "batteries included" approach to JavaScript linting. It has a minimal configuration and includes a set of opinionated rules designed to promote clean, readable code.</p>
</li>
</ol>
<p>And we should also talk about <strong>Typescript</strong>. When using <a target="_blank" href="https://www.typescriptlang.org/">TypeScript</a>, the TypeScript compiler itself acts as a linter. It checks the syntax of TypeScript code and provides warnings and errors when there are issues. This built-in linter can catch common mistakes and issues such as misspelled variable names, invalid method calls, and syntax errors.</p>
<p>The TypeScript compiler can be run using the <code>tsc</code> command in a terminal. When the <code>--noEmit</code> flag is used, the TypeScript compiler will only perform a syntax check without compiling the code to JavaScript. This allows the compiler to act as a linter and provide feedback on code quality without actually generating any output.</p>
<p>You can also configure the TypeScript compiler using a <code>tsconfig.json</code> file to specify various options, including the strictness of the checking. This can help catch even more potential issues and ensure that the code follows best practice</p>
<p>If you're not familiar with TypeScript, I recommend you <a target="_blank" href="https://www.freecodecamp.org/news/an-introduction-to-typescript/">this article I wrote a while ago</a>.</p>
<h1 id="heading-about-code-formatters">About Code Formatters</h1>
<p>In modern web development, code formatters have become an essential tool for developers. These tools automate the process of code formatting, making it easier to write and read code.</p>
<h2 id="heading-what-are-code-formatters">What are Code Formatters?</h2>
<p>Code formatters are automated tools that help you format source code automatically. The main purpose of code formatters is to standardize the formatting of code across a project or team, making it easier to read and understand code.</p>
<p>With code formatters, developers no longer need to spend time formatting code manually, which can save a lot of time and effort.</p>
<p>Code formatting tools have been around for decades. One of the earliest tools was the "indent" program, which was used to format C code in the early 1970s. But these early tools were limited and didn't have the same level of functionality as modern code formatters.</p>
<p>In the early 2000s, tools like "astyle" and "uncrustify" were developed, which introduced more advanced formatting capabilities.</p>
<h2 id="heading-why-are-code-formatters-useful">Why are Code Formatters Useful?</h2>
<p>Code formatters are useful for a variety of reasons. First and foremost, they help to standardize code formatting, which makes it easier to read and understand code. This is particularly important when working on large projects with multiple developers, where everyone needs to be able to read and understand each other's code.</p>
<p>Code formatters also help to ensure that code is consistent across a project or team, which can help to prevent errors and improve code quality. They also make it easier to maintain code over time, as the code is formatted consistently and is easier to read and understand.</p>
<h2 id="heading-main-code-formatters-available">Main Code Formatters Available</h2>
<p>There are several code formatting tools available in the market today. Here are some of the most popular ones:</p>
<ol>
<li><p><strong>Prettier:</strong> <a target="_blank" href="https://prettier.io/">Prettier</a> is a popular code formatter for JavaScript, TypeScript, and CSS. It's highly configurable and can be used in a variety of different environments, including editors, build tools, and code quality checkers.</p>
</li>
<li><p><strong>ESLint:</strong> While primarily known as a linting tool, <a target="_blank" href="https://eslint.org/">ESLint</a> can also be used as a code formatter. It has a <code>--fix</code> flag that can automatically format your code based on rules you define.</p>
</li>
<li><p><strong>Beautify:</strong> Beautify is a code formatter for JavaScript, HTML, and CSS that can be used in a variety of editors and IDEs. It allows you to customize your formatting options and has support for a wide range of languages.</p>
</li>
</ol>
<h1 id="heading-how-to-implement-eslint-and-prettier">How to Implement ESLint and Prettier</h1>
<p>Cool, so now let's see a linter and code formatter in action! We're going to implement the two most popular tools (ESLint and Prettier) in a simple React project to get an idea of how these things work.</p>
<p>First let's create our project by running this in our command line: <code>npm create vite@latest linternsAndFormatters --template react</code></p>
<p>Then <code>cd</code> into your project and run <code>npm install</code> so our dependencies get installed.</p>
<p>Now that we have our project up and running, we'll start by installing <strong>ESLint.</strong></p>
<h2 id="heading-how-to-install-eslint">How to Install ESLint</h2>
<p>To install ESLint, we can just run <code>npm init @eslint/config</code> in our console. This will fire a series of prompts asking how we want to use ESLint in our project and building the corresponding config. Your console might end up looking something like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/04/image-72.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Installing ESLint</em></p>
<p>After all this, we'll see a new file called <code>.eslintrc.cjs</code> in the root of our project. Here's where ESLint's config lives and where we can customize the linter to our preferences. The initial config given the options I selected is the following:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-string">"env"</span>: {
        <span class="hljs-string">"browser"</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">"es2021"</span>: <span class="hljs-literal">true</span>
    },
    <span class="hljs-string">"extends"</span>: [
        <span class="hljs-string">"eslint:recommended"</span>,
        <span class="hljs-string">"plugin:react/recommended"</span>
    ],
    <span class="hljs-string">"overrides"</span>: [
    ],
    <span class="hljs-string">"parserOptions"</span>: {
        <span class="hljs-string">"ecmaVersion"</span>: <span class="hljs-string">"latest"</span>,
        <span class="hljs-string">"sourceType"</span>: <span class="hljs-string">"module"</span>
    },
    <span class="hljs-string">"plugins"</span>: [
        <span class="hljs-string">"react"</span>
    ],
    <span class="hljs-string">"rules"</span>: {
    }
}
</code></pre>
<p>To see our linter in action, let's add the following script in our <code>package.json</code> file:</p>
<pre><code class="lang-javascript"><span class="hljs-string">"lint"</span>: <span class="hljs-string">"eslint --fix . --ext .js,.jsx"</span>
</code></pre>
<p>This script executes the <code>eslint</code> command with the <code>--fix</code> option to automatically fix linting errors and warnings. The command is executed on all files with the <code>.js</code> or <code>.jsx</code> extension located in the root directory of the project, as specified by the <code>.</code> argument.</p>
<p>Now let's modify our <code>app.jsx</code> file to have the following code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">const</span> emptyVariable = <span class="hljs-string">''</span>

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Vite + React<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App
</code></pre>
<p>Then run <code>npm run lint</code> and voilà! Your linter is screaming with red highlighted text that you have an unused variable in your code! =D</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/04/image-73.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-install-prettier">How to Install Prettier</h2>
<p>Cool, now let's hop on to our code formatter.</p>
<p>We're going to install it by running <code>npm install --save-dev --save-exact prettier</code>.</p>
<p>Then we're going to create and empty config file by running <code>echo {}&gt; .prettierrc.json</code>.</p>
<p>Since we're here, add the following options to your newly created config file:</p>
<pre><code class="lang-javascript">{
  <span class="hljs-string">"singleQuote"</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-string">"jsxSingleQuote"</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-string">"semi"</span>: <span class="hljs-literal">false</span>
}
</code></pre>
<p>What this does is assure single quotes are used whenever possible and semicolons are nowhere to be found (because, god, who likes semicolons...).</p>
<p>As we did with our linter, let's add the following script in our <code>package.json</code> file:</p>
<pre><code class="lang-javascript">    <span class="hljs-string">"format"</span>: <span class="hljs-string">"prettier --write ."</span>
</code></pre>
<p>The script runs the Prettier code formatter on all the files in the project directory and its sub-directories. When run with the <code>--write</code> option, it modifies the files in place, changing them to conform to Prettier's rules for indentation, line length, and other formatting options. The <code>.</code> argument specifies that all files in the project directory and its sub directories should be formatted.</p>
<p>Lastly, let's "uglify" the first line of our <code>app.jsx</code> file like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
</code></pre>
<p>Run <code>npm run format</code> and you should see it corrected right in front of you:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
</code></pre>
<p>You can breath easy now, those ugly semicolons won't come back to haunt you. ;)</p>
<p>As we've seen, the set up of these two tools isn't that complex, and they truly help to make our everyday jobs easier. ESLint will help us catch bugs and unnecessary/redundant code, and Prettier will help us standardize code format all across our codebase.</p>
<p>Another tip is that if you have a CI/CD pipeline in place, it's a good idea to implement the linting and formatting scripts as part of your workflow. This will help you ensure that every deployment is both automatically linted and formatted.</p>
<p>If you're not familiar with CI/CD or setting up a pipeline, I recently wrote <a target="_blank" href="https://www.freecodecamp.org/news/what-is-ci-cd/">an article about that</a>. ;)</p>
<h1 id="heading-wrapping-up">Wrapping up</h1>
<p>Linters and code formatters are powerful tools that can greatly benefit web developers.</p>
<p>Linters help you catch potential bugs and issues before they become serious problems, and encourage you to write more maintainable and readable code.</p>
<p>Code formatters help you enforce a consistent code style and format, saving time and reducing the chances of human error.</p>
<p>By using these tools in your web development workflow, you can improve your productivity and the quality of your code.</p>
<p>As always, I hope you enjoyed the article and learned something new.</p>
<p>If you want, you can also follow me on <a target="_blank" href="https://www.linkedin.com/in/germancocca/">LinkedIn</a> or <a target="_blank" href="https://twitter.com/CoccaGerman">Twitter</a>. See you in the next one!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/04/4M2n.gif" alt="Image" width="600" height="400" loading="lazy"></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Alternatives to Prettier – Popular Code Linting and Formatting Tools ]]>
                </title>
                <description>
                    <![CDATA[ Many programmers hate code formatting because it is tedious and time-consuming. You can spend hours making sure everything is perfect and well-indented.  This is why code formatters are so useful. A code formatter is a tool that formats code accordin... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/alternatives-to-prettier/</link>
                <guid isPermaLink="false">66d0395a386d35c4e3bb3c39</guid>
                
                    <category>
                        <![CDATA[ clean code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Prettier ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kamaldeen Lawal ]]>
                </dc:creator>
                <pubDate>Wed, 15 Mar 2023 17:43:50 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/Alternatives-to-Prettier.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Many programmers hate code formatting because it is tedious and time-consuming. You can spend hours making sure everything is perfect and well-indented. </p>
<p>This is why code formatters are so useful.</p>
<p>A code formatter is a tool that formats code according to certain standards. It makes it so you don't have to worry as much about code formatting. Instead, you can focus on writing good code. This save time and also reduces your stress.</p>
<p>In this guide, we will talk about the Prettier code formatter. We will also talk about alternatives to Prettier like JsFmt, StandardJS, EsLint + EditorConfig, and Beautifier. Hopefully you’ll take away something that you can use to improve your code formatting.</p>
<h2 id="heading-brief-overview-of-prettier">Brief Overview of Prettier</h2>
<p><a target="_blank" href="https://prettier.io/">Prettier</a> is a popular code formatter that can handle complex code structures and format your code in a readable way.</p>
<p>It operates on the principle of no configuration required and it's designed to produce readable, consistent code. </p>
<p>Prettier is an opinionated tool that encourages programmers to follow its formatting rules. It also parses the code and reprints it uniformly.</p>
<h2 id="heading-why-explore-alternatives-to-prettier">Why Explore Alternatives to Prettier?</h2>
<p>There are many advantages of using Prettier. But like every other tool, exploring alternatives is sometimes a good idea. Here are some of the reasons for exploring alternatives to Prettier:</p>
<ol>
<li><strong>Inflexible style guides</strong>: Prettier adopts a strict set of formatting rules, and does not allow for much customization. Some programmers want a tool with a lot of configurations available.</li>
<li><strong>Speed</strong>: Although Prettier is fast, you may need a tool that provides faster and better performance in large codebases or real-time formatting.</li>
<li><strong>Language support</strong>: Prettier may not support (or might offer inadequate support) for the language you use.</li>
<li><strong>More features</strong>: While Prettier is open source, you might prefer a paid code formatter that has more features and better integration with other tools.</li>
</ol>
<p>In the guide, we will talk about four alternatives to Prettier. They are:</p>
<ul>
<li>JsFmt</li>
<li>StandardJS</li>
<li>ESLint+EditorConfig</li>
<li>JS Beautifier</li>
</ul>
<h2 id="heading-jsfmt-overview">JsFmt Overview</h2>
<p><a target="_blank" href="https://rdio.github.io/jsfmt/">JsFmt</a> is a code formatter that uses esformatter as a formatting tool. It automatically reformats JavaScript code according to a set of predefined rules. It is available specifically to re-write, format, search, and validate JavaScript code.</p>
<p>To install JsFmt, run this command:</p>
<pre><code>npm install -g jsfmt
</code></pre><p>A .jsfmtrc file, which can either be a JSON or INI formatted file, can overwrite any of the esformatter formatting options.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"indent"</span>: <span class="hljs-number">4</span>,
  <span class="hljs-attr">"line_ending"</span>: <span class="hljs-string">"unix"</span>,
  <span class="hljs-attr">"quote"</span>: <span class="hljs-string">"single"</span>
}
</code></pre>
<p>In this example, we specified the spaces for indentation to be four, we chose line-ending to be unix, and we picked single as the quote style for strings.</p>
<p>You can use the jsfmt rewrite method to change values in your JavaScript code. Here's an example of how to do that:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> x = <span class="hljs-number">5</span>;
<span class="hljs-keyword">const</span> y = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> z = x + y;
<span class="hljs-built_in">console</span>.log(z);

<span class="hljs-comment">//  The output will change to the below code after writing:  npx jsfmt --rewrite '5 -&gt; 20' reduce.js</span>

<span class="hljs-keyword">const</span> x = <span class="hljs-number">20</span>;
<span class="hljs-keyword">const</span> y = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> z = x + y;
<span class="hljs-built_in">console</span>.log(z);
</code></pre>
<p>To learn more about JsFmt, you can read its documentation <a target="_blank" href="https://github.com/rdio/jsfmt">here</a>.</p>
<h3 id="heading-features-of-jsfmt">Features of JsFmt</h3>
<ol>
<li>Formating: JsFmt can format code according to a set of predefined rules.</li>
<li>Integrations: you can integrate JsFmt with popular code editors like Visual Studio Code, Sublime Text, and Atom, making it easy to format your code directly in your editor.</li>
<li>Command-line interface: JsFmt allows you to format your code as part of your build process or development workflow in the command line.</li>
</ol>
<h3 id="heading-jsfmt-formatting-rules">JsFmt Formatting Rules</h3>
<p>Some of the formatting rules adopted by JsFmt are as follows</p>
<ol>
<li>Set indent: With the <code>--indent</code> option, JsFmt indents your code with two spaces per level.</li>
<li>Quotes: In JsFmt, single quotes are the default for strings, but you can change this with the <code>--double-quote</code> option.</li>
<li>Wrapping: you can use <code>--linene-width</code> to change the default wrap lines setting in JsFmt.</li>
<li>Semicolons: JsFmt adds semicolons at the end of each statement by default. You can disable this using the <code>--no-semi</code> option.</li>
</ol>
<h3 id="heading-pros-of-jsfmt">Pros of JsFmt</h3>
<ol>
<li>Formatting, searching, and re-writing of JavaScript.</li>
<li>Customizable: JsFmt's flexibility makes it a popular choice for many development teams. It is a highly customizable tool that you can configure to match the specific requirements of a project.</li>
</ol>
<h3 id="heading-con-of-jsfmt">Con of JsFmt</h3>
<ol>
<li>Lack of flexibility: despite the fact that it's customizeable, certain parts of JsFmt may not always be configurable to match the needs of your project.</li>
</ol>
<h2 id="heading-standardjs-overview">StandardJS Overview</h2>
<p><a target="_blank" href="https://standardjs.com/">StandardJS</a> is an open-source, no configuration linter, formatter, and JavaScript style guide. It checks the code for probable errors with the linter. It also formats code automatically, and helps you write code that is easy to read and understand.</p>
<p>It is the most popular option, coming in at 27,905 stars on GitHub clean code linter. It is also a powerful tool for ensuring the quality and consistency of your JavaScript code.</p>
<p>You can either install it globally or locally on your machine. To install globally, use the first command, while the second command is for installing locally. Note that to install it, you must have Node and npm installed on your computer.</p>
<p>To install globally: <code>$ npm install standard --global</code></p>
<p>To install locally: <code>$ npm install standard --save-dev</code></p>
<p>Alternatively, you can add StandardJS to the package JSON.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"my-cool-package"</span>,
  <span class="hljs-attr">"devDependencies"</span>: {
    <span class="hljs-attr">"standard"</span>: <span class="hljs-string">"*"</span>
  },
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"test"</span>: <span class="hljs-string">"standard &amp;&amp; node my-tests.js"</span>
  }
}
</code></pre>
<p>The styles are checked automatically when you run the command:</p>
<pre><code>npm test
</code></pre><p>To illustrate how it works, check the JavaScript code below:</p>
<pre><code class="lang-javascript!">let number = 10;
if (number == 10) {
  alert("yes");
} else {
  alert("no");
}

// The Output after running the npm test in the terminal

&gt; test
&gt; standard &amp;&amp; node my-tests.js

standard: Use JavaScript Standard Style (https://standardjs.com)
  C:\Users\hp\Desktop\pretier\Js\script.js:5:5: Parsing error: Unexpected token else (null)
</code></pre>
<p>StandardJS shows that the code has a parsing error.</p>
<p>To fix most issues, run the following code:</p>
<pre><code>standard --fix
</code></pre><h3 id="heading-features-of-standardjs">Features of StandardJS</h3>
<ol>
<li>Community: A community of contributors guides the development of StandardJS by providing bug fixes and new features.</li>
<li>Linting: StandardJS linting catches style issues and programmer errors early.</li>
<li>Automatic code formatting: StandardJS get rid of messy and inconsistent code.</li>
<li>Command line integration: It has a command-line interface for running linter checks and auto-formatting code.</li>
</ol>
<h3 id="heading-standardjs-formatting-rules">StandardJS Formatting Rules</h3>
<p>Some of the widely used StandardJS formatting rules are listed below:</p>
<ol>
<li>Indentation: StandardJS enforces a consistent indentation level of 2 spaces by default. To strictly implement this, you need to add the <code>--fix</code> option.</li>
<li>Line wrap: StandardJS doesn't enforce the rule of line length, but it recommends keeping lines shorter than 80 characters. Use <code>$ myfile.js | standard --stdin</code> to manually check the file length.</li>
<li>Semicolons: StandardJS recommends using semicolons for clarity and to avoid potential issues. But the tool doesn't enforce this. To enforce the use of semicolons, use this:</li>
</ol>
<pre><code class="lang-json">{
  <span class="hljs-attr">"extends"</span>: <span class="hljs-string">"standard"</span>
}
</code></pre>
<ol>
<li>Spaces around operators: StandardJS recommends using spaces around operators, such as + and -, but it doesn't enforce this rule.</li>
</ol>
<h3 id="heading-pros-of-standardjs">Pros of StandardJS</h3>
<ol>
<li>Zero configuration: No decisions to make. StandardJS is the easiest way to enforce code quality in your project.</li>
<li>Easy installation: StandardJS is easy to install, and you can use it with most editors and IDEs.</li>
<li>Enhances Productivity: StandardJS saves developers time and increases productivity by formatting code according to its rules.</li>
<li>Wide support: Most editors and IDEs support StandardJS using plugins and integrations.</li>
<li>Consistency: StandardJS promote consistency across projects by enforcing a strict set of coding rules.</li>
</ol>
<h3 id="heading-cons-of-standardjs">Cons of StandardJS</h3>
<ol>
<li>Opinionated: Some developers may find the tool too opinionated. If that's the case, you might prefer to use other linting tools that allow for more customization.</li>
<li>Limited flexibility: StandardJS enforces a strict set of rules and conventions, which some developers may feel restricted by and prefer a more customizable approach.</li>
</ol>
<h2 id="heading-eslint-editconfig-overview">ESLint + EditConfig Overview</h2>
<p><a target="_blank" href="https://eslint.org/">ESlint</a> provides configurable rules that you can tailor according to your specific project needs. It is one of the most popular JavaScript linter that analyzes programming errors, bugs, and suspicious constructs.</p>
<p>You can extend its functionality by adding plugins, which can provide custom parsers and extra rules.</p>
<p>You'll need Node installed on your before installing ESLint. To install it, use the command below:</p>
<pre><code>npm init @eslint/config
</code></pre><p>Alternatively, you can create this manually on your machine:</p>
<pre><code>npm install --save-dev eslint
</code></pre><p>For both installations, you need to have installed a package.json file. If not, run this command</p>
<pre><code>npx eslint --init
</code></pre><p>The command will ask a series of questions about your project, coding style, and preferences to generate your configuration file.</p>
<p>You can run ESLint on any of your directories or files with this command:</p>
<pre><code>npx eslint yourfile.js
</code></pre><p>Rules can be turned off and you can run the toolonly with basic syntax validation, as ESLint is configurable and flexible to your use case.</p>
<p>Once you have generated the configuration file, you can customize according to your needs. The configuration file is a JavaScript file that exports an object with the configuration settings.</p>
<p>For example, you can specify the rules you want, plugins to use, and environments for your code:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"rules"</span>: {
    <span class="hljs-attr">"semi"</span>: [<span class="hljs-string">"error"</span>, <span class="hljs-string">"always"</span>],
    <span class="hljs-attr">"quotes"</span>: [<span class="hljs-string">"error"</span>, <span class="hljs-string">"double"</span>],
    <span class="hljs-attr">"indent"</span>: [<span class="hljs-string">"error"</span>, <span class="hljs-number">4</span>]
  }
}
</code></pre>
<p>The semi, quotes, and indent are the names of the rules in ESLint, while the error level of the rule is the first value. There are only three outputs for the first values. They are:</p>
<ul>
<li>"off" or 0 - turn the rule off</li>
<li>"warn" or 1 - turn the rule on as a warning (doesn’t affect exit code)</li>
<li>"error" or 2 - turn the rule on as an error (exit code will be 1).</li>
</ul>
<pre><code class="lang-json">{
  <span class="hljs-attr">"env"</span>: {
    <span class="hljs-attr">"browser"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">"es2021"</span>: <span class="hljs-literal">true</span>
  },
  <span class="hljs-attr">"extends"</span>: <span class="hljs-string">"eslint: recommended"</span>,
  <span class="hljs-attr">"overrides"</span>: [],
  <span class="hljs-attr">"parserOptions"</span>: {
    <span class="hljs-attr">"ecmaVersion"</span>: <span class="hljs-string">"latest"</span>
  },
  <span class="hljs-attr">"rules"</span>: {
    <span class="hljs-attr">"indent"</span>: [<span class="hljs-string">"error"</span>, <span class="hljs-number">4</span>],
    <span class="hljs-attr">"quotes"</span>: [<span class="hljs-string">"error"</span>, <span class="hljs-string">"single"</span>],
    <span class="hljs-attr">"semi"</span>: [<span class="hljs-string">"error"</span>, <span class="hljs-string">"never"</span>]
  }
}
</code></pre>
<p>In this example, you specify that your code should run in a browser environment. Enable ECMAScript 2018 syntax, and follow the ESLint rules. You've also enabled three specific rules: indent, quotes, and semi.</p>
<p>The indent rule determines that an alert error will be thrown if indentation is more than 4 spaces. Likewise if you use double-quotes instead of single, there will be an error. You also specified not to use semi-colons.</p>
<p>To fix most of the issues in ESLint, use this command:</p>
<pre><code>npx eslint yourfile.js --fix
</code></pre><p>Here's an example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">2</span>, <span class="hljs-number">6</span>, <span class="hljs-number">78</span>, <span class="hljs-number">8</span>];
<span class="hljs-keyword">let</span> peoples = [<span class="hljs-string">"shola"</span>, <span class="hljs-string">"kamal"</span>];
<span class="hljs-built_in">console</span>.log(peoples);

<span class="hljs-comment">// The output</span>

 error  <span class="hljs-string">'numbers'</span> is assigned a value but never used  no-unused-vars
   error  Extra semicolon                               semi

✖ <span class="hljs-number">2</span> problems (<span class="hljs-number">2</span> errors, <span class="hljs-number">0</span> warnings)
  <span class="hljs-number">1</span> error and <span class="hljs-number">0</span> warnings potentially fixable <span class="hljs-keyword">with</span> the <span class="hljs-string">`--fix`</span> option.
</code></pre>
<h3 id="heading-editorconfig-overview">EditorConfig Overview</h3>
<p><a target="_blank" href="https://editorconfig.org/#file-format-details">EditorConfig</a> is an open-source file format. It provides standard ways of defining and maintaining coding styles in a project.</p>
<p>It maintains consistent coding styles which is especially useful when you have many developers working on the same project across various editors and IDEs. The tool consists of a collection of text editor plugins that enable editors to read the file format and adhere to defined styles.</p>
<p>You'll need to create a file named .editorconfig in the directory. EditorConfig plugins will look for the file in the directory of the opened file and on every parent directory.</p>
<p>Files are read from top to bottom, and a search for .editorconfig files will stop if the root file path is reached.</p>
<p>Creating an EditorConfig file is straightforward. Open a new file, and save it with .editorconfig as shown below.</p>
<p><img src="https://i.imgur.com/SHEafBl.png" alt="editorconfig file" width="600" height="400" loading="lazy"></p>
<p>Inside the .editorconfig file, the following wildcard patterns are used:</p>
<pre><code class="lang-javascript">root = <span class="hljs-literal">true</span>

[*]

indent_size = <span class="hljs-number">4</span>
indent_style = space
end_of_line = lf
insert_final_newline = <span class="hljs-literal">true</span>

 <span class="hljs-comment">// Set root to true: A search for .editorconfig files will stop if the root file path is reached or an EditorConfig file with root=true is found.</span>

 <span class="hljs-comment">//[*]: Except for path selectors, it matches any string of characters.</span>

 <span class="hljs-comment">//indent_size: Indent size is set to 4. </span>
 <span class="hljs-comment">//Indent_style: set to space</span>
 <span class="hljs-comment">//end_of_line: Set to lf</span>
<span class="hljs-comment">//insert_final_newline: Set to true</span>
</code></pre>
<p>To know more about the wildcards pattern, visit the <a target="_blank" href="https://editorconfig.org/#file-format-details">EditorConfig docs</a>.</p>
<p>Using ESLint and EditorConfig together can help to spot potential issues early. It also ensures that your code is consistent, maintainable, and of high quality.</p>
<p>The combination of ESLint and EDitorConfig saves you time on code maintenance and debugging.</p>
<h3 id="heading-features-of-eslint">Features of ESLint</h3>
<ol>
<li>Configurable: ESLint allows you to customize and add your own rules to the configuration file.</li>
<li>Support ECMAScript: ESLint ensures your code is compatible with the latest language features.</li>
<li>Supports many file formats: ESLint can analyze JavaScript code in a variety of file formats, including but not limited to .js, .jsx, and .vue.</li>
<li>Integration: ESLint integrates seamlessly with Webpack and Gulp, which are popular build tools.</li>
</ol>
<h3 id="heading-eslint-formatting-rules">ESLint Formatting Rules</h3>
<p>Some of the popular formatting rules in ESLint are listed below:</p>
<ol>
<li>Indentation: ESLint by default expects a four-space per level indent. But you can customize this using the popular <code>--fix</code> option.</li>
<li>Semicolons: ESLint enforces the use of semicolons at the end of each statement. This rule is set to "always" by default. Use the popular <code>--fix</code> option to reset it to "never".</li>
<li>Line wrap: Use the <code>max-len</code> rule to change the maximum line length from the default 80 to your preferred  value.</li>
<li>Quotes: The quotes rule can be changed from the default value of "single" to "double" using the <code>--fix</code> option.</li>
</ol>
<h3 id="heading-pros-of-eslint">Pros of ESLint</h3>
<ol>
<li>Saves time: ESLint saves time by reducing the amount of time spent on manual code reviews and debugging.</li>
<li>Active Community: ESLint has a wide and active community base that contributes to the development of the tool and its ecosystem.</li>
<li>Code analyzer: ESLint analyze your code to quickly find problems.</li>
<li>Consistency: ESLint helps in maintaining clean and readable code by enforcing a consistent coding style.</li>
</ol>
<h3 id="heading-cons-of-eslint">Cons of ESLint</h3>
<ol>
<li>Rule conflicts: With a large set of rules, ESLint rules can sometimes conflict with one another, resulting in more configuration and fine-tuning.</li>
<li>Steeper learning curve: ESLint has a pretty steep learning curve, as it has many rules and learning how to configure them can take time.</li>
<li>False positives: ESLint can sometimes flag code as problematic code even when it is correct, which can be frustrating for developers.</li>
</ol>
<h2 id="heading-overview-of-js-beautifier">Overview of JS Beautifier</h2>
<p><a target="_blank" href="https://beautifier.io/">JavaScript Beautifier</a>, also known as js-beautify, is an open-source, cross-platform command-line tool. It is a beautifier for re-formating and re-indenting bookmarklets, ugly JavaScript, and it partly deobfuscates scripts.</p>
<p>You can install JS beautifier both locally and globally.</p>
<p>Run <code>npm -g install js-beautify</code> to install globally, and <code>npm install js-beautify</code> to install locally.</p>
<p>You can configure it through the command line option.</p>
<p>After local installation, you can import and call the appropriate beautifier methods for JS, CSS, or HTML. An example of how to call the methods goes like this: beautify (<code>code</code>, <code>options</code>).</p>
<p>While <code>code</code> is the string of code to be beautified, <code>options</code> is an object with the settings you would like used to beautify the code. See the example below for a JavaScript script file:</p>
<pre><code>npx beautify(<span class="hljs-string">"script.js"</span>,{<span class="hljs-attr">indent_size</span>:<span class="hljs-number">4</span>})
</code></pre><p>You can also configure it through a configuration file:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"indent_size"</span>: <span class="hljs-number">4</span>,
  <span class="hljs-attr">"indent_char"</span>: <span class="hljs-string">" "</span>,
  <span class="hljs-attr">"indent_with_tabs"</span>: <span class="hljs-literal">false</span>,
  <span class="hljs-attr">"brace_style"</span>: <span class="hljs-string">"collapse"</span>
}
</code></pre>
<p>Create a file <code>.jsbeautifyrc</code> in the root directory of your project. This option will override the default option of JS beautifier. You can also use Beautifier on your web browser. Learn more about <a target="_blank" href="https://www.npmjs.com/package/js-beautify">JSbeautifer from its docs here</a>.</p>
<h3 id="heading-feature-of-js-beautifier">Feature of JS Beautifier</h3>
<ol>
<li>Formatting: You can customize JS Beautifier to fit your personal preferences and coding style because it offers varieties of formatting options, such as indentation size and braces style.</li>
<li>Online version: You can beautify JavaScript using JS Beautifier in your web browser, just as you can on the command line.</li>
</ol>
<h3 id="heading-js-beautifier-formatting-rules">JS Beautifier Formatting Rules</h3>
<ol>
<li>Indentation: The default identation in JS Beautifier is 4 spaces, but you can change it to 2 or 8 spaces, or use tabs instead. Use the <code>indent_size</code> option to specify the number of spaces to use for indentation.</li>
<li>Semicolons: You can change your semicolon settings in JS Beautifier with the <code>semicolon</code> option. You change this by alternating the value of the semicolon option between "true" or "false".</li>
<li>Line wrap: Use the <code>max_char</code> option to limit each line to 80 characters. You can set the value of the max_char to 80 or any other preferred  number.</li>
</ol>
<h3 id="heading-pros-of-js-beautifier">Pros of JS Beautifier</h3>
<ol>
<li>Time-saving: JS Beautifier saves time. It automates the many steps of formatting and organizing code.</li>
<li>Online presence: JS Beautifier's online presence is a game changer, as a lot of non-developers will be able to use it.</li>
<li>Detects errors: JS Beautifier helps catch small errors that can be difficult to spot otherwise.</li>
<li>Integration: JS Beautifier's compatibility with varieties of code editors and IDEs makes it a versatile tool.</li>
</ol>
<h3 id="heading-cons-of-js-beautifier">Cons of JS Beautifier</h3>
<ol>
<li>Complexity: Despite having an online version, JS Beautifier is somewhat complex for beginners.</li>
<li>Potential for over-formatting: In some cases, JS Beautifier may make the code harder to read because of over-formatting.</li>
</ol>
<h2 id="heading-results-from-all-the-formatting-tools">Results from all the Formatting Tools</h2>
<p>We'll use the code sample below to test how these four popular tools format code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">8</span>,<span class="hljs-number">9</span>,<span class="hljs-number">7</span>]

          <span class="hljs-keyword">let</span> peoples = [<span class="hljs-string">'kamal'</span>, <span class="hljs-string">'lawal'</span>, <span class="hljs-string">"shola"</span>, <span class="hljs-string">"olaide"</span>];
</code></pre>
<p>As you can see, this code isn't properly formatted. So let's try the tools to see how they help.</p>
<h3 id="heading-formatting-example-with-jsfmt">Formatting example with JsFmt</h3>
<p>Using the above code as a sample, the command <code>npx jsfmt --write "Your file"</code> will format the code to this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">7</span>]

<span class="hljs-keyword">let</span> peoples = [<span class="hljs-string">'kamal'</span>, <span class="hljs-string">'lawal'</span>, <span class="hljs-string">"shola"</span>, <span class="hljs-string">"olaide"</span>];
</code></pre>
<p>As you can see, there is a noticeable change in the code, and you can configure this to your desired taste.</p>
<h3 id="heading-formatting-example-with-standardjs">Formatting example with StandardJS</h3>
<p>By running the popular <code>npx standard--fix</code> command from the terminal, you will have the following result.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">7</span>]

<span class="hljs-keyword">const</span> peoples = [<span class="hljs-string">'kamal'</span>, <span class="hljs-string">'lawal'</span>, <span class="hljs-string">'shola'</span>, <span class="hljs-string">'olaide'</span>]
</code></pre>
<p>Notice the code was properly formatted, most especially the elements with double quotes within the <code>peoples</code> array.</p>
<h3 id="heading-formatting-example-with-eslint">Formatting example with ESLint</h3>
<p>Using the same code as above, the <code>npx eslint "Your file name --fix"</code> command will quickly format to this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">8</span>,<span class="hljs-number">9</span>,<span class="hljs-number">7</span>]

<span class="hljs-keyword">let</span> peoples = [<span class="hljs-string">'kamal'</span>,<span class="hljs-string">'lawal'</span>,<span class="hljs-string">'shola'</span>,<span class="hljs-string">'olaide'</span>]
</code></pre>
<p>You can also change the configuration of the tool, by changing the elements of the <code>peoples</code> array so they're inside double quotes:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">8</span>,<span class="hljs-number">9</span>,<span class="hljs-number">7</span>]

<span class="hljs-keyword">let</span> peoples = [<span class="hljs-string">"kamal"</span>,<span class="hljs-string">"lawal"</span>,<span class="hljs-string">"shola"</span>,<span class="hljs-string">"olaide"</span>];
</code></pre>
<p>You can do this by going to the .eslintrc file and manipulating the rules like this:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"quotes"</span>: [<span class="hljs-string">"error"</span>, <span class="hljs-string">"double"</span>],
  <span class="hljs-attr">"semi"</span>: [<span class="hljs-string">"off"</span>, <span class="hljs-string">"always"</span>]
}
</code></pre>
<h3 id="heading-formatting-example-with-js-beautifier">Formatting example with JS Beautifier</h3>
<p>Here's the result from JS Beautifier for the same code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">7</span>]
<span class="hljs-keyword">let</span> peoples = [<span class="hljs-string">'kamal'</span>, <span class="hljs-string">'lawal'</span>, <span class="hljs-string">"shola"</span>, <span class="hljs-string">"olaide"</span>];
</code></pre>
<p>You can configure it more from your JSON file if you wish.</p>
<p>Now you've seen these formatters in action!</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this guide, we talked about some alternatives to using Prettier. We discussed Jsfmt, ESlint, StandardJS, and JS Beautifier, as well as their features, pros, and cons.</p>
<p>I hope you are now more equipped to choose the right linter/formatter for your coding projects.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Add ESLint to an Angular Application ]]>
                </title>
                <description>
                    <![CDATA[ By Rodrigo Kamada In this article, we'll build a web application using the latest version of Angular. Then we'll add ESLint which analyzes the JavaScript code statically to find any problems. Prerequisites Before you start, you need to install and co... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-add-eslint-to-an-angular-application/</link>
                <guid isPermaLink="false">66d460c9c7632f8bfbf1e495</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ TypeScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 15 Nov 2022 16:39:58 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/10/angular-eslint-cover-hashnode.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Rodrigo Kamada</p>
<p>In this article, we'll build a web application using the latest version of <a target="_blank" href="https://angular.io/">Angular</a>. Then we'll add <a target="_blank" href="https://eslint.org/">ESLint</a> which analyzes the JavaScript code statically to find any problems.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before you start, you need to install and configure the tools below (if you don't already have them installed) to create the Angular application:</p>
<ul>
<li><a target="_blank" href="https://git-scm.com/">Git</a>: Git is a distributed version control system. We'll use it to sync the repository.</li>
<li><a target="_blank" href="https://nodejs.org/">Node.js and npm</a>: Node.js is a JavaScript code runtime software based on Google's V8 engine. npm is a package manager for Node.js (Node Package Manager). We'll use them to build and run the Angular application and install the libraries.</li>
<li><a target="_blank" href="https://angular.io/cli">Angular CLI</a>: Angular CLI is a command line utility tool for Angular. We'll use it to create the base structure of the Angular application.</li>
<li>IDE (for example <a target="_blank" href="https://code.visualstudio.com/">Visual Studio Code</a> or <a target="_blank" href="https://www.jetbrains.com/webstorm/">WebStorm</a>): an Integrated Development Environment is a tool with a graphical interface to help in the development of applications. We'll use it to develop the Angular application.</li>
</ul>
<h2 id="heading-getting-started">Getting Started</h2>
<h3 id="heading-create-the-angular-application">Create the Angular application</h3>
<p>Angular is a development platform for building web, mobile and desktop applications using HTML, CSS, and TypeScript (JavaScript). Currently, Angular is at version 14 and Google is the main maintainer of the project.</p>
<p>First, let's create the application with the Angular base structure using the <code>@angular/cli</code> with the route file and the SCSS style format.</p>
<pre><code>ng <span class="hljs-keyword">new</span> angular-eslint --routing <span class="hljs-literal">true</span> --style scss
CREATE angular-eslint/README.md (<span class="hljs-number">1067</span> bytes)
CREATE angular-eslint/.editorconfig (<span class="hljs-number">274</span> bytes)
CREATE angular-eslint/.gitignore (<span class="hljs-number">548</span> bytes)
CREATE angular-eslint/angular.json (<span class="hljs-number">3136</span> bytes)
CREATE angular-eslint/package.json (<span class="hljs-number">1045</span> bytes)
CREATE angular-eslint/tsconfig.json (<span class="hljs-number">863</span> bytes)
CREATE angular-eslint/.browserslistrc (<span class="hljs-number">600</span> bytes)
CREATE angular-eslint/karma.conf.js (<span class="hljs-number">1431</span> bytes)
CREATE angular-eslint/tsconfig.app.json (<span class="hljs-number">287</span> bytes)
CREATE angular-eslint/tsconfig.spec.json (<span class="hljs-number">333</span> bytes)
CREATE angular-eslint/.vscode/extensions.json (<span class="hljs-number">130</span> bytes)
CREATE angular-eslint/.vscode/launch.json (<span class="hljs-number">474</span> bytes)
CREATE angular-eslint/.vscode/tasks.json (<span class="hljs-number">938</span> bytes)
CREATE angular-eslint/src/favicon.ico (<span class="hljs-number">948</span> bytes)
CREATE angular-eslint/src/index.html (<span class="hljs-number">299</span> bytes)
CREATE angular-eslint/src/main.ts (<span class="hljs-number">372</span> bytes)
CREATE angular-eslint/src/polyfills.ts (<span class="hljs-number">2338</span> bytes)
CREATE angular-eslint/src/styles.scss (<span class="hljs-number">80</span> bytes)
CREATE angular-eslint/src/test.ts (<span class="hljs-number">749</span> bytes)
CREATE angular-eslint/src/assets/.gitkeep (<span class="hljs-number">0</span> bytes)
CREATE angular-eslint/src/environments/environment.prod.ts (<span class="hljs-number">51</span> bytes)
CREATE angular-eslint/src/environments/environment.ts (<span class="hljs-number">658</span> bytes)
CREATE angular-eslint/src/app/app-routing.module.ts (<span class="hljs-number">245</span> bytes)
CREATE angular-eslint/src/app/app.module.ts (<span class="hljs-number">393</span> bytes)
CREATE angular-eslint/src/app/app.component.scss (<span class="hljs-number">0</span> bytes)
CREATE angular-eslint/src/app/app.component.html (<span class="hljs-number">23364</span> bytes)
CREATE angular-eslint/src/app/app.component.spec.ts (<span class="hljs-number">1097</span> bytes)
CREATE angular-eslint/src/app/app.component.ts (<span class="hljs-number">219</span> bytes)
✔ Packages installed successfully.
    Successfully initialized git.
</code></pre><p>Now we will install the libraries and add the ESLint settings.</p>
<pre><code>ng add @angular-eslint/schematics
ℹ Using package manager: npm
✔ Found compatible package version: @angular-eslint/schematics@<span class="hljs-number">14.2</span><span class="hljs-number">.5</span>.
✔ Package information loaded.

The package @angular-eslint/schematics@<span class="hljs-number">14.2</span><span class="hljs-number">.5</span> will be installed and executed.
Would you like to proceed? Yes
✔ Packages successfully installed.

    All @angular-eslint dependencies have been successfully installed 🎉

    Please see https:<span class="hljs-comment">//github.com/angular-eslint/angular-eslint for how to add ESLint configuration to your project.</span>

    We detected that you have a single project <span class="hljs-keyword">in</span> your workspace and no existing linter wired up, so we are configuring ESLint <span class="hljs-keyword">for</span> you automatically.

    Please see https:<span class="hljs-comment">//github.com/angular-eslint/angular-eslint for more information.</span>

CREATE .eslintrc.json (<span class="hljs-number">984</span> bytes)
UPDATE package.json (<span class="hljs-number">1511</span> bytes)
UPDATE angular.json (<span class="hljs-number">3447</span> bytes)
✔ Packages installed successfully.
</code></pre><p>Next, we will run the command below to validate the ESLint installation and configuration:</p>
<pre><code>npm run lint

&gt; angular-eslint@<span class="hljs-number">1.0</span><span class="hljs-number">.0</span> lint /home/rodrigokamada/angular-eslint
&gt; ng lint


Linting <span class="hljs-string">"angular-eslint"</span>...

All files pass linting.
</code></pre><p>And you're all set! The message "<em>All files pass linting</em>" shows that no problems were found.</p>
<p>The application repository is <a target="_blank" href="https://github.com/rodrigokamada/angular-eslint.">available here</a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This is what we covered in this article:</p>
<ul>
<li>We created an Angular application.</li>
<li>We added ESLint to analyze and find problems with the code.</li>
</ul>
<p>You can use this to check your application code before deploying to your environment.</p>
<p>Thank you for reading and I hope you enjoyed the article!</p>
<p>This tutorial was posted on my <a target="_blank" href="https://rodrigo.kamada.com.br/blog/adicionando-o-eslint-em-uma-aplicacao-angular">blog</a> in Portuguese.</p>
<p>To stay updated whenever I post new articles, follow me on <a target="_blank" href="https://twitter.com/rodrigokamada">Twitter</a> and <a target="_blank" href="https://www.linkedin.com/in/rodrigokamada">LinkedIn</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use GitHub Super Linter in Your Projects ]]>
                </title>
                <description>
                    <![CDATA[ By Rishit Dagli When you're starting a new project, you might have to add multiple linting tools to beautify your code and prevent simple errors.  You will often use multiple linters – one of them might support an npm installation and other one might... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/github-super-linter/</link>
                <guid isPermaLink="false">66d460c5f855545810e934cd</guid>
                
                    <category>
                        <![CDATA[ clean code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ GitHub ]]>
                    </category>
                
                    <category>
                        <![CDATA[ GitHub Actions ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 30 Aug 2022 15:25:35 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/08/FCC-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Rishit Dagli</p>
<p>When you're starting a new project, you might have to add multiple linting tools to beautify your code and prevent simple errors. </p>
<p>You will often use multiple linters – one of them might support an npm installation and other one might have a PyPI installation and so on. You will also want to set up some automation in your CI to run these linters, but this process is quite tedious 😫.</p>
<p>In this article, I will show you how you can use GitHub Super Linter, a single linter to solve all these problems. Most of my personal projects use GitHub Super Linter as well, and I have personally found it be a huge lifesaver.</p>
<h2 id="heading-why-is-linting-necessary">Why is Linting Necessary?</h2>
<p>Linting is essentially a form of static code analysis. It analyzes the code you wrote against some rules for stylistic or programmatic errors. Think of it as a tool that flags suspicious usage in software.</p>
<p>A linter can help you save a lot of time by:</p>
<ul>
<li>Preventing broken code from being pushed</li>
<li>Helping establish coding best practices</li>
<li>Building guidelines for code layout and format</li>
<li>Helping code reviews be a lot smoother</li>
<li>Flagging bugs in your code from syntax errors</li>
</ul>
<p>Given the useful nature of linting tools, you would ideally want to run a linter before any code reviews happen on every single piece of code that is pushed to your repository. This definitely helps you write better, more readable, and more stable code.</p>
<p>Here is an example of using <a target="_blank" href="https://github.com/psf/black">Black</a>, a linting tool for Python focusing on code formatting.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/08/Black-Example.png" alt="Image" width="600" height="400" loading="lazy">
<em>Formatting changes made by Black</em></p>
<p>GitHub Super Linter can help you quite a lot in bringing these capabilities to your projects easily and efficiently. GitHub Super Linter is a combination of multiple commonly used linters which you can use very easily. It lets you set up automated runs for these linters, as well as manage multiple linters in a single project!</p>
<p>There is also a ton of customization capabilities with environment variables that can help you customize the Super Linter to your individual repository.</p>
<h2 id="heading-how-to-use-github-super-linter-in-github-actions">How to Use GitHub Super Linter in GitHub Actions</h2>
<p>Super Linter is primarily designed to be run inside a GitHub Action, which is also how I have been using it for quite some time. We will talk about this first. To follow along you should create a new GitHub Action in your repository. Let us create a new file at <code>.github/workflows/linter.yml</code>.</p>
<p>Going forward, I will assume you know about the basic syntax for GitHub Actions. But in case you do not or need a quick refresher, I would suggest going through this <a target="_blank" href="https://docs.github.com/en/actions/quickstart">Quick Start Guide</a>.</p>
<h3 id="heading-how-to-create-an-action">How to Create an Action</h3>
<p>We already have a blank file <code>.github/workflows/linter.yml</code>, which we will now populate with an action you can use to lint your project.</p>
<p>We will start by giving our action a name. This is what appears under the GitHub Action Status Check:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>
</code></pre>
<p>Next up, let's specify triggers for our action. This answers the question of when you should lint your codebase. Here we tell it to run the lint on every push and on every pull request.</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>

<span class="hljs-attr">on:</span> [<span class="hljs-string">push</span>, <span class="hljs-string">pull_request</span>]
</code></pre>
<p>This is another very commonly used configuration for the triggers. This only runs when you make a pull request to <code>main</code> or <code>master</code> branches and not on pushes to these branches.</p>
<pre><code class="lang-yaml"><span class="hljs-attr">on:</span>
  <span class="hljs-attr">push:</span>
    <span class="hljs-attr">branches-ignore:</span> [<span class="hljs-string">master</span>, <span class="hljs-string">main</span>]
  <span class="hljs-attr">pull_request:</span>
    <span class="hljs-attr">branches:</span> [<span class="hljs-string">master</span>, <span class="hljs-string">main</span>]
</code></pre>
<p>Next up, we want to set up a job. All the components you put in a single job will run sequentially. Here, think of it as the steps and in which order we want them to run whenever the trigger is satisfied. </p>
<p>We will name this job "Lint Code Base" and ask GitHub to run our job on a runner with the last version of Ubuntu supported by GitHub.</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>

<span class="hljs-attr">on:</span> [<span class="hljs-string">push</span>, <span class="hljs-string">pull_request</span>]

<span class="hljs-attr">jobs:</span>
  <span class="hljs-attr">build:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
</code></pre>
<p>You are not bound to using a single kind of runner (ubuntu-latest), like we do here. It's a common practice to have a matrix of agent kinds, but in this case it will run the same way on all kinds of runners. You often you use a matrix of runners to test that your code works well on all kinds of platforms. </p>
<p>GitHub Super Linter doesn't work any differently on other machine types so we just use a single machine type.</p>
<p>Next up, we will start defining the steps we want this workflow to have. We essentially have two steps:</p>
<ol>
<li>Checkout the code</li>
<li>Run the super linter</li>
</ol>
<p>On to checking out the code. To do this, we will use the official checkout action by GitHub. </p>
<p>We will set <code>fetch-depth: 0</code> to fetch all history for all branches and tags which is required for Super linter to get a proper list of changed files. If you hadn't done this, only a single commit would be fetched.  </p>
<p>We also give our step a name and tell it we want to use the action present in the GitHub repository at <code>actions/checkout@v3</code> .</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>

<span class="hljs-attr">on:</span> [<span class="hljs-string">push</span>, <span class="hljs-string">pull_request</span>]

<span class="hljs-attr">jobs:</span>
  <span class="hljs-attr">build:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>

    <span class="hljs-attr">steps:</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Checkout</span> <span class="hljs-string">Code</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v3</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">fetch-depth:</span> <span class="hljs-number">0</span>
</code></pre>
<p>This piece of code checks out your repository under <code>$GITHUB_WORKSPACE</code> which allows the rest of the workflow to access this repository. The repository we are checking out is the one where your code resides, ideally the same repository.</p>
<h3 id="heading-how-to-run-the-linter">How to Run the Linter</h3>
<p>Now we'll add the step to run the linter since we have our code checked out. You can customize GitHub Super Linter using environment variables when running the action.</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>

<span class="hljs-attr">on:</span> [<span class="hljs-string">push</span>, <span class="hljs-string">pull_request</span>]

<span class="hljs-attr">jobs:</span>
  <span class="hljs-attr">build:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>

    <span class="hljs-attr">steps:</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Checkout</span> <span class="hljs-string">Code</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v3</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">fetch-depth:</span> <span class="hljs-number">0</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">github/super-linter@v4</span>
</code></pre>
<p>We will now talk about the environment variables which you will often be using with GitHub Super Linter as well as some examples.</p>
<ul>
<li><code>VALIDATE_ALL_CODEBASE</code>: this decides whether Super Linter should lint the whole codebase or just the changes introduced with that commit. These changes are found out using <code>git diff</code>, but you can also change the search algorithm (but we will not be looking into this in this article). Example: <code>VALIDATE_ALL_CODEBASE: true</code>.</li>
<li><code>GITHUB_TOKEN</code>: As the name suggests, this is the value of the GitHub token. If you use this, GitHub will show up each of the linters you use (we will see how to do that soon) as separate checks on the UI. Example: In GitHub Actions you can use <code>GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}</code>.</li>
<li><code>DEFAULT_BRANCH</code>: The name of the repository default branch. Example: <code>DEFAULT_BRANCH: main</code>.</li>
<li><code>IGNORE_GENERATED_FILES</code>: In case you have any files which are generated by tools, you could mark them as <code>@generated</code>. If this environment variable is set to true, Super Linter ignores these files. Example: <code>IGNORE_GENERATED_FILES: true</code>.</li>
<li><code>IGNORE_GITIGNORED_FILES</code>: Excludes the files which are in .gitignore from linting. Example: <code>IGNORE_GITIGNORED_FILES: true</code>.</li>
<li><code>LINTER_RULES_PATH</code>: A custom path where any linter customization files should be. By default your files are expected to be at <code>.github/linters/</code>. Example: <code>LINTER_RULES_PATH: /</code>.</li>
</ul>
<p>These are some of the environment variables you will use the most often, but none of the ones we've discussed yet talk about language-specific linting. </p>
<p>If you do not use any of the environment variables we talk about, Super Linter automatically finds and uses all the applicable linters for your codebase.</p>
<h2 id="heading-how-to-add-specific-linters-to-super-linter">How to Add Specific Linters to Super Linter</h2>
<p>You will often only be interested in using specific linters for your projects. You can use the following environment variable pattern to add any linters you want:</p>
<pre><code>VALIDATE_{LANGUAGE}_{LINTER}
</code></pre><p>You can find the naming conventions for these in the list of <a target="_blank" href="https://github.com/github/super-linter#supported-linters">Supported Linters</a>.</p>
<p>Here are a couple of examples, where we specify we want to use Black to lint all Python files, ESLint for JavaScript files, and HTMLHint for HTML files.</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>

<span class="hljs-attr">on:</span> [<span class="hljs-string">push</span>, <span class="hljs-string">pull_request</span>]

<span class="hljs-attr">jobs:</span>
  <span class="hljs-attr">build:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>

    <span class="hljs-attr">steps:</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Checkout</span> <span class="hljs-string">Code</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v3</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">fetch-depth:</span> <span class="hljs-number">0</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">github/super-linter@v4</span>
        <span class="hljs-attr">env:</span>
          <span class="hljs-attr">VALIDATE_ALL_CODEBASE:</span> <span class="hljs-literal">true</span>
          <span class="hljs-attr">VALIDATE_JAVASCRIPT_ES:</span> <span class="hljs-literal">true</span>
          <span class="hljs-attr">VALIDATE_PYTHON_BLACK:</span> <span class="hljs-literal">true</span>
          <span class="hljs-attr">VALIDATE_HTML:</span> <span class="hljs-literal">true</span>
          <span class="hljs-attr">GITHUB_TOKEN:</span> <span class="hljs-string">${{</span> <span class="hljs-string">secrets.GITHUB_TOKEN</span> <span class="hljs-string">}}</span>
</code></pre>
<p>Once you set one of the linters to <code>true</code>, all the other linters will not run. In the above snippet, none of the linters except ESLint, Black, or HTMLHint will run.</p>
<p>However, in this example, we set a single linter to <code>false</code> so every linter except ESLint will run here:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>

<span class="hljs-attr">on:</span> [<span class="hljs-string">push</span>, <span class="hljs-string">pull_request</span>]

<span class="hljs-attr">jobs:</span>
  <span class="hljs-attr">build:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>

    <span class="hljs-attr">steps:</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Checkout</span> <span class="hljs-string">Code</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v3</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">fetch-depth:</span> <span class="hljs-number">0</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Code</span> <span class="hljs-string">Base</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">github/super-linter@v4</span>
        <span class="hljs-attr">env:</span>
          <span class="hljs-attr">VALIDATE_ALL_CODEBASE:</span> <span class="hljs-literal">true</span>
          <span class="hljs-attr">VALIDATE_JAVASCRIPT_ES:</span> <span class="hljs-literal">false</span>
          <span class="hljs-attr">GITHUB_TOKEN:</span> <span class="hljs-string">${{</span> <span class="hljs-string">secrets.GITHUB_TOKEN</span> <span class="hljs-string">}}</span>
</code></pre>
<h2 id="heading-how-to-customize-lint-checks">How to Customize Lint Checks</h2>
<p>Linters often use configuration files so you can modify the rules the linter uses. In the above two full length examples I showed, Super Linter will try to find any configuration files under <code>.github/linters/</code>.</p>
<p>These could be your <code>.eslintrc.yml</code> file used to configure ESLint, <code>.htmlhintrc</code> for configuring HTMLHint, and so on.</p>
<p>Here is an example of the configuration file if you use the Flake8 linter for Python:</p>
<pre><code>[flake8]
max-line-length = <span class="hljs-number">120</span>
</code></pre><p>You save this at <code>.github/linters/.flake8</code>. You'll then use it while running the Flake8 linter. You can find an example of template configuration files you can use <a target="_blank" href="https://github.com/github/super-linter/tree/main/TEMPLATES">here</a>. </p>
<p>However, here are two examples of how you can modify this path:</p>
<ol>
<li>All your linter configuration files are in some other directory</li>
</ol>
<p>Add the directory path as an environment variable like this:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">LINTER_RULES_PATH:</span> <span class="hljs-string">configs/</span>
</code></pre>
<ol start="2">
<li>Add path for a configuration file</li>
</ol>
<p>You can also hardcode a path for a specific linter as an environment variable. Here is an example:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">JAVASCRIPT_ES_CONFIG_FILE:</span> <span class="hljs-string">configs/linters/.eslintrc.yml</span>
</code></pre>
<h2 id="heading-how-to-run-super-linter-outside-github-actions">How to Run Super Linter Outside GitHub Actions</h2>
<p>GitHub Super Linter was built to be run inside GitHub Actions. But running it locally or on other CI platforms can be particularly helpful. You will essentially be running Super Linter as you were locally in any other CI platforms.</p>
<h3 id="heading-how-to-run-super-linter-locally">How to Run Super Linter Locally</h3>
<p>You first want to pull the latest Docker container down from DockerHub with this command:</p>
<pre><code class="lang-shell">docker pull github/super-linter:latest
</code></pre>
<p>To run this container you then run the following:</p>
<pre><code class="lang-shell">docker run -e RUN_LOCAL=true -e USE_FIND_ALGORITHM=true VALIDATE_PYTHON_BLACK=true -v /project/directory:/tmp/lint github/super-linter
</code></pre>
<p>Notice a couple of things here:</p>
<ul>
<li>We run it with the <code>RUN_LOCAL</code> flag to bypass some of the GitHub Actions checks. This automatically sets <code>VALIDATE_ALL_CODEBASE</code> to true.</li>
<li>We map our local codebase to <code>/tmp/lint</code> so that the linter can pick up the code.</li>
<li>The way we set environment variables is of course different, but the overall process of running the GitHub Super Linter remains the same.</li>
</ul>
<h3 id="heading-how-to-run-super-linter-on-other-ci-platforms">How to Run Super Linter on Other CI Platforms</h3>
<p>Running GitHub Super Linter on other CI platform is pretty similar to locally running it. Here is an example of running it in Azure Pipelines by <a target="_blank" href="https://blog.tyang.org/2020/06/27/use-github-super-linter-in-azure-pipelines/">Tao Yang</a>.</p>
<pre><code class="lang-yaml"><span class="hljs-bullet">-</span> <span class="hljs-attr">job:</span> <span class="hljs-string">lint_tests</span>
  <span class="hljs-attr">displayName:</span> <span class="hljs-string">Lint</span> <span class="hljs-string">Tests</span>
  <span class="hljs-attr">pool:</span>
    <span class="hljs-attr">vmImage:</span> <span class="hljs-string">ubuntu-latest</span>
  <span class="hljs-attr">steps:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">script:</span> <span class="hljs-string">|
      docker pull github/super-linter:latest
      docker run -e RUN_LOCAL=true -v $(System.DefaultWorkingDirectory):/tmp/lint github/super-linter
</span>    <span class="hljs-attr">displayName:</span> <span class="hljs-string">'Code Scan using GitHub Super-Linter'</span>
</code></pre>
<p>This just runs the commands we would have for locally running the Super Linter as a script. You could run it in the exact same way on other CI platforms.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Thank you for sticking with me until the end. I hope that you've taken away a thing or two about linting and using GitHub Super Linter. It has most certainly been one of my favorite open source projects.</p>
<p>If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next post!</p>
<p>You can also find me on Twitter <a target="_blank" href="https://twitter.com/rishit_dagli">@rishit_dagli</a>, where I tweet about open source and machine learning.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Popular Front End Development Tools You Should Know ]]>
                </title>
                <description>
                    <![CDATA[ By Yiğit Kemal Erinç If you are just getting started with JavaScript, the number of tools and technologies you'll hear about may be overwhelming. And you might have a hard time deciding which tools you actually need. Or maybe you're familiar with the... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/front-end-development-tools-you-should-know/</link>
                <guid isPermaLink="false">66d45e4273634435aafcef82</guid>
                
                    <category>
                        <![CDATA[ Babel ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CircleCI ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Front-end Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ webpack ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 03 Nov 2020 02:35:19 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/11/frontend-dev-tools.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Yiğit Kemal Erinç</p>
<p>If you are just getting started with JavaScript, the number of tools and technologies you'll hear about may be overwhelming. And you might have a hard time deciding which tools you actually need.</p>
<p>Or maybe you're familiar with the tools, but you haven't given much thought to what problems they solve and how miserable your life would be without their help.</p>
<p>I believe it is important for Software Engineers and Developers to understand the purpose of the tools we use every day. </p>
<p>That's why, in this article, I look at NPM, Babel, Webpack, ESLint, and CircleCI and I try to clarify the problems they solve and how they solve them. </p>
<h2 id="heading-npm">NPM</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/11/image-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>NPM is the default package manager for JavaScript development. It helps you find and install packages (programs) that you can use in your programs.</p>
<p>You can add npm to a project simply by using the "<strong>npm init</strong>" command. When you run this command it creates a "<strong>package.json</strong>" file in the current directory. This is the file where your dependencies are listed, and npm views it as the ID card of the project. </p>
<p>You can add a dependency with the "<strong>npm install (package_name)</strong>" command. </p>
<p>When you run this command, npm goes to the remote registry and checks if there is a package identified by this package name. If it finds it, a new dependency entry is added to your <strong>package.json</strong> and the package, with it's internal dependencies, is downloaded from the registry.</p>
<p>You can find downloaded packages or dependencies under the <strong>"node_modules"</strong> folder. Just keep in mind that it usually gets pretty big – so make sure to add it to <strong>.gitignore</strong>.</p>
<p><img src="https://i2.wp.com/blog.logrocket.com/wp-content/uploads/2020/06/node-modules-meme.jpeg?resize=730%2C525&amp;ssl=1" alt="How to keep your JavaScript libraries up to date - LogRocket Blog" width="600" height="400" loading="lazy"></p>
<p>NPM does not only ease the process of finding and downloading packages but also makes it easier to work collaboratively on a project. </p>
<p>Without NPM, it would be hard to manage external dependencies. You would need to download the correct versions of every dependency by hand when you join an existing project. And that would be a real hassle. </p>
<p>With the help of npm, you can just run <strong>"npm install"</strong> and it will install all external dependencies for you. Then you can just run it again anytime someone on your team adds a new one.</p>
<h2 id="heading-babel">Babel</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/11/image-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Babel is a JavaScript compiler or transpiler which translates the ECMAScript 2015+ code into code that can be understood by older JavaScript engines.</p>
<p>Babel is the most popular Javascript compiler, and frameworks like Vue and React use it by default. That said, concepts we will talk about here are not only related to Babel and will apply to any JavaScript compiler.</p>
<h3 id="heading-why-do-you-need-a-compiler">Why do you need a compiler?</h3>
<p>"Why do we need a compiler, isn't JavaScript an interpreted language?" you may ask if you are familiar with the concepts of compiled and interpreted languages. </p>
<p>It's true that we usually call something a "compiler" if it translates our human-readable code to an executable binary that can be understood by the CPU. But that is not the case here. </p>
<p>The term transpiler may be more appropriate since it is a subset of a compiler: Transpilers are compilers that translate the code from a programming language to another language (in this example, from modern JS to an older version).</p>
<p>JavaScript is the language of browsers. But there is a problem with browsers: Cross compatibility. JavaScript tools and the language itself are evolving rapidly and many browsers fail to match that pace. This results in compatibility issues.</p>
<p>You probably want to write code in the most recent versions of JavaScript so you can use its new features. But if the browser that your code is running has not implemented some of the new features in its JavaScript engine, the code will not execute properly on that browser.</p>
<p>This is a complex problem because every browser implements the features at a different speed. And even if they do implement those new features, there will always be people who use an older version of their browser. </p>
<p>So what if you want to be able to use the recent features but also want your users to view those pages without any problems?</p>
<p>Before Babel, we used polyfills to run older versions of certain code if the browser did not support the modern features. And when you use Babel, it uses polyfills behind the scenes and does not require you to do anything.</p>
<h3 id="heading-how-do-transpilerscompilers-work">How do transpilers/compilers work?</h3>
<p>Babel works similar to other compilers. It has parsing, transformation, and code generation stages. </p>
<p>We won't go in-depth here into how it works, since compilers are complicated things. But to understand the basics of how compilers work, you can check out the <a target="_blank" href="https://github.com/jamiebuilds/the-super-tiny-compiler">the-super-tiny-compiler project</a>. It is also mentioned in Babel's official documentation as being helpful in understanding how Babel works.</p>
<p>We can usually get away with knowing about Babel plugins and presets. Plugins are the snippets that Babel uses behind the scenes to compile your code to older versions of JavaScript. You can think of each modern feature as a plugin. You can go to <a target="_blank" href="https://babeljs.io/docs/en/plugins/">this</a> link to check out the full list of plugins.</p>
<p><img src="https://erinc.io/wp-content/uploads/2020/10/image.png" alt="Image" width="600" height="400" loading="lazy">
<em>List of plugins for ES5</em></p>
<p>Presets are collections of plugins. If you want to use Babel for a React project you can use the pre-made <strong>@babel/preset-react</strong> which contains the necessary plugins.</p>
<p><img src="https://erinc.io/wp-content/uploads/2020/10/image-1.png" alt="Image" width="600" height="400" loading="lazy">
<em>React Preset Plugins</em></p>
<p>You can add plugins by editing the Babel config file.</p>
<h3 id="heading-do-you-need-babel-for-your-react-app">Do you need Babel for your React App?</h3>
<p>For React, you need a compiler because React code generally uses JSX and JSX needs to be compiled. Also the library is built on the concept of using ES6 syntax. </p>
<p>Luckily, when you create a project with <strong>create-react-app</strong>, it comes with Babel already configured and you usually do not need to modify the config.</p>
<h3 id="heading-examples-of-a-compiler-in-action">Examples of a compiler in action</h3>
<p>Babel's website has an online compiler and it is really helpful to understand how it works. Just plug in some code and analyze the output.</p>
<p><img src="https://erinc.io/wp-content/uploads/2020/10/image-4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><img src="https://erinc.io/wp-content/uploads/2020/10/image-5.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-webpack">Webpack</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/11/image.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Webpack is a static module bundler. When you create a new project, most JavaScript frameworks/libraries use it out of the box nowadays. </p>
<p>If the phrase "static module bundler" sounds confusing, keep reading because I have some great examples to help you understand.</p>
<h3 id="heading-why-do-you-need-a-bundler">Why do you need a bundler?</h3>
<p>In web apps you're going to have a lot of files. This is especially the case for Single Page Applications (React, Vue, Angular), with each having their own dependencies. </p>
<p>What I mean by a dependency is an import statement – if file A needs to import file B to run properly, then we say A depends on B. </p>
<p>In small projects, you can handle the module dependencies with <code>&lt;script&gt;</code> tags. But when the project gets larger, the dependencies rapidly become hard to manage. </p>
<p>Maybe, more importantly, dividing the code into multiple files makes your website load more slowly. This is because the browser needs to send more requests compared to one large file, and your website starts to consume a ton of bandwidth, because of HTTP headers.</p>
<p>We, as developers want our code to be modular. We divide it into multiple files because we do not want to work with one file with thousands of lines. Still, we also want our websites to be performant, to use less bandwidth, and to load fast. </p>
<p>So now, we'll see how Webpack solves this issue.</p>
<h3 id="heading-how-webpack-works">How Webpack works</h3>
<p>When we were talking about Babel, we mentioned that JavaScript code needs to be transpiled before the deployment. </p>
<p>But compiling with Babel is not the only operation you need before deploying your project. </p>
<p>You usually need to uglify it, transpile it, compile the SASS or SCSS to CSS if you are using any preprocessors, compile the TypeScript if you are using it...and as you can see, this list can get long easily. </p>
<p>You do not want to deal with all those commands and operations before every deployment. It would be great if there was a tool that did all that for you in the correct order and correct way. </p>
<p>The good news – there is: Webpack.</p>
<p>Webpack also provides features like a local server with hot reload (they call it hot module replacement) to make your development experience better. </p>
<p>So what's hot reloading? It means that whenever you save your code, it gets compiled and deployed to the local HTTP server running on your machine. And whenever a file changes, it sends a message to your browser so you do not even need to refresh the page. </p>
<p>If you have ever used "npm run serve", "npm start" or "npm run dev", those commands also start Webpack's dev server behind the scenes.</p>
<p>Webpack starts from the entry point of your project (index) and generates the Abstract Syntax Tree of the file. You can think of it as parsing the code. This operation is also done in compilers, which then look for import statements recursively to generate a graph of dependencies.</p>
<p>It then converts the files into [IIFEs](https://developer.mozilla.org/en-US/docs/Glossary/IIFE#:~:text=An%20IIFE%20(Immediately%20Invoked%20Function,soon%20as%20it%20is%20defined.) to modularize them (remember, putting code inside a function restricts its scope). By doing this, they modularize the files and make sure the variables and functions are not accessible to other files. </p>
<p>Without this operation, it would be like copying and pasting the code of the imported file and that file would have the same scope.</p>
<p>Webpack does many other advanced things behind the scenes, but this is enough to understand the basics.</p>
<h2 id="heading-bonus-eslint">Bonus – ESLint</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/11/image-3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Code quality is important and helps keep your projects maintainable and easily extendable. While most of us developers recognize the significance of clean coding, we sometimes tend to ignore the long term consequences under the pressure of deadlines.</p>
<p>Many companies decide on coding standards and encourage developers to obey those standards. But how can you make sure that your code meets the standards? </p>
<p>Well, you can use a tool like ESLint to enforce rules in the code. For example, you can create a rule to enforce or disallow the usage of semicolons in your JavaScript code. If you break a rule, ESLint shows an error and the code does not even get compiled – so it is not possible to ignore that unless you disable the rule.</p>
<p>Linters can be used to enforce standards by writing custom rules. But you can also use the pre-made ESLint configs established by big tech companies to help devs get into the habit of writing clean code. </p>
<p>You can take a look at Google's ESLint config <a target="_blank" href="https://github.com/google/eslint-config-google">here</a> – it is the one I prefer.</p>
<p>ESLint helps you get used to best practices, but that's not its only benefit. ESLint also warns you about possible bugs/errors in your code so you can avoid common mistakes.</p>
<p><img src="https://erinc.io/wp-content/uploads/2020/11/image-1024x717.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-bonus-cicd-circleci">Bonus – CI/CD (CircleCI)</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/11/image-4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Continuous Integration/Development has gained a lot of popularity in recent years as many companies have adopted Agile principles. </p>
<p>Tools like Jenkins and CircleCI allow you to automate the deployment and testing of your software so you can deploy more often and reliably without going through difficult and error-prone build processes by yourselves. </p>
<p>I mention CircleCI as the product here because it is free and used frequently in JavaScript projects. It's also quite easy to use.</p>
<p>Let's go over an example: Say you have a deployment/QA server and your Git repository. You want to deploy your changes to your deployment/QA server, so here is an example process:</p>
<ol>
<li>Push the changes to Git</li>
<li>Connect to the server</li>
<li>Create a Docker container and run it</li>
<li>Pull the changes to the server, download all the dependencies (npm install)</li>
<li>Run the tests to make sure nothing is broken</li>
<li>Use a tool like ESLint/Sonar to ensure code quality</li>
<li>Merge the code if everything is fine</li>
</ol>
<p>With the help of CircleCI, you can automatically do all these operations. You can set it up and configure to do all of the above operations whenever you push a change to Git. It will reject the push if anything goes wrong, for example a failing test.</p>
<p>I will not get into the details of how to configure CircleCI because this article is more about the "Why?" of each tool. But if you are interested in learning more and seeing it in action, you can check out <a target="_blank" href="https://www.youtube.com/watch?v=CB7vnoXI0pE&amp;ab_channel=TheCodingTrain">this tutorial series</a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>The world of JavaScript is evolving rapidly and new tools are gaining popularity every year. </p>
<p>It's easy to react to this change by just learning how to use the tool – we are often too busy to take our time and think about the reason why that tool became popular or what problem it solves.</p>
<p>In this article, I picked the tools I think are most popular and shared my thoughts on their significance. I also wanted to make you think about the problems they solve rather than just the details of how to use them.</p>
<p>If you liked the article you can check out and subscribe to my <a target="_blank" href="https://erinc.io/">blog</a> where I try to write frequently. Also, let me know what you think by commenting so we can brainstorm or you can tell me what other tools you love to use :)</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create Your Own ESLint Config Package ]]>
                </title>
                <description>
                    <![CDATA[ By Leonardo Faria ESLint is a powerful tool that helps you enforce consistent coding conventions and ensure quality in your JavaScript codebase.  Coding conventions are sometimes difficult to decide on, and having a tool that automates their enforcem... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/creating-your-own-eslint-config-package/</link>
                <guid isPermaLink="false">66d85172075ce303cca1b3a2</guid>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 17 Jun 2020 13:00:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/06/susan-holt-simpson-2nSdQEd-Exc-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Leonardo Faria</p>
<p>ESLint is a powerful tool that helps you enforce consistent coding conventions and ensure quality in your JavaScript codebase. </p>
<p>Coding conventions are sometimes difficult to decide on, and having a tool that automates their enforcement helps avoid unnecessary discussions. Ensuring quality is also a welcome feature: linters are excellent tools for catching bugs, such as those related to variable scope.</p>
<p>ESLint is designed to be completely configurable, giving you the option of enabling/disabling each rule, or mixing the rules to match your needs.  </p>
<p>With this in mind, the JavaScript community and companies who use JavaScript can extend the original ESLint config. There are <a target="_blank" href="https://www.npmjs.com/search?q=eslint-config">several examples</a> in the npm registry: <a target="_blank" href="https://www.npmjs.com/package/eslint-config-airbnb">eslint-config-airbnb</a> is one of the most well-known.</p>
<p>In your daily routine, you will probably combine more than one config, since there is no one-config-fits-all. This post will show how to create your own repository of configurations, giving you the option to centralize all your rule definitions.</p>
<h2 id="heading-create-the-project">Create the project</h2>
<p>First you'll need to create a new folder and npm project. <a target="_blank" href="https://eslint.org/docs/developer-guide/shareable-configs">By convention</a>, the module name begins with <code>eslint-config-</code>, such as <code>eslint-config-test</code>.</p>
<pre><code class="lang-bash">mkdir eslint-config-test
<span class="hljs-built_in">cd</span> eslint-config-test
npm init
</code></pre>
<p>You will have a package.json file that will look like the following snippet:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"eslint-config-test"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"description"</span>: <span class="hljs-string">""</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"test"</span>: <span class="hljs-string">"echo \"Error: no test specified\" &amp;&amp; exit 1"</span>
  },
  <span class="hljs-attr">"author"</span>: <span class="hljs-string">""</span>,
  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"ISC"</span>
}
</code></pre>
<p>Next, it's time to add your ESLint dependencies:</p>
<pre><code class="lang-bash">npm install -D eslint eslint-config-airbnb eslint-config-prettier eslint-plugin-import eslint-plugin-jsx eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks prettier
</code></pre>
<p>The packages will change according to your needs. In this case, I work with React codebases and I use <a target="_blank" href="https://prettier.io/">Prettier</a> to format my code. The <a target="_blank" href="https://eslint.org/docs/developer-guide/shareable-configs#publishing-a-shareable-config">documentation</a> mentions that if your shareable config depends on a plugin, you should also specify it as a <code>peerDependency</code>.</p>
<p>Next, I will create a <code>.eslintrc.js</code> with my configuration - this is similar to what you already do in your apps:</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-attr">extends</span>: [
    <span class="hljs-string">'airbnb'</span>,
    <span class="hljs-string">'eslint:recommended'</span>,
    <span class="hljs-string">'plugin:import/errors'</span>,
    <span class="hljs-string">'plugin:react/recommended'</span>,
    <span class="hljs-string">'plugin:jsx-a11y/recommended'</span>,
    <span class="hljs-string">'plugin:prettier/recommended'</span>,
    <span class="hljs-string">'prettier/react'</span>,
  ],
  <span class="hljs-attr">plugins</span>: [
    <span class="hljs-string">'react-hooks'</span>,
  ],
  <span class="hljs-attr">rules</span>: {
  },
};
</code></pre>
<p>The <code>rules</code> object stores any rule that you want to override. In the snippet above <code>rules</code> is empty but feel free to check <a target="_blank" href="https://github.com/leonardofaria/eslint-config-leozera/blob/master/.eslintrc.js#L14:L58">my overrides</a>. In the Airbnb/JavaScript repository you can <a target="_blank" href="https://github.com/airbnb/javascript/issues/1089">find a list of overridden rules</a> by the community.</p>
<h3 id="heading-create-custom-rules">Create custom rules</h3>
<p>Time to create a <code>.prettierrc</code> with your custom rules - this is a tricky part since Prettier and ESLint can conflict on a few rules:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"tabWidth"</span>: <span class="hljs-number">2</span>
}
</code></pre>
<p>It is important to mention that the <code>.prettierrc</code> file should live in the root of the project that is using your package. Right now, I am manually copying it. </p>
<p>The next step is to export your config in the <code>index.js</code> file:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> eslintrc = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./.eslintrc.js'</span>);

<span class="hljs-built_in">module</span>.exports = eslintrc;
</code></pre>
<p>It is technically possible to create all configurations in the <code>index.js</code> file. But if you do this, you won't get the config object linted (insert your <a target="_blank" href="https://www.imdb.com/title/tt1375666/">Inception</a> joke here).</p>
<h3 id="heading-youre-done">You're done!</h3>
<p><em>Voilà!</em> That’s all you need to start your own package of configurations. You can test locally your config package by running the following in a JavaScript project:</p>
<pre><code class="lang-bash">npm install /Users/leonardo/path/to/eslint-config-test
</code></pre>
<p>Keep in mind that the dependencies of your configuration package may also be installed.</p>
<p>If everything looks fine, you can publish to the npm registry:</p>
<pre><code class="lang-bash">npm publish
</code></pre>
<h2 id="heading-full-example">Full example</h2>
<p>I have a functional GitHub project showing the setup of this post: <a target="_blank" href="https://github.com/leonardofaria/eslint-config-leozera">eslint-config-leozera</a>. You can also see it below:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codesandbox.io/embed/github/leonardofaria/eslint-config-leozera/tree/master/?fontsize=14&amp;theme=dark" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodeSandbox embed" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin" loading="lazy"></iframe></div>
<h2 id="heading-more-about-the-project">More about the project</h2>
<ul>
<li><a target="_blank" href="https://eslint.org/docs/user-guide/configuring">Configuring ESLint</a>: official ESLint docs. You know, <em>read the docs</em></li>
<li><a target="_blank" href="https://medium.com/@bretcameron/how-to-publish-your-first-npm-package-b224296fc57b">How to publish your first NPM package</a>: quoting the post subtitle – "everything you need to know to create a NPM package."</li>
<li><a target="_blank" href="https://github.com/wesbos/eslint-config-wesbos">eslint-config-wesbos</a>: a project by <a target="_blank" href="https://www.wesbos.com/">Wes Bos</a> that helped me while doing this work</li>
</ul>
<p>Also posted on <a target="_blank" href="https://bit.ly/2AKW42t">my blog</a>. If you like this content, follow me on <a target="_blank" href="https://twitter.com/leozera">Twitter</a> and <a target="_blank" href="https://github.com/leonardofaria">GitHub</a>. Cover photo by <a target="_blank" href="https://unsplash.com/photos/2nSdQEd-Exc">Susan Holt Simpson/Unsplash</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Don’t just lint your code - fix it with Prettier ]]>
                </title>
                <description>
                    <![CDATA[ Linting makes our lives easier because it tells us what’s wrong with our code. But how can we avoid doing the actual work that goes into fixing it? Previously I wrote about linting, what it is, and how it makes your life easier. At the end, I actuall... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/dont-just-lint-your-code-fix-it-with-prettier/</link>
                <guid isPermaLink="false">66b8e322682e4a25eed26199</guid>
                
                    <category>
                        <![CDATA[ clean code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Code Quality ]]>
                    </category>
                
                    <category>
                        <![CDATA[ code review ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ front end ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Front-end Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ frontend ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Prettier ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Colby Fayock ]]>
                </dc:creator>
                <pubDate>Wed, 06 Nov 2019 15:45:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/11/formatting-1.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Linting makes our lives easier because it tells us what’s wrong with our code. But how can we avoid doing the actual work that goes into fixing it?</p>
<p><a target="_blank" href="https://www.freecodecamp.org/news/what-is-linting-and-how-can-it-save-you-time/">Previously I wrote about linting</a>, what it is, and how it makes your life easier. At the end, I actually included a way that you could automatically fix your code. So why am I writing this?</p>
<h2 id="heading-what-do-you-mean-fix-it"><strong>What do you mean fix it?</strong></h2>
<p>Before we roll into it, let’s hit this quick. Linters are powerful and provide an easy way to scan your code for syntax errors that could lead to bugs. Or they can simply help keep a codebase clean, healthy, and consistent. When run, it will show all the issues and let you go through each one individually to fix them.</p>
<p>Taking that to the next level, some linters will actually allow you to pass in an argument to the command running the linter that allows it to fix it for you automagically. This means you don’t have to manually go through and make all of those little whitespace and semicolon (add them! ?) tweaks yourself!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/11/ron-swanson-happy.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Ron Swanson happy</em></p>
<h2 id="heading-so-what-more-can-i-do-to-fix-things"><strong>So what more can I do to fix things?</strong></h2>
<p>If you already use the fix option, thats a good start. But there are tools out there that have been developed specifically to tackle this problem beyond just a flag into your command. The one I’m going to cover is Prettier.</p>
<h2 id="heading-what-is-prettier"><strong>What is Prettier?</strong></h2>
<p><a target="_blank" href="https://prettier.io/">Prettier</a> pegs itself as “an opinionated code formatter." It takes an input of your code and outputs it in a consistent format stripping any of the original code style. It actually converts your code to a <a target="_blank" href="https://github.com/benjamn/recast">syntax tree</a>, then rewrites it using the styles and rules you and Prettier provide together via your ESLint config and Prettier’s default rules.</p>
<p>You can easily use Prettier alone just to format your code, which works just fine. But if you combine this with an underlying ESLint process, you get both a powerful linter and a powerful fixer. I’m going to show you how to make those work together.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/11/voltron.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Voltron</em></p>
<h2 id="heading-getting-started-with-prettier"><strong>Getting started with Prettier</strong></h2>
<p>For this walkthrough, I’m going to assume that you have ESLint set up and configured in an application. Particularly, I’m going to pick up where I left off in my previous walkthrough where <a target="_blank" href="https://www.freecodecamp.org/news/what-is-linting-and-how-can-it-save-you-time/">we installed ESLint to a React application</a>.</p>
<p>Additionally of note, Prettier tells us right from the start that it's an opinionated code formatter. You should expect that it will format your code in a consistent way, but maybe a different way than you currently have it configured. But don’t fret! You can tweak this configuration.</p>
<p>So what are we starting off with? We already:</p>
<ul>
<li>Have installed <a target="_blank" href="https://github.com/eslint/eslint">ESLint</a></li>
<li>Have added <a target="_blank" href="https://github.com/babel/babel-eslint">Babel</a> as our parser</li>
<li>Have added a <a target="_blank" href="https://github.com/yannickcr/eslint-plugin-react">plugin</a> that includes React configurations</li>
</ul>
<p>Next, let’s get started by installing a few packages:</p>
<pre><code class="lang-shell">yarn add prettier prettier-eslint prettier-eslint-cli -D
</code></pre>
<p><em>Note: the command above is similar to using <code>npm</code>. If your project doesn't use <code>yarn</code>, swap out to <code>npm</code> as appropriate.</em></p>
<p>Above, we’re installing:</p>
<ul>
<li><a target="_blank" href="https://github.com/prettier/prettier">prettier</a>: core Prettier package and engine</li>
<li><a target="_blank" href="https://github.com/prettier/prettier-eslint">prettier-lint</a>: passes the Prettier result to ESLint to fix using your ESLint config</li>
<li><a target="_blank" href="https://github.com/prettier/prettier-eslint-cli">prettier-eslint-cli</a>: helps Prettier and ESLint work together on various files across your project</li>
</ul>
<p>And we’re installing them as a dev dependency, as we don’t need it outside development.</p>
<h2 id="heading-configuring-your-new-formatter"><strong>Configuring your new formatter</strong></h2>
<p>Now that our packages are installed, we can set up <code>yarn</code> to run this command for us.</p>
<p>Previously, we set up a <code>lint</code> script to look like this in our <code>package.json</code>:</p>
<pre><code class="lang-json"><span class="hljs-string">"scripts"</span>: {
  ...
  <span class="hljs-attr">"lint"</span>: <span class="hljs-string">"eslint . --ext .js"</span>
  ...
}
</code></pre>
<p>We’re going to leave that as it is, but we’ll do something similar and create a new script right next to it called <code>format</code> for our formatter Prettier:</p>
<pre><code class="lang-json"><span class="hljs-string">"scripts"</span>: {
  ...
  <span class="hljs-attr">"format"</span>: <span class="hljs-string">"prettier-eslint --eslint-config-path ./.eslintrc.js --write '**/*.js'"</span>,
  <span class="hljs-attr">"lint"</span>: <span class="hljs-string">"eslint . --ext .js"</span>
  ...
}
</code></pre>
<p>So what’s going on there? We’re:</p>
<ul>
<li>Adding a new script called <code>format</code>, that we can run as <code>yarn format</code></li>
<li>We’re utilizing our <code>prettier-eslint-cli</code> package to run the formatting for us</li>
<li>We’re passing in our ESLint config located next to our <code>package.json</code> in the root of the project (change this if it’s in a different location)</li>
<li>And finally, we’re telling prettier to write all files matching <code>**/*.js</code>, or any JS files it finds recursively through our project</li>
</ul>
<p>The beauty here is that we're passing in our ESLint config to Prettier. This means we only have to maintain 1 config for both tools, but we still leverage the linting power of ESLint along with the formatting power of Prettier.</p>
<h2 id="heading-run-your-formatter"><strong>Run your formatter!</strong></h2>
<p>Now that we’re all set up, let’s run it! Run this following:</p>
<pre><code>yarn format
</code></pre><p>and immediately, we see that it works:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/11/prettier-command-line-success.png" alt="Image" width="600" height="400" loading="lazy">
<em>Successfully running Prettier</em></p>
<h2 id="heading-hey-my-code-looks-different"><strong>Hey, my code looks different!</strong></h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/11/spongebob-pitchforks.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Angry mob with pitchforks</em></p>
<p>As I mentioned earlier, Prettier tells us straight up, it’s an opinionated formatter. It ships with its own rules, sort of like its own ESLint config, so it will go through and make those changes as well.</p>
<p>Don’t abandon your code! Instead, you can review the changes, see if maybe it makes sense to keep it that way (it will be very consistent) or you can update your ESLint config (<code>.eslintrc.js</code>) to overwrite the rules you don’t like. This is also a good way to maybe learn some new things that you might not have expected to get caught before.</p>
<h2 id="heading-so-where-does-this-leave-us"><strong>So where does this leave us?</strong></h2>
<p>If you’ve followed along so far, we now have two commands:</p>
<ul>
<li><code>lint</code>: which will check your code for you and tell you what's wrong</li>
<li><code>format</code>: will automatically try to fix the problems for you</li>
</ul>
<p>When using these in practice, your best bet is to always run <code>format</code> first to let it try to automatically fix anything it can. Then immediately run <code>lint</code> to catch anything Prettier wasn’t able to fix automatically.</p>
<h2 id="heading-whats-next"><strong>What’s next?</strong></h2>
<p>Now that we can format our code automatically, we should be able to fix our code automatically!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/11/fresh-off-the-boat-mind-blown.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Eddie from Fresh Off the Boat's mind blown</em></p>
<p>Next time we’ll take this a step further and set up a <code>git</code> hook that will allow this to run before you commit. This means you won't ever have to worry about forgetting to run this again!</p>
<div id="colbyfayock-author-card">
  <p>
    <a href="https://twitter.com/colbyfayock">
      <img src="https://res.cloudinary.com/fay/image/upload/w_2000,h_400,c_fill,q_auto,f_auto/w_1020,c_fit,co_rgb:007079,g_north_west,x_635,y_70,l_text:Source%20Sans%20Pro_64_line_spacing_-10_bold:Colby%20Fayock/w_1020,c_fit,co_rgb:383f43,g_west,x_635,y_6,l_text:Source%20Sans%20Pro_44_line_spacing_0_normal:Follow%20me%20for%20more%20JavaScript%252c%20UX%252c%20and%20other%20interesting%20things!/w_1020,c_fit,co_rgb:007079,g_south_west,x_635,y_70,l_text:Source%20Sans%20Pro_40_line_spacing_-10_semibold:colbyfayock.com/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_68,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_145,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_222,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_295,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/v1/social-footer-card" alt="Follow me for more Javascript, UX, and other interesting things!" width="600" height="400" loading="lazy">
    </a>
  </p>
  <ul>
    <li>
      <a href="https://twitter.com/colbyfayock">? Follow Me On Twitter</a>
    </li>
    <li>
      <a href="https://youtube.com/colbyfayock">?️ Subscribe To My Youtube</a>
    </li>
    <li>
      <a href="https://www.colbyfayock.com/newsletter/">✉️ Sign Up For My Newsletter</a>
    </li>
  </ul>
</div>

<p><em>Originally published at <a target="_blank" href="https://www.colbyfayock.com/2019/11/dont-just-lint-your-code-fix-it-with-prettier/">https://www.colbyfayock.com/2019/11/dont-just-lint-your-code-fix-it-with-prettier/</a></em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What is linting and how can it save you time? ]]>
                </title>
                <description>
                    <![CDATA[ One of the biggest challenges in software development is time. It’s something we can’t easily get more of, but linting can help us make the most out of the time we have. So what is linting? lint, or a linter, is a tool that analyzes source code to fl... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-linting-and-how-can-it-save-you-time/</link>
                <guid isPermaLink="false">66b8e39893a17625e9eef113</guid>
                
                    <category>
                        <![CDATA[ clean code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Code Quality ]]>
                    </category>
                
                    <category>
                        <![CDATA[ code review ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ front end ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Front-end Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ frontend ]]>
                    </category>
                
                    <category>
                        <![CDATA[ GatsbyJS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Colby Fayock ]]>
                </dc:creator>
                <pubDate>Wed, 09 Oct 2019 14:30:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/10/linting.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>One of the biggest challenges in software development is time. It’s something we can’t easily get more of, but linting can help us make the most out of the time we have.</p>
<h2 id="heading-so-what-is-linting">So what is linting?</h2>
<p><strong>lint</strong>, or a <strong>linter</strong>, is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs. <a target="_blank" href="https://en.wikipedia.org/wiki/Lint(software)">https://en.wikipedia.org/wiki/Lint(software)</a></p>
<p>Simply put, a linter is a tool that programmatically scans your code with the goal of finding issues that can lead to bugs or inconsistencies with code health and style. Some can even help fix them for you!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/miachel-scott-tell-me-more.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Michael Scott - Tell me more</em></p>
<p>Take for instance, the following example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> test = <span class="hljs-string">'I am a test'</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Test: <span class="hljs-subst">${test}</span>`</span>);
<span class="hljs-keyword">const</span> test = <span class="hljs-string">'Another one.'</span>;
</code></pre>
<p>We’re declaring the constant <code>test</code> twice, which our javascript engine won’t be happy about. With the proper linter settings and watch configuration, instead of getting caught later as an error when the code runs, you’ll immediately get an error through your linter running in the background:</p>
<pre><code>  <span class="hljs-number">10</span>:<span class="hljs-number">9</span>  error  Parsing error: Identifier <span class="hljs-string">'test'</span> has already been declared

   <span class="hljs-number">8</span> |   <span class="hljs-keyword">const</span> test = <span class="hljs-string">'I am a test'</span>;
   <span class="hljs-number">9</span> |   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Test: <span class="hljs-subst">${<span class="hljs-number">2</span>}</span>`</span>);
&gt; <span class="hljs-number">10</span> |   <span class="hljs-keyword">const</span> test = <span class="hljs-string">'Another one.'</span>;
     |         ^
</code></pre><p>It might be pretty obvious that you have 2 of the same <code>const</code> declarations given this is only 3 lines, but in a more complex application, this can save tons of time having to hunt down a pesky bug that’s not always obvious.</p>
<h2 id="heading-what-all-can-linting-help-with"><strong>What all can linting help with?</strong></h2>
<p><a target="_blank" href="https://eslint.org/docs/rules/">Lots of things</a>, including but not limited to:</p>
<ul>
<li>Flagging bugs in your code from syntax errors</li>
<li>Giving you warnings when code may not be intuitive</li>
<li>Providing suggestions for common best practices</li>
<li>Keeping track of TODO’s and FIXME’s</li>
<li>Keeping a consistent code style</li>
</ul>
<p>Most things you can think of probably already <a target="_blank" href="https://github.com/dustinspecker/awesome-eslint">exist in one form or another</a>, and if not, you can even <a target="_blank" href="https://gist.github.com/sindresorhus/1656c46f23545deff8cc713649dcff26">create custom rules</a> that fit your needs!</p>
<h2 id="heading-how-is-this-actually-helping-or-why-should-i-care"><strong>How is this actually helping or why should I care?</strong></h2>
<p>Probably the biggest overlying theme of the list above is the fact that these issues will be called out immediately. No longer will these issues creep up on you in the middle of running your app or give someone anxiety during a code review. No longer will you and your reviewer endlessly fight passive aggressively through the comments about whether or not to include a semicolon at the end of JS statements (<a target="_blank" href="https://stackoverflow.com/a/444082">you should</a> ?).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/looking-for-semicolon.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Grandma looking for a semicolon</em></p>
<p>All of those moments that stop you from being productive because of a silly syntax error or the micro-interactions you and your teammates have during a review take time. They add up and end up taking away the time you can spend fixing another bug or developing the next great feature of your product.</p>
<h2 id="heading-so-how-do-i-actually-get-started"><strong>So how do I actually get started?</strong></h2>
<p>Even though there are linters for most, if not all, other mainstream languages, for the purpose of this post, I’m going to focus on Javascript. The same principles apply, but the tooling may be a bit different.</p>
<p>I’m going to run through how you can get set up for basic linting in a React app. You can easily follow along by spinning up your own React app or using my <a target="_blank" href="https://www.gatsbyjs.org/">Gatsby</a> starter: <a target="_blank" href="https://github.com/colbyfayock/gatsby-starter-sass#starting-from-scratch">https://github.com/colbyfayock/gatsby-starter-sass#starting-from-scratch</a></p>
<h3 id="heading-your-linter"><strong>Your Linter</strong></h3>
<p>To get started, we first need a linter. <a target="_blank" href="https://trends.google.com/trends/explore?geo=US&amp;q=eslint,jshint,jslint">Probably the most popular</a> in the Javascript world is <a target="_blank" href="https://eslint.org/">ESLint</a>. Your linter will actually be the engine for defining rules and parsing your files to test against. ESLint is available as an <a target="_blank" href="https://www.npmjs.com/package/eslint">npm package</a> by itself and <a target="_blank" href="https://eslint.org/docs/user-guide/getting-started">once installed</a>, out of the box it allows you to set up a basic configuration file and hit the ground running with some command line tools.</p>
<p>Let’s first add our ESLint dependency:</p>
<pre><code>yarn add eslint -D
</code></pre><p>We’re installing this as a <code>devDependency</code> (hence the <code>-D</code> flag), because this isn’t something our application needs to run. After it’s successfully installed, let’s add it to our <code>package.json</code> as a script:</p>
<pre><code class="lang-json">...
<span class="hljs-string">"scripts"</span>: {
  ...
  <span class="hljs-attr">"lint"</span>: <span class="hljs-string">"eslint .  --ext .js"</span>
  ...
},
...
</code></pre>
<p>In the above, we’re running our linter on the entire project directory on any file that has an extension of <code>.js</code>. If you're working with a large project with many file types, maybe even some you don't want linted, you can <a target="_blank" href="https://eslint.org/docs/user-guide/command-line-interface">change that flag or be more specific</a> with other options.</p>
<p>To support ESLint, we’ll need to do one more thing. Let’s add a file at the root of our project (probably where your <code>package.json</code> is) called <code>.eslintrc.js</code> and make the contents of the file simply:</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {};
</code></pre>
<p>Once you’re ready, you can run <code>yarn lint</code> and… get an error.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/lint-with-import-errors.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Lint results - Import errors</em></p>
<p>This is okay, and expected in our project, so let’s move on.</p>
<h3 id="heading-your-parser"><strong>Your Parser</strong></h3>
<p>A common tool in the chain for Javascript developers is <a target="_blank" href="https://babeljs.io/">Babel</a>, which allows you to write code with features that may not be supported in all browsers, such as using <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow functions</a>, that are available in <a target="_blank" href="http://es6-features.org/#Constants">ES6</a>, and other conventions like importing files via <code>import</code>.</p>
<p>The code you write may already run through Babel to work in a browser, but that doesn’t apply to ESLint by default, so ESLint allows you to specify a parser that allows the linting processing to look at the same code as your browser sees. In this case we’ll want to use <a target="_blank" href="https://github.com/babel/babel-eslint">Babel’s ESLint</a> parser that’s made available to us.</p>
<p>To set that up, we’ll want to first install our dependency:</p>
<pre><code>yarn add babel-eslint -D
</code></pre><p>Typically if you're using <code>babel-eslint</code> you'll want to make sure <code>babel</code> is installed next to it, but in our case, Gatsby already uses <code>babel</code>, so we don’t necessarily need to add it. After that’s set up, we’ll want to update our <code>.eslintrc.js</code> config file with some new options:</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-string">"env"</span>: {
        <span class="hljs-string">"browser"</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">"node"</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">"es6"</span>: <span class="hljs-literal">true</span>
    },
    <span class="hljs-string">"parser"</span>: <span class="hljs-string">"babel-eslint"</span>
};
</code></pre>
<p>Here, we’re letting ESLint know that our environment will be run in node (Gatsby’s precompiling), inside the browser (the app), and it will use ES6. This helps ESLint know how to run your code. Additionally, we want to set up our parser to be <code>babel-eslint</code>.</p>
<p>Once we’re ready to go, run <code>yarn lint</code> again and… well nothing really happened.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/lint-with-nothing-happening.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Lint results - Nothing happened</em></p>
<p>This is still expected, as we don’t have any rules set up!</p>
<h3 id="heading-plugins-for-your-code"><strong>Plugins for your code</strong></h3>
<p>Writing a <a target="_blank" href="https://reactjs.org/">React</a> app? The Babel parser may help you transform your code, but you might have a hard time being productive, as ESLint needs to understand how it should work to lint your React files.</p>
<p>Part of the beauty of ESLint is that it allows you to <a target="_blank" href="https://eslint.org/docs/developer-guide/working-with-plugins">configure plugins</a> that have the opportunity to create and set rules for you. Luckily, along with our Babel parser above that does some of the heavy lifting, we have a <a target="_blank" href="https://github.com/yannickcr/eslint-plugin-react">React plugin</a> available that does just that and takes care of linting the JSX for us.</p>
<p>Let’s first install our dependency:</p>
<pre><code>yarn add eslint-plugin-react -D
</code></pre><p>Further, let’s update our <code>.eslintrc.js</code> file again:</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-string">"settings"</span>: {
        <span class="hljs-string">"react"</span>: {
            <span class="hljs-string">"version"</span>: <span class="hljs-string">"detect"</span>
        }
    },
    <span class="hljs-string">"env"</span>: {
        <span class="hljs-string">"browser"</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">"node"</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">"es6"</span>: <span class="hljs-literal">true</span>
    },
    <span class="hljs-string">"plugins"</span>: [
        <span class="hljs-string">"react"</span>
    ],
    <span class="hljs-string">"parser"</span>: <span class="hljs-string">"babel-eslint"</span>
};
</code></pre>
<p>What we’re adding here is a setting that automatically detects what React version you’re using, which is helpful to let your linting get parsed properly, and then set up our react plugin that we installed above.</p>
<p>For one last final time, we will run our <code>lint</code> script and get nothing:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/lint-with-nothing-happening-1.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Lint results - Nothing happened</em></p>
<h3 id="heading-rules-defined-by-otherss-opinions"><strong>Rules defined by others’s opinions</strong></h3>
<p>If you’re not really sure where to get started or just want to quickly get up and running, it’s recommend that you enable <a target="_blank" href="https://eslint.org/docs/rules/">ESLint’s own recommended rules</a>. Let’s add this to our <code>.eslintrc.js</code> config file:</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-string">"settings"</span>: {
        <span class="hljs-string">"react"</span>: {
            <span class="hljs-string">"version"</span>: <span class="hljs-string">"detect"</span>
        }
    },
    <span class="hljs-string">"env"</span>: {
        <span class="hljs-string">"browser"</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">"node"</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">"es6"</span>: <span class="hljs-literal">true</span>
    },
    <span class="hljs-string">"plugins"</span>: [
        <span class="hljs-string">"react"</span>
    ],
    <span class="hljs-string">"extends"</span>: [
        <span class="hljs-string">"eslint:recommended"</span>
    ],
    <span class="hljs-string">"parser"</span>: <span class="hljs-string">"babel-eslint"</span>
};
</code></pre>
<p>And let’s run our <code>yarn lint</code>.</p>
<p>Woah! This will immediately give you a lot errors, it seems like something’s wrong.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/lint-with-react-errors.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Lint results - React errors</em></p>
<p>Since we’re running a React app, we also want to make sure our linter understands the rules it should follow, so let’s also add our React plugin to the <code>.eslintrc.js</code> config setup:</p>
<pre><code class="lang-json">    <span class="hljs-string">"extends"</span>: [
        <span class="hljs-string">"eslint:recommended"</span>,
        <span class="hljs-string">"plugin:react/recommended"</span>
    ],
</code></pre>
<p>Now if you run <code>yarn lint</code>, you get something a little more logical.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/lint-with-errors.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Lint results - Normal errors</em></p>
<p>If you’re following along, it looks like our starter app had a misplaced semicolon and a missing prop in our <code>propTypes</code> validation for <code>Layout.js</code>. Writing this helped me fix my own repo! ?</p>
<p>Going further, if those don’t seem to fit your needs, lots of developers and teams have published their own configurations that ESLint allows you to easily tap into.</p>
<p>Some popular ones include:</p>
<ul>
<li><a target="_blank" href="https://www.npmjs.com/package/eslint-config-airbnb">Airbnb’s config</a></li>
<li><a target="_blank" href="https://github.com/standard/eslint-config-semistandard">Semistandard</a></li>
<li><a target="_blank" href="https://github.com/google/eslint-config-google">Google’s JS Style Guide</a></li>
</ul>
<p>Not happy with the options available? You can even <a target="_blank" href="https://eslint.org/docs/6.0.0/developer-guide/shareable-configs">create and publish your own</a> to either layer on top of others as a starting point or give it a go from scratch.</p>
<h3 id="heading-let-it-do-the-work-most-of-it"><strong>Let it do the work (most of it)</strong></h3>
<p>You don’t think I’m going to make you fix all of those thing yourself do you? Well, you may have to fix some, but let’s try to get ESLint to fix some of it for us.</p>
<p>If you noticed after we ran the command above, ESLint gave us an extra message:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/lint-fix-option.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Lint results - Option to fix</em></p>
<p>So let’s give it a try! Let’s run:</p>
<pre><code>yarn lint --fix
</code></pre><p>And what do you know, it now only gives us 1 linting error.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/lint-with-one-error.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Lint results - 1 error</em></p>
<p>Turns out ESLint was able to fix our semicolon issue automatically, but we’ll still have to add <code>pageName</code> to our <code>Layout</code>’s <code>propTypes</code> manually, not too much of a lift.</p>
<p>Running one more time and finally nothing again! But this time because everything's looking good.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/lint-with-nothing-happening-2.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Lint results - No errors</em></p>
<h2 id="heading-go-forth-and-write-messy-code"><strong>Go forth and write messy code!</strong></h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/10/bruce-almighty-typing.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Bruce Almighty - Typing</em></p>
<p>Kidding ? The good news here, is now you can easily take a look at the general health of your codebase as well as hopefully fix most of it automatically. This is going to save a lot of headaches as you work with others on your team, and generally, it’s nice to have all of your code neat and tidy.</p>
<p>This post is just getting started. ESLint is a wide open book with tons of plugins and rules, and it’s not the only linting tool in the game. Play around and find what fits best for you and your team. The little time spent configuring it to your app will save you lots more time in the long run.</p>
<h2 id="heading-other-linting-tools-to-check-out"><strong>Other linting tools to check out</strong></h2>
<ul>
<li><a target="_blank" href="https://jshint.com/">JSHint</a>: an alternative to ESLint</li>
<li><a target="_blank" href="https://github.com/stylelint/stylelint">Stylelint</a>: a linting tool for CSS and CSS-like syntaxes like <a target="_blank" href="https://sass-lang.com/">Sass</a></li>
<li><a target="_blank" href="https://github.com/dustinspecker/awesome-eslint">Awesome ESLint</a>: a simple list of awesome configs, parsers, plugins, and other tools to boost your ESLint game</li>
<li><a target="_blank" href="https://webhint.io/">Webhint</a>: linting tool for accessibility, speed, and more website best practices</li>
<li><a target="_blank" href="https://github.com/evcohen/eslint-plugin-jsx-a11y">A11y JSX Plugin</a>: ESLint plugin for checking accessibility rules on JSX elements</li>
</ul>
<div id="colbyfayock-author-card">
  <p>
    <a href="https://twitter.com/colbyfayock">
      <img src="https://res.cloudinary.com/fay/image/upload/w_2000,h_400,c_fill,q_auto,f_auto/w_1020,c_fit,co_rgb:007079,g_north_west,x_635,y_70,l_text:Source%20Sans%20Pro_64_line_spacing_-10_bold:Colby%20Fayock/w_1020,c_fit,co_rgb:383f43,g_west,x_635,y_6,l_text:Source%20Sans%20Pro_44_line_spacing_0_normal:Follow%20me%20for%20more%20JavaScript%252c%20UX%252c%20and%20other%20interesting%20things!/w_1020,c_fit,co_rgb:007079,g_south_west,x_635,y_70,l_text:Source%20Sans%20Pro_40_line_spacing_-10_semibold:colbyfayock.com/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_68,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_145,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_222,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_295,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/v1/social-footer-card" alt="Follow me for more Javascript, UX, and other interesting things!" width="600" height="400" loading="lazy">
    </a>
  </p>
  <ul>
    <li>
      <a href="https://twitter.com/colbyfayock">? Follow Me On Twitter</a>
    </li>
    <li>
      <a href="https://youtube.com/colbyfayock">?️ Subscribe To My Youtube</a>
    </li>
    <li>
      <a href="https://www.colbyfayock.com/newsletter/">✉️ Sign Up For My Newsletter</a>
    </li>
  </ul>
</div>

<p><em>Originally published</em> at <a target="_blank" href="https://www.colbyfayock.com/2019/10/what-is-linting-and-how-can-it-save-you-time">https://www.colbyfayock.com/2019/10/what-is-linting-and-how-can-it-save-you-time</a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ ESLint: The Essential Facts About Essential Front End Tools ]]>
                </title>
                <description>
                    <![CDATA[ By Vinh Nguyen Recently, I've been getting more involved in front-end development. The more I do, the more my mind and my soul get lost in its chaotic world. Even a simple To–Do–List app can easily require a bunch of tools—ESLint, Babel, Webpack, to ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-essentials-eslint/</link>
                <guid isPermaLink="false">66d46040a326133d12440a39</guid>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Front-end Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 13 Aug 2019 15:07:35 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/08/The-Essentials--ESLint-01.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Vinh Nguyen</p>
<p>Recently, I've been getting more involved in front-end development. The more I do, the more my mind and my soul get lost in its chaotic world. Even a simple To–Do–List app can easily require a bunch of tools—<a target="_blank" href="https://eslint.org">ESLint</a>, <a target="_blank" href="https://babeljs.io/">Babel</a>, <a target="_blank" href="https://webpack.js.org/">Webpack</a>, to name a few—and packages just to get started. </p>
<p>Fortunately, there’re many starter kits out there so we don’t have to do everything from the ground up. With them, everything is already set up so we can start writing the first line of code right away. It saves time on repetitive, boring tasks, which can be great for experienced developers. </p>
<p>However, this benefit comes with a price for beginners. Since everything works out of the box, it seems like magic, and they might not know what's really happening under the hood, which is important to understand at some level. Although the learning curve is not as steep as others—try to compare with some tools you've been learning and using, you'll get what I mean—in this chaotic world, we need a survival guide for the journey.</p>
<p>This series will cover fundamental tools of front-end development and what essentials we need to know about them. This will allow us to rule the tools instead of being controlled by them. </p>
<p>In it, we’ll focus on the developer experience of each one of these tools. So the goal of this series is to act as a survival guide and to give a high-level overview of each tool, not to serve as documentation.</p>
<p>What will be included:</p>
<ul>
<li>ESLint &lt;- We are here</li>
<li>Babel</li>
<li>Webpack</li>
<li>Flow</li>
<li>TypesScript</li>
<li>Jest.</li>
</ul>
<p>Enough of a preface, let's get started with the first tool: ESLint.</p>
<h1 id="heading-what-is-eslint-and-why-should-we-use-it">What is ESLint and Why should we use it?</h1>
<p>ESLint is, as the name implies, a linter for <a target="_blank" href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript</a>. And, the definition of a linter is:</p>
<blockquote>
<p>a machine for removing the short fibers from cotton seeds after ginning.</p>
</blockquote>
<p>Although code and cotton seeds don't have any relationship, regardless of code or cotton seeds, a linter will help make things cleaner and more consistent. We don't want to see the code like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> count = <span class="hljs-number">1</span>;
<span class="hljs-keyword">const</span> message  =  <span class="hljs-string">"Hello, ESLint"</span>
    count += <span class="hljs-number">1</span>
</code></pre>
<p>It both looks ugly and has a mistake. Here's when ESLint steps in to help with that. Instead of letting the error be dumped out to the browser console when we run the code, ESLint will catch it as we're typing (well not really: we’ll need editor or IDE extensions for this, which will be covered later). </p>
<p>Of course, this error isn't difficult to figure out, but wouldn't it be nicer to have an assistant reminding us every time we're about to make a mistake and perhaps auto-correcting it for us? Although ESLint can’t catch all kinds of errors, it at least spares us some effort so we can spend time on other things that matter and need human attention.</p>
<h1 id="heading-how-does-eslint-work">How does ESLint work?</h1>
<p>Now that we know what ESLint is and why we need it, let's go a bit deeper and check out how it works. In essence, we can break it down to three big steps.</p>
<h2 id="heading-parser">Parser</h2>
<p>The code that we write is nothing more than a sequence of characters. However, this sequence isn't just random characters: they need to follow a set of rules or conventions that is the grammar forming a language. </p>
<p>For a human, going from reading text or code to understanding it conceptually takes us little effort. For a computer, this is much more difficult to accomplish. For example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> tool = <span class="hljs-string">'ESLint'</span> <span class="hljs-comment">// 1</span>
  <span class="hljs-keyword">const</span>  tool  =  <span class="hljs-string">"ESLint"</span> <span class="hljs-comment">// 2</span>
</code></pre>
<p>As we read the two lines above, we immediately know that they are identical, and can be read as "there's a constant named <code>tool</code> with the value of ESLint". For a computer, which doesn't understand the meaning, these two lines look quite different. As a result, if we feed in raw code to ESLint, it's nearly impossible to do anything. </p>
<p>When things get complicated and hard to communicate—think of how we can have a computer to understand what we do—abstraction can be an escape. By abstracting a thing, we hide all unnecessary details, reduce noise, and keep everyone on the same page, which eases the communication. In the above example, one space or two spaces don’t matter, neither do single quotes or double quotes.</p>
<p>In other words, that's what a parser does. It converts raw code to an Abstract Syntax Tree (AST), and this AST is used as the medium for lint rules to base on. There are still many steps a parser need to do in order to create an AST—if you're interested in learning more about how an AST is generated, <a target="_blank" href="https://the-super-tiny-compiler.glitch.me/">this tutorial</a> has a good overview.</p>
<h2 id="heading-rules">Rules</h2>
<p>The next step in the process is to run the AST through a list of rules. A rule is a logic of how to figure out potential existing issues in the code from the AST. Issues here aren't necessarily syntactic or semantic errors, but might be stylistic ones as well. The output given out from a rule will include some useful information for later use like lines of code, positions, and informative messages about the issue.</p>
<p>In addition to catching issues, a rule can even auto-correct code if possible. For example, when <a target="_blank" href="https://eslint.org/docs/rules/no-multi-spaces">no-multi-spaces</a> is applied to the code above, it will trim all redundant spaces, which makes the code look clean and consistent.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span>  tool  =  <span class="hljs-string">"ESLint"</span> <span class="hljs-comment">// 2</span>
<span class="hljs-comment">// becomes</span>
<span class="hljs-keyword">const</span> tool = <span class="hljs-string">"ESLint"</span> <span class="hljs-comment">// 2</span>
</code></pre>
<p>In different scenarios, a rule can be used at different levels—opted-out, warning only, or strict error—and have various options, which gives us control on how to use the rule.</p>
<h2 id="heading-result">Result</h2>
<p>Here comes the end of the process. With the output from a rule, it's just the matter of how we display it in a human friendly manner, thanks to all the useful information we mentioned earlier. Then from the result, we can quickly point out the issue, where it is, and make a fix, or maybe not.</p>
<h1 id="heading-integration">Integration</h1>
<p>ESLint can be used as a standalone tool with its robust CLI, but that’s a bare-bones way to use ESLint. We don't want to type in a command every time we want to lint code, especially in a development environment. The solution for this is to integrate ESLint into our development environment so we can write code and see issues caught by ESLint all in one place.</p>
<p>This kind of integration comes from extensions specific to IDEs or editors. These extensions require ESLint to work since they run ESLint behind the scene—no wonder that we still need to install ESLint along with them, they are nothing without ESLint. This principle applies to other IDE or editor extensions we are using daily.</p>
<p>Remember the output from a rule we talked above? An extension will use it to display in the IDE or editor. How exactly the output is displayed depends on how the extension is implemented and how the IDE or editor is open to its extensions. Some extensions also take advantage of the abilities of issue correction from rules to change code on save if we enable it.</p>
<h1 id="heading-configuration">Configuration</h1>
<p>Configuration is the main power that gives versatility to a tool. ESLint is not different from that, except it has the most comprehensive configuration among other tools. In general, we need a file or a place to put the configuration, and there's a couple of options of us. </p>
<p>All of them boil down to two main ways: either we have a separate configuration file for each tool, or we bundle them all in <code>package.json</code>.  <code>.eslintrc.js</code> is one of the files that ESLint will be looking for its configuration, and also the one with the highest priority.</p>
<p>The next thing we need to know about configuration is its hierarchy and cascading behavior. Thanks to these features, we don't need to have a configuration file in every single folder in the project. </p>
<p>If a configuration file doesn't exist in a folder, ESLint simply looks up the folder's parent for one until it can't find one. Then it'll fall back to the user–wide default configuration in <code>~/.eslintrc</code>. Otherwise, the configuration file will add up or override the ones at upper levels.</p>
<p>There is, however, a special tweak on this. If we specify <code>root: true</code> in a configuration file, the lookup will stop at that file instead of going up like before. Besides, ESLint will use that configuration file as the root configuration, and all child configurations will be based on this one.</p>
<p>This is not only limited to ESLint - these things are common for other tools. Let's talk about ESLint specific configuration.</p>
<h2 id="heading-parser-1">Parser</h2>
<p>The role of the parser in ESLint has been discussed above. By default, ESLint uses <a target="_blank" href="https://github.com/eslint/espree">Espree</a> as its parser. We can change this parser to another compatible one like <a target="_blank" href="https://www.npmjs.com/package/babel-eslint">babel-eslint</a> or <a target="_blank" href="https://www.npmjs.com/package/@typescript-eslint/parser">@typescript-eslint/parser</a> if we use Babel or Typescript, respectively.</p>
<p>To configure the parser, we use <code>parserOptions</code>. Among options supported by Espree, here are some we often use and need to pay attention to:</p>
<ul>
<li><code>ecmaVersion</code></li>
</ul>
<p>We need to specify the appropriate ECMA version to features we want to use. For example, if <code>emcaVersion: 5</code>, the code below will give some errors.</p>
<pre><code><span class="hljs-string">``</span><span class="hljs-string">`javascript
let a = [1, 2, 3, 4] // error due to `</span><span class="hljs-keyword">let</span><span class="hljs-string">` keyword
var b = [...a, 5] // error due to spread syntax</span>
</code></pre><pre><code>
The parser can<span class="hljs-string">'t parse the code because both the `let` keyword and spread syntax were just introduced in ES6. Changing `emcaVersion` to 6 or above will simply resolve the errors.

* `sourceType`

Nowadays, we mostly write everything in modules, then bundle them together. So this option, most of the time, should be `module`. 

Another value we can use—as well as the default—is `script`. The difference is whether we can use [JS modules](https://v8.dev/features/modules) or not, i.e., use `import` and `export` keyword. The next time we get this error message `Parsing error: '</span><span class="hljs-keyword">import</span><span class="hljs-string">' and '</span><span class="hljs-keyword">export</span><span class="hljs-string">' may appear only with '</span>sourceType: <span class="hljs-built_in">module</span><span class="hljs-string">'`, we know where to look.

* `ecmaFeatures.jsx`

There might be additional ES features we want to use, for example [JSX](https://facebook.github.io/jsx/) syntax. We use `ecmaFeatures.jsx: true` to enable this feature. Note that, JSX support from Espree isn'</span>t the same <span class="hljs-keyword">as</span> JSX <span class="hljs-keyword">in</span> React. If we want React specific JSX, we should use [eslint-plugin-react](https:<span class="hljs-comment">//github.com/yannickcr/eslint-plugin-react) for better results.</span>

If we use another parser, these options are more or less the same. Some might have fewer options, and others might have more, but they<span class="hljs-string">'re all defined under `parserOptions`.

## Environment

It depends on where the code is running: there are different predefined global variables. We have `window`, `document` in the browser, for example. It would be irritating if the [no-undef](https://eslint.org/docs/rules/no-undef) rule is enabled, and ESLint keeps telling us `window` or `document` is not defined.

The `env` option is here to help. By specifying a list of environments, ESLint will know about global variables in these environments, and let us use them without a word.

There'</span>s a special environment we need to note, <span class="hljs-string">`es6`</span>. It<span class="hljs-string">'ll implicitly set `parserOptions.ecmaVersion` to 6, and enable all ES6 features except for modules which we still need to use `parserOptions.sourceType: "module"` separately.

# Plugins and Shareable Configs

Having the same configuration for rules over and over again across different projects might be tiresome. Luckily, we can reuse a configuration, and only override rules as needed with `extends`. We call this type of config shareable configs, and ESLint already has two for us: `eslint:recommended` and `eslint:all`. 

Conventionally, ESLint'</span>s shareable configs have <span class="hljs-string">`eslint-config`</span> prefix so we can easily find them via NPM <span class="hljs-keyword">with</span> [<span class="hljs-string">`eslint-config`</span>](https:<span class="hljs-comment">//www.npmjs.com/search?q=keywords:eslint-config) keyword. Among hundreds of results, there're some popular ones, like [eslint-config-airbnb](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb) or [eslint-config-google](https://github.com/google/eslint-config-google), you name it.</span>

Out <span class="hljs-keyword">of</span> the box, ESLint has a bunch <span class="hljs-keyword">of</span> rules to serve different purposes <span class="hljs-keyword">from</span> possible errors, best practices, ES6 to stylistic issues. Moreover, to supercharge its ability, ESLint has a great number <span class="hljs-keyword">of</span> <span class="hljs-number">3</span>rd-party rules provided by almost a thousand plugins. Similar to shareable configs, ESLint<span class="hljs-string">'s plugins are prefixed with `eslint-plugin`, and are available on NPM with the [`eslint-plugin`](https://www.npmjs.com/search?q=keywords:eslint-plugin) keyword.

A plugin defines a set of new rules, and in most cases it exposes its own handy configs. For example, the [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react) gives us two shareable configs, `eslint-plugin-react:recommended` and `eslint-plugin-react:all` just like `eslint:recommended` and `eslint:all`. To use one of them, we need to, firstly, define the plugin name, and secondly extend the config.

```javascript
{
  plugins: ["react"],
  extends: "plugin:react/recommended"
}

// Note that we need to prefix the config by `plugin:react`</span>
</code></pre><p>One common question to ask is what plugins or configs to use. While it largely depends on our needs, we can use <a target="_blank" href="https://github.com/dustinspecker/awesome-eslint">Awesome ESLint</a> as a reference to find useful plugins as well as configs.</p>
<h1 id="heading-prettier">Prettier</h1>
<p>We're almost there - we've almost gotten to the end. Last but not least, we'll discuss a popular pair of ESLint, <a target="_blank" href="https://github.com/prettier/prettier">Prettier</a>. In short, Prettier is an opinionated code formatter. Though Prettier can be used alone, integrating it to ESLint enhances the experience a lot, and <a target="_blank" href="https://github.com/prettier/eslint-plugin-prettier">eslint-plugin-prettier</a> does this job.</p>
<p>The difference between using Prettier alone and using Prettier with ESLint can be summarized to code formatting as an issue. Instead of giving format issues separately, running Prettier with ESLint will treat format issues just like other issues. However, these issues are always fixable, which is equivalent to formatting the code. </p>
<p>That's how <code>eslint-plugin-prettier</code> works. It runs Prettier, as a rule, behind the scene and compares the code before and after being run through Prettier. Finally, it reports differences as individual ESLint issues. To fix these issues, the plugin simply uses the formatted code from Prettier.</p>
<p>To have this integration, we need to install both <code>prettier</code> and <code>eslint-plugin-prettier</code>. <code>eslint-plugin-prettier</code> also comes with <code>eslint-plugin-prettier:recommended</code> config—which extends <a target="_blank" href="https://github.com/prettier/eslint-config-prettier">eslint-config-prettier</a>. Therefore we also need to install <code>eslint-config-prettier</code> to use it.</p>
<pre><code class="lang-javascript">{
  <span class="hljs-string">"plugins"</span>: [<span class="hljs-string">"prettier"</span>],
  <span class="hljs-string">"extends"</span>: <span class="hljs-string">"plugin:prettier/recommended"</span>
}
</code></pre>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Code linters or formatters have become the de facto standard in software development in general, and ESLint, specifically, in front-end development. </p>
<p>Its benefits go far beyond what it does technically, as it helps developers focus on more important matters. Thanks to delegating code styling to a machine, we can avoid opinionated styles on code review, and use that time instead for more meaningful code review. Code quality also benefits, and we get more consistent and less error-prone code.</p>
<p><em>This article was originally posted at <a target="_blank" href="https://blog.vinhis.me/2019/08/03/the-essentials-of-essential-frontend-tools-eslint.html">my blog</a></em>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to integrate Prettier with ESLint and stylelint ]]>
                </title>
                <description>
                    <![CDATA[ By Abhishek Jain or How to never worry about code styling again _Photo by [Unsplash](https://unsplash.com/photos/bJjsKbToY34?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText" rel="noopener" target="_blank" title="">NordWood Themes ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/integrating-prettier-with-eslint-and-stylelint-99e74fede33f/</link>
                <guid isPermaLink="false">66c3581f71e87702d4e5b6de</guid>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Prettier ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 30 Jul 2018 06:23:11 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*4rTkzZ0psMNKu9RgpEfQmg.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Abhishek Jain</p>
<h4 id="heading-or-how-to-never-worry-about-code-styling-again">or How to never worry about code styling again</h4>
<p><img src="https://cdn-media-1.freecodecamp.org/images/2NnxX8zdoJFw9uQM9ez2epHwX3Z26IQQjmt-" alt="Image" width="600" height="400" loading="lazy">
_Photo by [Unsplash](https://unsplash.com/photos/bJjsKbToY34?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" rel="noopener" target="_blank" title=""&gt;NordWood Themes on &lt;a href="https://unsplash.com/search/photos/computer?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" rel="noopener" target="<em>blank" title=")</em></p>
<p><a target="_blank" href="https://github.com/eslint/eslint">ESLint</a> and <a target="_blank" href="https://github.com/stylelint/stylelint">stylelint</a> are really amazing tools that allow you to enforce coding patterns among your teams. This has many benefits, like outputting better and more consistent code, getting rid of useless diffs in commits (newline, indentation, et al.) among many others.</p>
<p>But over time, this can prove to be a bit of a hassle among the developers of a team, who find it an extra mental burden to manually add semicolons, newlines, indentations, etc just to conform to the lint rules. This is where a code formatting tool like <a target="_blank" href="https://github.com/prettier/prettier">Prettier</a> comes in.</p>
<p>Prettier can be set up to automatically format your code according to some specified rules. If you are using VSCode, you can even have your code be formatted whenever you hit save (I’m sure there must ways to set this up in other editors but I haven’t looked into it.)</p>
<p>However, you don’t want to create a new Prettier config file, since you already have all the formatting related rules specified in your ESLint and stylelint config files. So, we will need some magic for that. ✨</p>
<p>Let’s now dive into a step by step of how to set this all this up, and also how to format all of your existing code according to the lint rules. This guide assumes that your project already has ESLint and stylelint set up with their <code>.eslintrc</code> and <code>.stylelintrc</code> files.</p>
<h3 id="heading-part-1-formatting-the-existing-codebase">PART 1: Formatting the existing codebase</h3>
<h4 id="heading-step-1"><strong>Step 1</strong></h4>
<p>Install <a target="_blank" href="https://github.com/prettier/prettier-eslint">prettier-eslint</a>, which is a tool that formats your JavaScript using Prettier followed by <code>eslint --fix</code>. The <code>--fix</code> is an ESLint feature that attempts to automatically fix some problems for you.</p>
<pre><code>npm install --save-dev prettier-eslint
</code></pre><p>This tool infers the equivalent Prettier config options from your existing <em>.eslintrc</em> file. So you shouldn’t need to create a new <em>.prettierrc</em> file in most cases.</p>
<h4 id="heading-step-2"><strong>Step 2</strong></h4>
<p>Install <a target="_blank" href="https://github.com/prettier/prettier-eslint-cli">prettier-eslint-cli</a>. This is the CLI tool that’ll help you run all of your files through prettier-eslint at once.</p>
<pre><code>npm install --save-dev prettier-eslint-cli
</code></pre><h4 id="heading-step-3"><strong>Step 3</strong></h4>
<p>Install <a target="_blank" href="https://github.com/hugomrdias/prettier-stylelint">prettier-stylelint</a>, which is a tool that formats your CSS/SCSS with Prettier followed by <code>stylelint —-fix</code>. Like ESLint, <code>--fix</code> is a stylelint feature that attempts to automatically fix some problems for you.</p>
<pre><code>npm install prettier-stylelint --save-dev
</code></pre><p>This tool <em>also</em> attempts to create a Prettier config based on the stylelint config.</p>
<p>Note that unlike prettier-eslint, you don’t have to install another package for its CLI since that is already included in it.</p>
<h4 id="heading-step-4"><strong>Step 4</strong></h4>
<p>Write scripts inside your <code>package.json</code> targeting the existing files in your codebase that you wish to run through prettier-eslint and prettier-stylelint.</p>
<pre><code><span class="hljs-string">"scripts"</span>: {
</code></pre><pre><code>  <span class="hljs-string">"fix-code"</span>: <span class="hljs-string">"prettier-eslint --write 'src/**/*.{js,jsx}' "</span>,
</code></pre><pre><code>  <span class="hljs-string">"fix-styles"</span>: <span class="hljs-string">"prettier-stylelint --write 'src/**/*.{css,scss}' "</span>
</code></pre><pre><code>}
</code></pre><p>As you can see, I am targeting all of my existing JS and JSX and all of my existing CSS and SCSS, respectively.</p>
<p>The <code>--write</code> flag writes the changes in-place for the file currently being formatted. So, be careful and <strong>make sure that all of your existing files are under source control and that there are no uncommited changed</strong>.</p>
<h4 id="heading-step-5"><strong>Step 5</strong></h4>
<p>Run the scripts!</p>
<pre><code>npm run fix-codenpm run fix-styles
</code></pre><p>Now, you can check-in all of these new changes as a single big commit (maybe even from a temporary git user, if you don’t wanna pollute your own git history.)</p>
<h3 id="heading-part-2-setting-up-vscode"><strong>Part 2: Setting up VSCode</strong></h3>
<p>Now that your existing codebase is formatted, its time to make sure that all the code being written henceforth is formatted automatically.</p>
<h4 id="heading-step-1-1"><strong>Step 1</strong></h4>
<p>Install the Prettier, ESLint, and stylelint extensions for VSCode:</p>
<p><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode"><strong>Prettier - Code formatter - Visual Studio Marketplace</strong></a><br><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode">_Extension for Visual Studio Code - VS Code plugin for prettier/prettier_marketplace.visualstudio.com</a><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint"><strong>ESLint - Visual Studio Marketplace</strong></a><br><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">_Extension for Visual Studio Code - Integrates ESLint JavaScript into VS Code._marketplace.visualstudio.com</a><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=shinnn.stylelint"><strong>stylelint - Visual Studio Marketplace</strong></a><br><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=shinnn.stylelint">_Extension for Visual Studio Code - Modern CSS/SCSS/Less linter_marketplace.visualstudio.com</a></p>
<h4 id="heading-step-2-1"><strong>Step 2</strong></h4>
<p>Configure a few VSCode settings:</p>
<p><code>"prettier.eslintIntegration": true</code> — tells Prettier to use prettier-eslint instead of Prettier</p>
<p><code>"prettier.stylelintIntegration": true</code> — tells Prettier to use prettier-stylelint instead of Prettier</p>
<p><code>"eslint.autoFixOnSave": false</code> — we don’t need ESLint to fix our code for us directly, since prettier-eslint will be running <code>eslint --fix</code> for us anyways.</p>
<p><code>"editor.formatOnSave": true</code> — runs Prettier with the above options on every file save, so you never have to manually invoke VSCode’s format command.</p>
<p>Additionally, you can check-in the above workplace settings to source control so its easier for other team members to set up their editors. You can do so by creating a <code>.vscode</code> folder at the root of your project and putting all of the above rules in a <code>settings.json</code> file.</p>
<p>Optionally, you can tell Prettier to ignore formatting certain patterns of files. To do this, just add a <code>.prettierignore</code> file to the root of your project specifying the paths to ignore. For instance:</p>
<pre><code>strings.jsonscripts<span class="hljs-comment">/*</span>
</code></pre><p><strong>And that’s it! Never worry about code styling again ?</strong></p>
<p>This article is by no means intended to be an exhaustive guide, but rather an introduction to what is possible with the amazing tools mentioned herein. I recommend opening up the official GitHub pages for each to learn more about how to utilise these tools more effectively for your specific workflow.</p>
<p>Please write a comment below for any help, suggestion, etc.</p>
<h4 id="heading-references"><em>References</em></h4>
<p>https://prettier.io/docs/en/<br>https://stylelint.io/user-guide/<br>https://eslint.org/<br>https://github.com/prettier/prettier-vscode<br>https://github.com/prettier/prettier-eslint<br>https://github.com/prettier/prettier-eslint-cli<br>https://github.com/hugomrdias/prettier-stylelint<br>https://www.youtube.com/watch?v=YIvjKId9m2c</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The Most Important ESLint Rule for Redux Applications ]]>
                </title>
                <description>
                    <![CDATA[ By Paul Matthew Jaworski tl;dr Run yarn add --dev eslint-plugin-import and add "import/named": 2 to your .eslintrc rules to prevent accidentally importing constants that don’t exist to your reducers. If you’re developing an application using React an... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-most-important-eslint-rule-for-redux-applications-c10f6aeff61d/</link>
                <guid isPermaLink="false">66c361c50cede4e9b1329ce0</guid>
                
                    <category>
                        <![CDATA[ ES6 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ eslint ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Redux ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 26 Oct 2016 03:18:50 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9cb751740569d1a4cae3b5.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Paul Matthew Jaworski</p>
<p><strong>tl;dr</strong> Run <code>yarn add --dev eslint-plugin-import</code> and add <code>"import/named": 2</code> to your <code>.eslintrc</code> rules to prevent accidentally importing constants that don’t exist to your reducers.</p>
<p>If you’re developing an application using <a target="_blank" href="https://facebook.github.io/react/">React</a> and <a target="_blank" href="http://redux.js.org/">Redux</a>, your reducers probably look something like this:</p>
<p>This example is pretty straight-forward. You’re only importing one constant up top.</p>
<p>Your file structure currently looks like this:</p>
<pre><code>.├── actions|   ├── constants.js|   └── index.js...omitted <span class="hljs-keyword">for</span> brevity...├── reducers|   ├── accountReducer.js|   └── index.js...omitted <span class="hljs-keyword">for</span> brevity...├── indes.js└── index.html
</code></pre><p>But as your codebase grows, your reducers will become more complicated. Organizing your files by type may no longer makes sense. So you decide to start organizing things by feature or route instead:</p>
<pre><code>.├── actions|   ├── constants.js|   └── index.js...omitted <span class="hljs-keyword">for</span> brevity...├── reducers|   └── index.js├── routes|   ├── accounts|   |   ├── components|   |   ├── containers|   |   ├── <span class="hljs-built_in">module</span>|   |   |   ├── actions.js|   |   |   ├── constants.js|   |   |   └── index.js (<span class="hljs-built_in">exports</span> our reducer)|   |   ├── styles|   |   └── index.js|   └── index.js...omitted <span class="hljs-keyword">for</span> brevity...├── indes.js└── index.html
</code></pre><p>Awesome! Now you don’t have 100 components in our main components folder anymore. Things are a bit neater, and easier to reason about.</p>
<p>There’s a problem with your refactor, though. Suddenly this <code>import</code> in your reducer is now referring to a non-existent path:</p>
<pre><code><span class="hljs-keyword">import</span> { RECEIVE_ACCOUNT_SUCCESS } <span class="hljs-keyword">from</span> <span class="hljs-string">'../actions/constants'</span>;
</code></pre><p>You get an error about that path being unresolved immediately, so you change it:</p>
<pre><code><span class="hljs-keyword">import</span> { RECEIVE_ACCOUNT_SUCCESS } <span class="hljs-keyword">from</span> <span class="hljs-string">'./constants'</span>;
</code></pre><p>Cool. But what if you didn’t actually define that constant in your new file?</p>
<p>Now you’re about to experience one of the most frustrating bugs possible in a Redux app — importing an undefined constant into a reducer. Your tests will break, your app will break, and you’ll bash your head into your desk until you figure it out.</p>
<p>The problem is that this type of bug just fails silently. ES6 imports don’t care whether or not the variable you’re importing is defined. You’ll never see an error about it.</p>
<h3 id="heading-eslint-to-the-rescue"><strong>ESLint to the Rescue!</strong></h3>
<p>The key to avoiding this type of bug is to installing <code>eslint-plugin-import</code>, then set one simple little rule in your <code>.eslintrc</code>:</p>
<pre><code><span class="hljs-string">"import/named"</span>: <span class="hljs-number">2</span>
</code></pre><p>That’s it! Now you’ll get an error anytime you try to import an undefined constant into one of your reducers.</p>
<p>Edit: Unless you’re extending a base config that already includes it, you’ll also need to add <code>"import"</code> to the plugins section of your <code>.eslintrc</code>. Thanks to <a target="_blank" href="https://www.freecodecamp.org/news/the-most-important-eslint-rule-for-redux-applications-c10f6aeff61d/undefined">Dave Mac</a> for pointing that out!</p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
