![](/img/trans.png)
[英]Prettier CLI and VS Code Extension formatting anonymous function differently
[英]Code formatting with prettier is not working in nest js
我正在使用 Visual Studio 代碼。 在我的 Nest JS 項目中,代碼沒有根據更漂亮的規則進行格式化。 我已經設置.prettierrc
和.eslintrc
。 我還從settings.json
文件中設置了formatOnSave: true
。
我的settings.json文件的一部分
"editor.formatOnType": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
我的.eslintrc文件 -
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off'
},
};
最后是我的.prettierrc文件 -
{
"useTabs": true,
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"jsxSingleQuote": false,
"trailingComma": "all",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "avoid",
"importOrder": ["^[./]"],
"importOrderSortSpecifiers": true,
"importOrderSeparation": true
}
你能告訴我如何正確格式化代碼嗎?
在我的情況下,.prettierrc 文件不在項目的根目錄中,VS Code 找不到它。 (我只需要更改此文件的路徑或將其移動到項目的根目錄)
prettier 會跳過格式化文件的一個原因是文件中存在語法錯誤。 如果 prettier 無法解析它,它會在謹慎方面犯錯並且不會觸及它。
對我來說,問題是我的文件的語言實際上是“[typescriptreact]”,必須將其作為其中一種語言包括在內,它是 defaultFormatter
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.