简体   繁体   English

如何通过 Eslint 设置更漂亮的自动格式化代码?

[英]How to set up prettier to auto-format code through Eslint?

In order to focus our development time on the actual code instead of arguing about coding style and careless mistake, we are trying to configure our Angular project using ESLint and Prettier.为了将我们的开发时间集中在实际代码上,而不是争论编码风格和粗心的错误,我们正在尝试使用 ESLint 和 Prettier 配置我们的 Angular 项目。 Based on https://github.com/angular-eslint/angular-eslint#notes-for-eslint-plugin-prettier-users , *.ts and .html can be configured by setting:基于https://github.com/angular-eslint/angular-eslint#notes-for-eslint-plugin-prettier-users*.ts.html可以通过设置进行配置:

.eslintrc.json .eslintrc.json

{
  "root": true,
  "ignorePatterns": ["projects/**/*"],
  "overrides": [
    {
      "files": ["*.ts"],
      "parserOptions": {
        "project": ["tsconfig.json", "e2e/tsconfig.json"],
        "createDefaultProgram": true
      },
      "extends": [
        "plugin:@angular-eslint/recommended",
        "plugin:@angular-eslint/template/process-inline-templates",
        "plugin:prettier/recommended"
      ],
      "rules": {}
    },

    // NOTE: WE ARE NOT APPLYING PRETTIER IN THIS OVERRIDE, ONLY @ANGULAR-ESLINT/TEMPLATE
    {
      "files": ["*.html"],
      "extends": ["plugin:@angular-eslint/template/recommended"],
      "rules": {}
    },

    // NOTE: WE ARE NOT APPLYING @ANGULAR-ESLINT/TEMPLATE IN THIS OVERRIDE, ONLY PRETTIER
    {
      "files": ["*.html"],
      "excludedFiles": ["*inline-template-*.component.html"],
      "extends": ["plugin:prettier/recommended"],
      "rules": {
        // NOTE: WE ARE OVERRIDING THE DEFAULT CONFIG TO ALWAYS SET THE PARSER TO ANGULAR (SEE BELOW)
        "prettier/prettier": ["error", { "parser": "angular" }]
      }
    }
}

However, there are other file types in the project besides *.ts and *.html , eg *.js , *.json , *.yaml ...etc.但是,除了*.ts*.html之外,项目中还有其他文件类型,例如*.js*.json*.yaml ...等。

Question : How can I use the similar approach to make Prettier auto-format other file types when I run ng lint ?问题:当我运行ng lint时,如何使用类似的方法使 Prettier 自动格式化其他文件类型? Here is what I have added to .eslintrc.json but running ng lint has no effect.这是我添加到.eslintrc.json但运行ng lint没有效果。 Note, I have added the file types in angular.json注意,我在angular.json中添加了文件类型

More items in.eslintrc.json更多项目在.eslintrc.json

   // JSON: https://www.npmjs.com/package/eslint-plugin-json
    {
      "files": ["*.json"],
      "extends": [
        "plugin:json/recommended-with-comments",
        "plugin:prettier/recommended"
      ]
    },

    // JavaScript
    {
      "files": ["*.js"],
      "excludedFiles": ["karma.conf.js"], // out-of-the-box karma.conf.js has known lint errors
      "extends": ["eslint:recommended", "plugin:prettier/recommended"]
    },

    // YAML
    {
      "files": ["*.yaml", "*.yml"],
      "plugins": ["yaml"],
      "extends": ["plugin:yaml/recommended", "plugin:prettier/recommended"]
    },

    // Markdown
    {
      "files": ["*.md"],
      "plugins": ["markdown"],
      "extends": ["plugin:markdown/recommended", "plugin:prettier/recommended"]
    }

angular.json angular.json

       "lint": {
          "builder": "@angular-eslint/builder:lint",
          "options": {
            "lintFilePatterns": [
              "src/**/*.ts",
              "src/**/*.html",
              "**/*.json",
              "**/*.js",
              "**/*.yaml",
              "**/*.md"
            ]
          }

This is the initial setup I use for NG repositories.这是我用于 NG 存储库的初始设置。 You're welcome to reuse it and extend it with additional rules.欢迎您重用它并使用其他规则对其进行扩展。 It allows to auto format and --fix on each save.它允许在每次保存时自动格式化和 --fix。

.eslintrc: .eslintrc:

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint",
    "prettier"
  ],
  "ignorePatterns": [
    "projects/**/*"
  ],
  "overrides": [
    {
      "files": [
        "*.ts"
      ],
      "parserOptions": {
        "project": [
          "tsconfig.json"
        ],
        "createDefaultProgram": true
      },
      "extends": [
        "eslint:recommended",
        "plugin:@angular-eslint/recommended",
        "plugin:@angular-eslint/template/process-inline-templates",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:prettier/recommended"
      ],
      "rules": {
        "@angular-eslint/directive-selector": [
          "error",
          {
            "type": "attribute",
            "prefix": "lib",
            "style": "camelCase"
          }
        ],
        "@angular-eslint/component-selector": [
          "error",
          {
            "type": "element",
            "prefix": "lib",
            "style": "kebab-case"
          }
        ],
        "prettier/prettier": [
          "error",
          {
            "arrowParens": "always",
            "bracketSameLine": true,
            "bracketSpacing": true,
            "printWidth": 120,
            "semi": true,
            "singleQuote": true,
            "tabWidth": 2,
            "trailingComma": "es5",
            "endOfLine": "auto",
            "useTabs": false
          }
        ]
      }
    },
    {
      "files": [
        "*.html"
      ],
      "extends": [
        "plugin:@angular-eslint/template/recommended"
      ],
      "rules": {}
    }
  ]
}

