天天看点

react-native如何配置eslint+prettierrc

Eslint + Prettier

1.相关依赖下载

package.json

"devDependencies": {
        "@babel/core": "^7.15.0",
        "@babel/eslint-parser": "^7.15.0",
        "@babel/runtime": "^7.8.4",
        "babel-jest": "^25.1.0",
        "eslint": "^7.32.0",
        "eslint-config-prettier": "^8.3.0",
        "eslint-plugin-flowtype": "^5.9.2",
        "eslint-plugin-html": "^6.1.2",
        "eslint-plugin-prettier": "^4.0.0",
        "eslint-plugin-react": "^7.25.1",
        "eslint-plugin-react-hooks": "^4.2.0",
        "eslint-plugin-react-native": "^3.11.0",
        ...
           

2.

eslintrc.js

配置

module.exports = {
    root: true,
    parser: '@babel/eslint-parser',
    env: {
        node: true,
        browser: true,
        es6: true,
    },
    extends: [
        'eslint:recommended',
        'plugin:react/recommended',
        'plugin:flowtype/recommended',
        'plugin:prettier/recommended',
    ],
    settings: {
        react: {
            version: 'detect',
        },
    },
    plugins: ['react', 'react-native', 'flowtype', 'html', 'react-hooks'],
    globals: {
        module: true,
    },
    // add your custom rules here
    rules: {
        'prettier/prettier': 'error',
        'react-native/no-inline-styles': 0,
        'react/prop-types': 0,
        'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
        'react-hooks/exhaustive-deps': 'warn', // Checks effect dependencies
        semi: [0],
    },
}
           

3.

prettierrc.js

配置

module.exports = {
    singleQuote: true,
    trailingComma: 'all',
    overrides: [
        {
            files: '.prettierrc',
            options: {
                parser: 'json',
            },
        },
    ],
}