简体   繁体   中英

Prettier, eslint - classProperties parser plugin error

I'm struggling to make my prettier works correctly. When I launch format-check ( prettier --check on js,ts files) it gives me SyntaxError: This experimental syntax requires enabling the parser plugin: 'classProperties' errors in many class files (even simple ones with one property and getter) despite below configuration. When I change parser from default into something else in .prettierrc.json , then "@trivago/prettier-plugin-sort-imports" plugin doesn't work. I want to have the configuration, that doesn't give me the mentioned error and allows sorting plugin to work. Maybe I've got some conflicts on the eslint-prettier line?

part of package.json in main directory

"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 in main directory

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 in main directory

{
  "semi": true,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 120,
  "tabWidth": 2,
  "arrowParens": "always",
  "importOrder": ["^[a-zA-Z](.*)$", "^@[a-zA-Z](.*)$$", "^[./]"],
  "importOrderSeparation": true
}

It appears to work correctly if the following is added to .prettierrc.json to enable the required parser plugin:

"experimentalBabelParserPluginsList": ["classProperties", "jsx", "typescript"]

This was tested with prettier 2.3.2 and @trivago/prettier-plugin-sort-imports 2.0.4.

Example of complete .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"]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM