【问题标题】:eslint object-curly-newline conflicts prettier multi-line rationaleeslint object-curly-newline 冲突更漂亮的多行基本原理
【发布时间】:2020-04-06 15:17:54
【问题描述】:

我是 prettier + typescript + eslint 的新手。 大多数集成工作正常,但解构对象中的多行则不行。

更漂亮的 1.19.1 Playground link

输入:

const {
  ciq,
  drawingMenuX,
  drawingMenuY,
  selectedDrawing,
} = store.chartStore;

输出:

const { ciq, drawingMenuX, drawingMenuY, selectedDrawing } = store.chartStore;

预期行为:

const {
  ciq,
  drawingMenuX,
  drawingMenuY,
  selectedDrawing,
} = store.chartStore;

我想在解构对象中保留多行格式,如基本原理 (https://prettier.io/docs/en/rationale.html#multi-line-objects)

在普通对象中效果很好,但在解构对象中却不行

我该如何解决这个问题? 我只需要修复每一行吗?

package.json

"@typescript-eslint/eslint-plugin": "^2.11.0",
"@typescript-eslint/parser": "^2.11.0",
"eslint": "^6.7.2",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-react": "^7.17.0",
"eslint-plugin-react-hooks": "^1.7.0",
"prettier": "^1.19.1",

.eslintrc.js

module.exports = {
  env: {
    browser: true,
    es6: true,
  },
  extends: ['plugin:react/recommended', 'plugin:prettier/recommended', 'airbnb'],
  plugins: ['react', '@typescript-eslint'],
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  },
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
  },
  settings: {
    'import/resolver': {
      node: {
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
      },
    },
  },
  rules: {
    'react/jsx-filename-extension': [
      1,
      {
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
      },
    ],

    'import/extensions': [
      'error',
      'ignorePackages',
      {
        js: 'never',
        jsx: 'never',
        ts: 'never',
        tsx: 'never',
      },
    ],

    'no-confusing-arrow': [0],
  },

  overrides: [
    {
      files: ['*.ts', '*.tsx'],
      rules: {
        '@typescript-eslint/no-unused-vars': [2, { args: 'none' }],
      },
    },
  ],
};

【问题讨论】:

    标签: javascript typescript eslint prettier


    【解决方案1】:

    我也更喜欢 Prettier 的处理方式。

    在您的.eslintrc.js 中将此添加到您的rules

    'object-curly-newline': 'off',
    

    【讨论】:

    • 谢谢!我想尽可能地换行我的花括号以提高可读性,但我在eslint.org/docs/rules/object-curly-newline 上找不到object-curly-newline 的任何eslint 代码。 Off 就是这样。另外,Off 不在我链接的官方文档中:S
    【解决方案2】:

    我遇到了同样的问题,不想完全关闭它。幸运的是,这个规则有一个配置选项,你可以在这里查看object-curly-newline

    如果在属性内部、属性之间甚至是最少数量的属性之间存在换行符,它允许配置换行符,甚至可以强制一致性(大括号,或者都不直接包含换行符)。

    这是我的使用方法:

    /* inside .eslintrc.json */
    
    "rules": {
      "object-curly-newline": [
        "error",
        {
          "ObjectExpression": { "consistent": true, "multiline": true },
          "ObjectPattern": { "consistent": true, "multiline": true },
          "ImportDeclaration": "never",
          "ExportDeclaration": { "multiline": true, "minProperties": 3 }
        }
      ]
    }
    

    【讨论】:

      【解决方案3】:

      根据我自己的测试,您应该在.eslintrc.js 中将以下内容添加到您的rules 中(有点符合GollyJer 的回答)

      'object-curly-newline': 0,
      

      【讨论】:

        猜你喜欢
        • 2020-09-09
        • 2018-05-13
        • 2021-04-08
        • 2020-10-19
        • 2019-01-23
        • 2020-02-13
        • 2021-06-02
        • 2021-04-06
        • 2020-01-28
        相关资源
        最近更新 更多