我在使我的 prettier 正确工作方面遇到了困难。当我启动format-check
(在 js,ts 文件上运行prettier --check
)时,尽管以下配置,它在许多类文件中(即使是只有一个属性和getter的简单类)给出SyntaxError: This experimental syntax requires enabling the parser plugin: 'classProperties'
错误。 当我从默认值更改解析器为其他值时,在.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
}