[英]Prettier and eslint indents not working together
我托盤設置了我的 vim 基於 typescript 的開發環境,但是縮進管理有問題。
可能 'eslint' 說: indent: Expected indentation of 2 spaces but found 4.
在prettier
的重新格式化后發現 4 個空格。
我的.eslintrc.js
:
module.exports = {
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
'plugin:react/recommended', // Uses the recommended rules from @eslint-plugin-react
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from @typescript-eslint/eslint-plugin
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
parserOptions: {
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: 'module', // Allows for the use of imports
ecmaFeatures: {
jsx: true, // Allows for the parsing of JSX
tsx: true, // Allows for the parsing of TSX ???
},
},
rules: {
indent: ['error', 2],
quotes: ['error', 'single'],
semi: ['error', 'never'],
'sort-keys': ['error', 'asc', { caseSensitive: true, natural: false }],
},
}
我的.prettierc
:
module.exports = {
semi: false,
trailingComma: 'all',
singleQuote: true,
printWidth: 80,
tabWidth: 2,
};
根據此 Kai Cataldo 對此GitHub 問題的評論:
ESLint 的縮進規則和 Prettier 的縮進樣式不匹配——它們是完全獨立的實現,是解決同一問題的兩種不同方法(“我們如何在項目中強制執行一致的縮進”)。
因此,在使用prettier
時,最好禁用 eslint 的indent
規則。 保證他們會發生沖突。
在eslintrc
添加indent: [2, 2, { SwitchCase: 1}]
定義的參數
新的 eslint 規則希望第一個參數是一個數字: Severity should be one of the following: 0 = off, 1 = warn, 2 = error
。
縮進量
該對象說明如何在此處的選項之后縮進switch
和case
語句。
這應該修復它https://github.com/prettier/eslint-config-prettier
它禁用 eslint 中與 prettier 沖突的規則
關閉默認的 Visual Studio Code 解析器並只保留 eslint 解析器為我修復了它。
只需轉到設置Ctrl/Cmd + ,
,選擇User
(全局設置)或Workspace
(僅適用於工作存儲庫),然后在右上角單擊帶有旋轉箭頭的紙張。 這將打開 json 文件中聲明的設置。 使用以下設置,它應該適用於任何情況:
{
// other settings
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.organizeImports": false
},
// other settings
}
通常在 Visual Studio Code 窗口的底部有一個Fix on save: X
標志。 這應該與此設置相關聯,因此請確保保持一致。
最煩人的事情.. 如此固定: eslint-config-prettier
{
"rules": {
"no-tabs": ["error", {"allowIndentationTabs": true}]
}
}
我遇到了同樣的問題。 問題是您可以手動覆蓋任何沖突規則。 就我而言,它是 ESLint 的prettier/prettier
的插件,因此可以在所需插件下添加縮進規則來解決。
"rules": {
// "indent":["error",10]
"prettier/prettier":[ //or whatever plugin that is causing the clash
"error",
{
"tabWidth":4
}
]
}
您可以像這樣覆蓋特定規則,以消除任何沖突。
npm i --save-dev eslint-config-prettier
npm i --save-dev eslint-plugin-prettier
//eslintrc.js
{
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest",
"plugin:prettier/recommended"
]
}
在我看來,唯一的沖突是三元運算。 解決此問題的另一種選擇是使用以下 eslint 規則。
"indent": ["error", 2, { "offsetTernaryExpressions": true }],
這里有更多的選擇: https://eslint.org/docs/latest/rules/indent#offsetternaryexpressions
我發現 eslint 的縮進規則更易於配置,並且會在 prettier 上使用它們。
我遇到了這個問題,通過在設置菜單(ctrl + shift + P)中將 Prettier 更改為使用選項卡,它為我解決了這個問題。
就我而言,我只是在 VSCode 上將CRLF (回車,換行)更改為LF (換行)
因此, npm i --save-dev eslint-config-prettier
解決了Akshay指出的問題
有同樣的問題,不使用eslint,解決如下:
npm i tslint-config-prettier --save-dev
tslint.config
:如果尚未添加,請添加:
"extends": ["tslint:recommended", "tslint-config-prettier"],
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.