Prettier 没有根据我的 eslint 配置进行格式化

Prettier doesn't format based on my eslint config

我已经在 VSCode 上安装了 Eslint-Prettier 扩展。我已经有一个 .eslintrc.js 并且我的默认格式化程序 (Eslint-prettier) 没有根据我的 eslint 规则进行格式化。

例如:当我输入 npm 运行 eslint 时。 --fix 它删除了所有分号,但在我按下 CTRL + V 后,prettier 再次添加了分号..

在我格式化我的电脑之前一切正常。谁能帮帮我?

我的 .eslintrc.js 文件内容是

module.exports = {
  env: {
    node: true,
    es6: true,
    browser: true
  },
  parserOptions: {
    ecmaVersion: 6,
    sourceType: "module",
    ecmaFeatures: {
      jsx: true,
      modules: true,
      experimentalObjectRestSpread: true
    }
  },
  rules: {
    "no-console": "off",
    "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off",

    // Best Practices
    eqeqeq: "error",
    "no-invalid-this": "error",
    "no-return-assign": "error",
    "no-unused-expressions": ["error", { allowTernary: true }],
    "no-useless-concat": "error",
    "no-useless-return": "error",

    // Variable
    // 'init-declarations': 'error',
    "no-use-before-define": "error",

    // Stylistic Issues
    "array-bracket-newline": ["error", { multiline: true, minItems: null }],
    "array-bracket-spacing": "error",
    "brace-style": ["error", "1tbs", { allowSingleLine: true }],
    "block-spacing": "error",
    "comma-dangle": "error",
    "comma-spacing": "error",
    "comma-style": "error",
    "computed-property-spacing": "error",
    "func-call-spacing": "error",
    "implicit-arrow-linebreak": ["error", "beside"],
    // indent: ['error', 4],
    "keyword-spacing": "error",
    "multiline-ternary": ["error", "never"],
    // 'no-lonely-if': 'error',
    "no-mixed-operators": "error",
    "no-multiple-empty-lines": ["error", { max: 2, maxEOF: 1 }],
    "no-tabs": "error",
    "no-unneeded-ternary": "error",
    "no-whitespace-before-property": "error",
    "nonblock-statement-body-position": "error",
    "object-property-newline": [
      "error",
      { allowAllPropertiesOnSameLine: true }
    ],
    "quote-props": ["error", "as-needed"],
    // quotes: ['error', 'prefer-single'],
    semi: ["error", "never"],
    "semi-spacing": "error",
    "space-before-blocks": "error",
    // 'space-before-function-paren': 'error',
    "space-in-parens": "error",
    "space-infix-ops": "error",
    "space-unary-ops": "error",

    // ES6
    "arrow-spacing": "error",
    "no-confusing-arrow": "error",
    "no-duplicate-imports": "error",
    "no-var": "error",
    "object-shorthand": "error",
    "prefer-const": "error",
    "prefer-template": "error"
  }
}


你可以看到 eslint 说没有尾随昏迷,在我右键单击其中一个并修复所有可自动修复的问题后,问题就消失了,但在我保存文件后,更漂亮的扩展名又把它们放了。

  1. 安装eslint:
    npm i --save-dev eslint
    
  2. 初始化 eslint:
    npx eslint --init
    
    对于某些选项,您需要选择下一步:
    • 您想如何使用 ESLint?
    • 检查语法并发现问题
    • 您希望您的配置文件采用什么格式?
    • JavaScript
  3. 安装更漂亮:
    npm i --save-dev eslint-config-prettier eslint-plugin-prettier prettier 
    
  4. 创建.prettierrc 配置文件:
     {
         "printWidth": 80,
         "singleQuote": true,
         "trailingComma": "es5"
     }
    

现在您可以使用 eslint 检查样式并从命令行使用 prettier 修复样式。

对于 VSCode 与 eslint 和 prettier 的集成,您需要安装以下之一:

  1. esbenp.prettier-vscode.
  2. dbaeumer.vscode-eslint;

之后您需要将此扩展设置为默认格式化程序:

  1. 打开命令面板ctrl+shift+p;
  2. Select Format Document With...;
  3. Select Configure Document With...;
  4. Select Prettier - Code FormatterESLint 根据您的喜好。

现在,eslint 将作为 linter 和 prettier 作为格式化程序工作:

VSCode 问题选项卡:

eslint 输出

如果我保存文件,所有更漂亮的问题都将得到修复(应打开保存时自动套用格式)

Eslint plugin does not apply automatically changes in settings located in .prettierrc. See issue on github. To fix this you can:

  • move .prettierrc content in .eslint (not recommended because prettier plugins sometimes do not read this file);
  • run from command pallette in VSCode ESLint: Restart ESLint server after edit .prettierrc file.