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