DEV Community

Cover image for The Power Pair: ESLint and Prettier—Masters of Quality and Style!✅🎨
ashishreddy.K
ashishreddy.K

Posted on

The Power Pair: ESLint and Prettier—Masters of Quality and Style!✅🎨

Welcome to the exciting world of coding in the AI era! 🚀 In this age of automation, many aspects of software development have become a breeze. But hold on—while we’re riding this wave of ease, it’s super easy to overlook some of the foundational gems like linting and formatting. Enter our heroes: ESLint and Prettier! 🤖💻

Recently, while chatting with some friends, I noticed a common mix-up between these two powerful tools. 🤔 Let’s unravel the mystery together and discover how they complement each other like peanut butter and jelly! 🍇🥜✨

Linting vs. Formatting: The Coding Showdown! 🎉

Let’s dive into the thrilling world of linting and formatting—the unsung heroes of clean code! 🌟

**Linting **is like having a personal coach for your code, checking for mistakes, syntax errors, and patterns that could lead to bugs. It’s all about enforcing rules to keep your code pristine and consistent.

Imagine it as your trusty spell checker, swooping in to catch those sneaky grammar blunders and typos. 🦸‍♂️ Just as you wouldn’t want a paper filled with errors, you definitely don’t want your code looking messy! Linting ensures everything is neat, tidy, and ready for the spotlight! 📝✅

Now, let’s talk about formatting! This is where the magic of style comes in! ✨ Formatting organizes and styles your code for readability and consistency, focusing on indentation, spacing, and line breaks without altering the code's functionality.

Think of formatting as your code’s personal stylist! 💇‍♀️ Imagine an answer sheet in an exam: even if your answers are perfect, messy handwriting or poor spacing can make them hard to read. Proper formatting is like putting on your best outfit—it keeps your code clean and polished, making it easier for others (and future you!) to understand. ✏️📄

So there you have it! Linting catches the bugs, while formatting makes everything shine. Together, they make your code a masterpiece! 🎨💻

Meet ESLint: Your Code’s Super Sidekick! 🦸‍♂️

ESLint is like having a wise mentor for your JavaScript and TypeScript code! 🎓 It helps you:

  • Detect Potential Bugs: Think of ESLint as your bug-hunting buddy, ready to sniff out pesky bugs before they cause chaos! 🐞

  • Enforce Consistent Coding Styles: It ensures your code has a polished look by enforcing naming conventions and syntax choices. 🎨✨

  • Promote Best Practices: Say goodbye to spaghetti code! ESLint encourages best practices in code organization. 📚

Enter Prettier: The Styling Guru! 💁‍♀️

Now, let’s welcome Prettier, the ultimate code formatter! 🌟 This tool takes the hassle out of styling, automatically adjusting your code for a consistent visual appeal:

  • Indentation, Spacing, and Line Breaks: Prettier ensures your code is perfectly indented and spaced. ✨

  • Consistent Formatting Rules: Whether you love single or double quotes, Prettier keeps your style sharp and professional! 🗒️🎉

Setting Up ESLint: Let’s Get Linting! 🎉

Configuring ESLint is a piece of cake! 🍰 Just run this command in your terminal to kick things off:

npm init @eslint/config@latest
Enter fullscreen mode Exit fullscreen mode

This magical command will guide you through a series of prompts, helping you tailor ESLint to fit your coding needs like a glove! 🧤 Once you’re done, you’ll have a shiny eslint.config.js file that looks something like this:

import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

export default [
  {files: ["**/*.{js,mjs,cjs,ts}"]},
  {languageOptions: { globals: globals.browser }},
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
];
Enter fullscreen mode Exit fullscreen mode

Next up, let’s make linting super easy by adding a script to your package.json! Just hop into the "scripts" section and add this line:

"scripts": {
  "lint": "eslint ."
}
Enter fullscreen mode Exit fullscreen mode

And voila! Now you can lint your code by simply running:

npm run lint
Enter fullscreen mode Exit fullscreen mode

With that, you’re all set to keep your code clean, consistent, and ready to shine! 🌟🙌

Setting Up Prettier: Let’s Get Pretty

Getting started with Prettier is a total breeze! 🌬️ First, let’s add it to your project with a snap of your fingers! Just run this command:

npm install --save-dev prettier
Enter fullscreen mode Exit fullscreen mode

Next up, we need to create a file named .prettierrc in your project. This is where the magic happens, and you can customize it to your heart’s content! 💖 Here’s a quick example to get you started:

{
  "singleQuote": true,
  "trailingComma": "all",
  "printWidth": 80
}
Enter fullscreen mode Exit fullscreen mode

Now, let’s make formatting a piece of cake! 🎂 Head over to your package.json and add a formatting script in the "scripts" section:

"scripts": {
  "format": "prettier --write ."
}
Enter fullscreen mode Exit fullscreen mode

And ta-da! Your code is now perfectly polished, beautifully formatted, and ready to dazzle! 🌟🎉

The Epic Showdown: ESLint vs. Prettier! ⚔️✨

Let’s dive into the key differences between our two coding champions, ESLint and Prettier! 🏆

Feature ESLint Prettier
Primary Function 🕵️‍♂️ Linting and code quality checks 🎨Code formatting
Focus 🔍 Identifying errors and enforcing best practices ✨ Ensuring consistent style
Configurability 🛠️ Highly customizable with rules 🖌️Less customizable; opinionated
Integration 🤝Can be used standalone or with a formatter 🌈 Often used in conjunction with a linter
Types of Issues ⚠️ Syntax errors, potential bugs, stylistic issues 📏 Formatting inconsistencies

Wrapping It Up! 🎉
So there you have it! With ESLint catching those pesky bugs and enforcing best practices, and Prettier giving your code a sleek, polished look, you’re armed with the dynamic duo of coding excellence! 💪✨

Remember, a clean and consistent codebase not only makes you look like a pro but also makes collaboration a breeze! 🌈 So why not share this knowledge with your fellow coders? Like and share this post to spread the love for clean code! ❤️

Keep coding and keep shining! 🌟🚀

Top comments (0)