Reputation: 163
I am using Vue 3 and VS Code with Prettier as my code formatter, and I want the indentation to be 4 spaces instead of 2, and I've configured 4 spaces on the bottom right side of my VS Code. But still whenever I format my code it indents everything to 2 spaces. I've tried reinstalling Prettier and VS Code, but it's still the same!
Upvotes: 9
Views: 10533
Reputation: 61
You have 3 concurrent configuration to do in order to reduce conflict between IDE and prettier and eslint: in a nutshell prettier should be used to format (write the file correctly and nicely), eslint should tell you syntax errors and format best practices miss and VSCODE should display it all correctly.
open the command palette an type >Prettier: Create Configuration File if you dont have this command then you are missing the prettier extention
select your project root where your "nodes_modules" folder is stored
open the generated file (.prettierrc) and edit the two line as follow (set the value to your liking, i like those):
{
"tabWidth": 4,
"useTabs": true
}
module.exports = {
root: true,
env: {
browser: true,
es2021: true,
},
extends: [
'eslint:recommended',
'plugin:vue/vue3-essential',
'plugin:vue/vue3-strongly-recommended',
'plugin:vue/vue3-recommended',
'plugin:@typescript-eslint/recommended',
],
parser: 'vue-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser',
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['vue', '@typescript-eslint'],
rules: {
'vue/attribute-hyphenation': ['warn', 'never'], // i want order not tyrany
indent: 'off', // prettier does it
'no-tabs': ['off', { allowIndentationTabs: true }], // i want tabs
// TODO : Remove Multiword-line ESlint-config
'vue/multi-word-component-names': 'off', // im fighting with my co devs
'@typescript-eslint/no-unused-vars': 'off', // already detected by vscode and conflicts in some vue3 script setup composition api declaration cases, this rule is overriden for ts files
'@typescript-eslint/no-empty-interface': 'off', // already detected by vscode
'vue/no-v-html': 'error', // big NO : sensible to XSS
'vue/no-setup-props-destructure': 'off', // allowed in vue3 script setup composition api ...
'no-undef': 'off',
'vue/html-indent': 0,
'vue/singleline-html-element-content-newline': 0,
'vue/html-self-closing': 'off',
'vue/max-attributes-per-line': 'off',
},
overrides: [
{
files: ['*.ts'],
rules: {
'@typescript-eslint/no-unused-vars': 'error',
},
},
],
}
{
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.indentSize": "tabSize",
"editor.tabSize": 4,
"editor.detectIndentation": false
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.indentSize": "tabSize",
"editor.tabSize": 4,
"editor.detectIndentation": false
}
}
// yes it's the same content in both languages
Of course dont take this conf at face value and keep improving it for perf and correctness.
PS: some devDependencies you might need ( I use nuxt ) you'll have to sort your version on your own ;)
"devDependencies": {
"@nuxtjs/eslint-module": "^4.1.0",
"@typescript-eslint/eslint-plugin": "6.13.0",
"@typescript-eslint/parser": "6.13.0",
"@vue/eslint-config-prettier": "8.0.0",
"@vue/eslint-config-standard": "^8.0.1",
"@vue/eslint-config-typescript": "12.0.0",
"eslint-config-typescript": "^3.0.0",
"eslint-plugin-markdown": "^3.0.0",
"eslint-plugin-nuxt": "^4.0.0",
"eslint-plugin-prettier": "5.0.1",
"eslint-plugin-vue": "^9.14.0",
"prettier": "^3.1.0",
"typescript": "5.4.4",
}
Upvotes: 0