Tips for Configuring Code Formatting with VScode's ESlint Plugin
Instead of focusing on how to utilize VScode's Prettier extension, let's delve into the process of integrating ESlint for code correctness checks and Prettier for formatting.
What are the Benefits of this Approach?
- Allows flexibility for team members who may prefer different code editors over VScode, promoting a tool-agnostic environment.
- Prioritizes code linting over formatting, but ensures that both aspects can coexist without conflicts.
- Reduces strain on hardware by minimizing the number of extensions running simultaneously.
- Streamlines configuration management by eliminating the need for separate personal configurations outside of the codebase.
- Enables automated ESlint checks before commits or in CI/CD pipelines.
How to Set up this Configuration?
To begin, install the ESlint extension exclusively, without adding the Prettier extension.
https://i.sstatic.net/0F3N6.png
Haven't Installed Vetur Yet?
If working with Vue2 apps (such as Nuxt), consider installing Vetur for simplified ESlint (+Prettier) integration with .vue files.
Once installed, access the Command Palette using ctrl + shift + p
(Windows/Linux) or cmd + shift + p
(Mac), then search for
Preferences: Open Default Settings (JSON)
.
https://i.sstatic.net/TP7oY.png
Your configuration should resemble the following:
{
"workbench.colorTheme": "Solarized Dark",
"editor.codeActionsOnSave": {
"source.fixAll": true,
},
"eslint.options": {
"extensions": [
".html",
".js",
".vue",
".jsx",
]
},
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"html",
"vue",
],
}
Testing Your Configuration
To verify the setup, download the example Github repo, install Node.js version 14, and run yarn
. Open VScode to ensure compatibility.
You can explore potential issues in .js or .vue files by checking the Problems view from the Command Palette (Problems: Focus on Problems View
). Notable examples include nuxt.config.js and /pages/index.vue.
https://i.sstatic.net/2oDyc.png
Address any fixable concerns, which could involve both Prettier formatting issues and ESlint errors based on specific rules like vue/require-v-for-key.
If you desire inline ESlint warnings/errors like those shown above, consider installing Error Lens.
Saving the file triggers automatic fixes for identified issues, aligning with both Prettier formatting and Nuxt-defined ESlint standards.
Congratulations, your setup is now operational! If not, refer to troubleshooting steps at the end of the response.
Creating a New Project?
For new projects, use
npx create-nuxt-app my-super-awesome-project
and choose "Linting tools: Eslint + Prettier" during setup.
https://i.sstatic.net/r5Qtc.png
Note: Refer to an additional step mentioned in this Github issue for current installations until a future fix is released.
To rectify, execute
yarn add -D eslint-plugin-prettier
and confirm your .eslintrc.js file matches the updated structure provided.
module.exports = {
root: true,
env: {
browser: true,
node: true
},
parserOptions: {
parser: '@babel/eslint-parser',
requireConfigFile: false
},
extends: [
'@nuxtjs',
'plugin:prettier/recommended',
'prettier'
],
plugins: [
],
rules: {}
}
Following these adjustments, your setup should seamlessly integrate ESlint and Prettier functionalities. Save the changes to trigger sequential ESlint and Prettier checks.
Troubleshooting Tips
- Restart the ESLint server or reload the window via the Command Palette if encountering issues.
- Feel free to reach out or seek assistance through contact options if further help is needed.