簡體   English   中英

使用更漂亮的代碼格式在 nest js 中不起作用

[英]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
}

你能告訴我如何正確格式化代碼嗎?

  1. 確保在 VS Code 中啟用了 prettier
  2. 檢查“prettier”日志(打開終端,select“輸出”選項卡。接下來需要在列表中找到 Prettier)

在我的情況下,.prettierrc 文件不在項目的根目錄中,VS Code 找不到它。 (我只需要更改此文件的路徑或將其移動到項目的根目錄)

prettier 會跳過格式化文件的一個原因是文件中存在語法錯誤。 如果 prettier 無法解析它,它會在謹慎方面犯錯並且不會觸及它。

對我來說,問題是我的文件的語言實際上是“[typescriptreact]”,必須將其作為其中一種語言包括在內,它是 defaultFormatter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM