Need advice about which tool to choose?Ask the StackShare community!
ESLint vs Prettier: What are the differences?
ESLint and Prettier are popular tools in the JavaScript ecosystem, used for maintaining code quality and enforcing coding standards. Let's explore the key difference between them.
Parsing and Analysis: ESLint primarily focuses on analyzing and identifying problematic patterns or code that might have potential bugs or errors. It performs a thorough static analysis of JavaScript code to catch issues like unused variables, missing semicolons, and more. On the other hand, Prettier is primarily concerned with code formatting and styling. It automatically enforces a consistent code style by parsing the code and then reformatting it to follow predefined rules.
Configuration and Style Guide: ESLint offers a highly flexible and customizable configuration setup. It allows developers to define their own set of rules or use popular style guides like Airbnb, Google, or Standard. Additionally, ESLint offers options for enabling or disabling specific rules based on project preferences. In contrast, Prettier has a less flexible configuration. It follows a strict set of predefined rules for code styling and does not provide options for customization or style guide selection.
Formatting vs Linting: While both ESLint and Prettier enforce code quality, they have different focuses. ESLint is primarily concerned with linting, which involves catching bugs and finding code quality issues. It helps to identify potential errors and improve the overall code quality. On the other hand, Prettier focuses solely on code formatting. It does not perform any linting or static analysis, and its primary goal is to ensure consistent and aesthetically pleasing code formatting.
Fixing vs Rewriting Code: ESLint, being a linter, not only identifies code issues but also provides automatic fix suggestion for certain errors. It has the ability to automatically fix some problems by rewriting the code. This can be helpful in saving time and effort for developers. In contrast, Prettier does not offer automatic fix suggestions. Instead, it completely rewrites the code to match the predefined formatting rules, resulting in a consistent and formatted codebase.
Supported Languages: ESLint primarily focuses on JavaScript and provides extensive support for linting JavaScript code, including ECMAScript 6+ syntax and JSX in React. It also supports various plugins and configurations for other languages like TypeScript and Vue. On the other hand, Prettier supports multiple languages, including JavaScript, TypeScript, CSS, HTML, JSON, YAML, and more. It can be used as a code formatter for a wide range of file types.
Integration with Development Tools: ESLint integrates seamlessly with popular development tools and IDEs. It provides plugins for editors like Visual Studio Code, Sublime Text, and Atom, allowing real-time linting and quick error identification during development. Additionally, ESLint can be easily integrated into build tools or pre-commit hooks for enforcing code quality standards. On the other hand, Prettier also supports integration with various tools and editors but primarily focuses on providing a standalone code formatter.
In summary, ESLint is more focused on static analysis, customization, and linting JavaScript code, whereas Prettier is primarily concerned with code formatting and enforcing consistent style rules across multiple languages.
Scenario: I want to integrate Prettier in our code base which is currently using ESLint (for .js and .scss both). The project is using gulp.
It doesn't feel quite right to me to use ESLint, I wonder if it would be better to use Stylelint or Sass Lint instead.
I completed integrating ESLint + Prettier, Planning to do the same with [ Stylelint || Sasslint || EsLint] + Prettier.
And have gulp 'fix' on file save (Watcher).
Any recommendation is appreciated.
In the case of .js files I would recommend using both Eslint and Prettier.
You can set up Prettier as an Eslint rule using the following plugin:
https://github.com/prettier/eslint-plugin-prettier
And in order to avoid conflicts between Prettier and Eslint, you can use this config:
https://github.com/prettier/eslint-config-prettier
Which turns off all Eslint rules that are unnecessary or might conflict with Prettier.
you don't actually have to choose between these tools as they have vastly different purposes. i think its more a matter of understanding how to use them.
while eslint and stylelint are used to notify you about code quality issues, to guide you to write better code, prettier automatically handles code formatting (without notifying me). nothing else.
prettier and eslint both officially discourage using the eslint-plugin-prettier way, as these tools actually do very different things. autofixing with linters on watch isnt a great idea either. auto-fixing should only be done intentionally. you're not alone though, as a lot of devs set this up wrong.
i encourage you to think about what problem you're trying to solve and configure accordingly.
for my teams i set it up like this: - eslint, stylelint, prettier locally installed for cli use and ide support - eslint config prettier (code formatting rules are not eslints business, so dont warn me about it) - vscode workspace config: format on save - separate npm scripts for linting, and formatting - precommit hooks (husky)
so you can easily integrate with gulp. its just js after all ;)
Pura vida! Well, I had a similar issue and at the end I decided to use Stylelint + Prettier for that job, in our case, we wanted that our linting process includes the SCSS files and not only the JS file, base on that we concluded that using only ESLint to do both things wasn't the best option, so, we integrated prettier with Stylelint, and for that we used a neat plugin that allowed us to use Prettier inside Stylelint here is the link, https://github.com/prettier/stylelint-prettier#recommended-configuration, I hope that this can help you, hasta pronto!, :)
Pros of ESLint
- Consistent javascript - opinions don't matter anymore8
- Free6
- IDE Integration6
- Customizable4
- Focuses code review on quality not style2
- Broad ecosystem of support & users2
Pros of Prettier
- Customizable2
- Open Source1
- Atom/VSCode package1
- Follows the Ruby Style Guide by default1
- Runs offline1
- Completely free1