reactjs - 更漂亮,eslint - classProperties 解析器插件错误

标签 reactjs eslint prettier typescript-eslint prettier-eslint

我正在努力使我更漂亮的作品正常工作。当我启动时 format-check ( prettier --check 在 js、ts 文件上)它给了我 SyntaxError: This experimental syntax requires enabling the parser plugin: 'classProperties'尽管有以下配置,但许多类文件(甚至是具有一个属性和 getter 的简单文件)中的错误。当我在 .prettierrc.json 中将解析器从默认更改为其他内容时, 然后"@trivago/prettier-plugin-sort-imports"插件不起作用。我想要配置,它不会给我提到的错误并允许排序插件工作。也许我在 eslint-prettier 线上有一些冲突?package.json 的一部分在主目录中

"devDependencies": {
    "@babel/plugin-proposal-class-properties": "7.12.1",
    "@types/react": "16.9.46",
    "@types/react-dom": "16.9.8",
    "@types/styled-components": "5.1.2",
    "@typescript-eslint/eslint-plugin": "2.34.0",
    "@typescript-eslint/parser": "2.34.0",
    "@trivago/prettier-plugin-sort-imports": "1.3.0",
    "eslint": "6.8.0",
    "eslint-config-airbnb": "18.2.0",
    "eslint-config-airbnb-base": "14.2.0",
    "eslint-config-prettier": "6.11.0",
    "eslint-plugin-eslint-comments": "3.2.0",
    "eslint-plugin-eslint-plugin": "2.3.0",
    "eslint-plugin-jest": "23.20.0",
    "eslint-plugin-jsx-a11y": "5.1.1",
    "eslint-plugin-prettier": "3.1.4",
    "eslint-plugin-react": "7.20.6",
    "eslint-plugin-react-hooks": "4.1.0",
    "jest-styled-components": "7.0.3",
    "lerna": "3.22.1",
    "prettier": "2.2.1"
  },
  "scripts": {
    "format": "prettier  --write \"**/*.{ts,tsx,js,jsx}\"",
    "format-check": "prettier --check \"**/*.{ts,tsx,js,jsx}\"",
    ...
  },
  "babel": {
    "presets": [
      "@babel/preset-env",
      "@babel/preset-react",
      {
        "plugins": [
          "@babel/plugin-proposal-class-properties",
          {
            "loose": true
          }
        ]
      }
    ]
  }
.eslintrc.js在主目录中
module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
    jest: true,
  },
  plugins: ['@typescript-eslint'],
  extends: [
    'airbnb',
    'plugin:react/recommended',
    'plugin:react-hooks/recommended',
    'plugin:jsx-a11y/strict',
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'react-app',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2017, // Allows for the parsing of modern ECMAScript features
    sourceType: 'module', // Allows for the use of imports
    ecmaFeatures: {
      jsx: true, // Allows for the parsing of JSX
    },
    project: ['./projects/*/tsconfig.json'],
  },
  settings: {
    react: {
      version: 'detect', // Tells eslint-plugin-react to automatically detect the version of React to use
    },
  },
};
.prettierrc.json在主目录中
{
  "semi": true,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 120,
  "tabWidth": 2,
  "arrowParens": "always",
  "importOrder": ["^[a-zA-Z](.*)$", "^@[a-zA-Z](.*)$$", "^[./]"],
  "importOrderSeparation": true
}

最佳答案

如果将以下内容添加到 .prettierrc.json 中,它似乎可以正常工作启用所需的解析器插件:

"experimentalBabelParserPluginsList": ["classProperties", "jsx", "typescript"]
这是用 prettier 测试的2.3.2 和 @trivago/prettier-plugin-sort-imports 2.0.4.
完整示例 .prettierrc.json :
{
  "semi": true,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 120,
  "tabWidth": 2,
  "arrowParens": "always",
  "importOrder": ["^[a-zA-Z](.*)$", "^@[a-zA-Z](.*)$$", "^[./]"],
  "importOrderSeparation": true,
  "experimentalBabelParserPluginsList": ["classProperties", "jsx", "typescript"]
}

关于reactjs - 更漂亮,eslint - classProperties 解析器插件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65284700/

相关文章:

javascript - 如何在 React 中更新 parent 的状态?

css - react : How to set width and height of an element in react component programatically

ESLint:定义的全局扩展仍未定义(no-undef)

javascript - 为什么 prettier 将尾随逗号放在自己的位置并给出错误说它需要删除?

javascript - 如何将 .prettierignore 文件基于 .gitignore

html - 为什么使用d3 svg组件时文档默认为最小div高度?

javascript - Reactjs:如何确保用户不会修改 POST 请求的响应

typescript - 在 <script setup> 中出现不当警告时如何正确配置 ESLint?

reactjs - 如何检测 NextJS 项目中的所有 Typescript 和 ESLint 错误,而不仅仅是打开的文件中的错误?

typescript - 需要帮助修复或抑制此 tslint 错误 : TS2742