(For VSCode) settings.json: (对于 VSCode)设置。json:

{
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "editor.defaultFormatter": "dbaeumer.vscode-eslint",
  "editor.formatOnSave": true,
  "editor.rulers": [
    120
  ],
  "javascript.format.insertSpaceAfterOpeningAndBeforeClosingEmptyBraces": true,
  "typescript.format.insertSpaceAfterOpeningAndBeforeClosingEmptyBraces": true
}

.editorconfig: .editorconfig:

root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.ts]
quote_type = single

[*.md]
max_line_length = off
trim_trailing_whitespace = false

Nevertheless, if you have Prettier plugin enabled in VSCode it might format differently to eslint local config.不过,如果你在 VSCode 中启用了 Prettier 插件,它的格式可能与 eslint 本地配置不同。 To solve it - go to "Extensions", find Prettier and select Disable(Workspace)要解决它 - go 到“扩展”,找到 Prettier 和 select 禁用(工作区)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在摩纳哥编辑器中自动格式化 XML - Auto-format XML in Monaco Editor VSCode (Angular) - EsLint Prettier 问题 - VSCode (Angular) - EsLint Prettier issue 如何在 Angular 中正确使用 Eslint 规则 newline-per-chained-call 和 Prettier - How to correctly use Eslint rule newline-per-chained-call with Prettier in Angular 有没有办法在 Angular 项目中列出所有活动的 ESLint / Prettier 规则? - Is there a way to list all active ESLint / Prettier rules in an Angular project? 如何在带有角度的 dotnet 核心的 Visual Studio 代码中设置调试器 - how to set up debugger in visual studio code for dotnet core with angular 如何使用Angular 2 CLI设置Istanbul代码覆盖率? - How to set up Istanbul code coverage with Angular 2 CLI? 如何将 typescript-eslint 规则添加到 eslint - How to add typescript-eslint rules to eslint 如何在 Visual Studio Code 中设置 Angular 应用程序路径以导入没有前缀的组件? - How to set up Angular app path to import components without prefix in Visual Studio Code? 如何使用 Angular 和 Flutter(均为 Dart)为工作区设置 VS Code 调试? - How do I set up VS Code debugging for a workspace using Angular and Flutter (both Dart)? 我应该如何在 Visual Studio 代码中为 node.js 设置 angular - How should I set up angular for node.js in visual studio code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM