Prettier用VSCode做出反应/ jsx-max-props-per-line格式

时间:2018-04-23 13:23:06

标签: javascript reactjs eslint prettier

我在React的JavaScript项目中使用Prettier。我的所有组件道具都是一行格式化的:

<Icon icon="arrow-left" width={15} height={18} />

我想这样:

<Icon
  icon="arrow-left"
  width={15}
  height={18}
/>

我已将"react/jsx-max-props-per-line": [1, { "when": "multiline" }]添加到我的.prettierrc中,但没有结果。

我也有一个ESLint配置,遵守这条规则:

{
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:prettier/recommended"
  ],
  "plugins": ["react", "prettier", "standard"],
  "rules": {
    "indent": [2, 2, { "SwitchCase": 1 }],
    "quotes": [2, "single"],
    "linebreak-style": [2, "unix"],
    "semi": [2, "always"],
    "no-console": [0],
    "no-loop-func": [0],
    "new-cap": [0],
    "no-trailing-spaces": [0],
    "no-param-reassign": [0],
    "func-names": [0],
    "comma-dangle": [0],
    "no-unused-expressions": [0],
    "block-scoped-var": [0],
    "react/prop-types": [0],
    "prettier/prettier": "error"
  }
}

我的.prettier文件配置:

  "bracketSpacing": true,
  "jsxBracketSameLine": true,
  "printWidth": 80,
  "singleQuote": true,
  "trailingComma": "all",
  "tabWidth": 2,
  "useTabs": false,
  "react/jsx-max-props-per-line": [1, { "when": "always" }]

也许是冲突?我试图将react/jsx-max-props-per-line移动到ESLint规则,但也没有结果。没有变化。

任何人都可以帮助我?

5 个答案:

答案 0 :(得分:4)

我能够让它与以下一起工作:

// .eslintrc.js
module.exports = {
  extends: [
    'react-app',
    'prettier',
    'plugin:prettier/recommended',
  ],
  plugins: ['prettier'],
  rules: {
    'react/jsx-first-prop-new-line': [2, 'multiline'],
    'react/jsx-max-props-per-line': [
      2,
      { maximum: 1, when: 'multiline' },
    ],
    'react/jsx-indent-props': [2, 2],
    'react/jsx-closing-bracket-location': [
      2,
      'tag-aligned',
    ],
  },
}

// .prettierrc
{
 "semi": false,
 "singleQuote": true,
 "printWidth":80 // default
}

以及我的开发依赖项,它基本上来自 eslint-config-react-app

"devDependencies": {
"@types/node": "^14.6.0",
"@types/react": "^16.9.46",
"@types/react-dom": "^16.9.8",
"@typescript-eslint/eslint-plugin": "^4.0.0",
"@typescript-eslint/parser": "^4.0.0",
"babel-eslint": "^10.0.0",
"eslint": "^7.5.0",
"eslint-config-prettier": "^7.2.0",
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-flowtype": "^5.2.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^24.0.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.20.3",
"eslint-plugin-react-hooks": "^4.0.8",
"eslint-plugin-testing-library": "^3.9.0",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"typescript": "4.0.5"
},

在行超过 printWidth: 80 个字符时格式化代码。

<InputField name="password" placeholder="password" label="Password" type="password" />
  
// transforms to this if line exceeds 'printWidth'

<InputField
  name="username"
  placeholder="username"
  label="Username"
  type="text" 
/>

答案 1 :(得分:1)

module.exports = {
    'extends': [
        'eslint:recommended',

    ],
    'env': {
        'es6': true
    },
    'plugins': ['react'],
    'parser': 'babel-eslint',
    'rules': {

        // you rules....

        'react/jsx-first-prop-new-line': [1, 'multiline'],
        'react/jsx-max-props-per-line': [1,
            {
                'maximum': 1
            }
        ]
    },
};

yarn add --dev prettier-eslint-cli

VS代码userConfig

"prettier.eslintIntegration": true,

答案 2 :(得分:0)

首先,您应该只对ESLint配置的ESLint规则,而不是您的.prettierrc文件......它们将被忽略,因为它们不是有效的Prettier配置。此外,ESLint不会影响Prettier行为。您可以通过ESLint运行Prettier(通过eslint-plugin-prettier作为可自动修复的规则)或运行Prettier并使用prettier-eslint运行ESLint,如果您prettier.eslintIntegration已打开,则使用VSCode。< / p>

现在您可能需要更改ESLint规则以使用{"when": "always"}选项。根据{{​​3}},使用"multiline"只会抱怨您的组件已经是多行,但每行有超过1个道具:

<Icon 
  icon="arrow-left"
  widht={15} height={18}
/>

即使标记最初不是多行,使用"always"也不会允许每行超过1个道具。

答案 3 :(得分:0)

问题是 "react/jsx-max-props-per-line" 不是有效的漂亮规则,它是 ESLint 的规则。我建议将此规则保留在 ESLint 配置文件中,但也要设置您的编辑器以在保存时进行所有可能的 ESLint 修复。

您可以按照此 tutorial 打开保存时的 linting。或者只是把这些:

  "editor.codeActionsOnSave": {
      "source.fixAll.eslint": true
  },
  "eslint.validate": ["javascript"]

用户或本地工作区中的行 settings.json 对象。这将解决您的问题。

根据我的经验,Prettier 和 ESLint 自动格式化可以很好地协同工作。

答案 4 :(得分:0)

如果您只想在 Vscode 中使用 Prettier 更改保存道具的行,那么有一个选项可以做到这一点。

在 settings.json 中添加以下行

"prettier.printWidth": 100,

enter image description here

之前: enter image description here

之后: enter image description here

相关问题