react eslint配置.eslintrc.js


module.exports = {
    env: {
        'browser': true,
        'commonjs': true,
        'es6': true
    },
    extends: 'eslint:recommended',
    globals: {
        page: true,
        REACT_APP_ENV: true
    },
    parser: '@babel/eslint-parser',
    parserOptions: {
        requireConfigFile: false, // 是否需要 babel 配置文件
        sourceType: 'module', // script 或者 module
        allowImportExportEverywhere: false, // 設置為 true,import 和 export 聲明 可以出現在文件的任務位置,否則只能出現在頂部
        ecmaFeatures: {
            globalReturn: false // 設置為 true,當 sourceType 為 script 時,允許全局 return
        },
        babelOptions: {
            presets: ['@babel/preset-react']
        }
    },
    plugins: [
        'react'
    ],
    rules: {
        'for-direction': 'error', // 禁止 for 循環出現方向錯誤的循環,比如 for (i = 0; i < 10; i--)
        'no-await-in-loop': 'off',// 禁止將 await 寫在循環里
        'no-compare-neg-zero': 'error',// 禁止與負零進行比較
        'no-empty-character-class': 'error', // 禁止在正則表達式中使用空的字符集 []
        'no-extra-parens': ['error','functions'], // 禁止函數表達式中出現多余的括號
        'no-extra-semi': 'error', // 禁止出現多余的分號
        'no-func-assign': 'error', // 禁止將一個函數聲明重新賦值
        'no-inner-declarations': ['error', 'both'],// 禁止在 if 代碼塊內出現函數聲明
        'no-regex-spaces': 'error', // 禁止在正則表達式中出現連續的空格
        'no-sparse-arrays': 'error',// 禁止在數組中出現連續的逗號
        'no-unreachable': 'error', // 禁止在 return, throw, break 或 continue 之后還有代碼
        'no-template-curly-in-string': 'error',// 禁止出現難以理解的多行表達式
        'use-isnan': 'error',// 必須使用 isNaN(foo)
        // 'complexity': ['error',{'max': 20}], // 禁止函數的循環復雜度超過 20
        'dot-location': ['error','property'], // 鏈式調用的時候,點號必須放在第二行開頭處,禁止放在第一行結尾處
        'no-alert': 'error',//禁止使用alert
        'no-empty-function': [
            'error',
            {
                'allow': [
                    'functions',
                    'arrowFunctions'
                ]
            }
        ],  // 不允許有空函數,除非是將一個空函數設置為某個項的默認值
        'no-extend-native': 'error', // 禁止修改原生對象
        'no-global-assign': 'error',// 禁止對全局變量賦值
        'no-return-assign': [
            'error',
            'always'
        ],// 禁止在 return 語句里賦值
        'keyword-spacing': [
            'error',
            {
                'before': true,
                'after': true
            }
        ],// 關鍵字前后必須有空格

        'key-spacing': [0, {
            'beforeColon': false,
            'afterColon': true
        }],//對象字面量中冒號的前后空格
        'max-depth': [
            'error',
            5
        ], // 代碼塊嵌套的深度禁止超過 5 層
        'max-params': [
            'error',
            6
        ],  // 函數的參數禁止超過 6 個
        'newline-per-chained-call': 'error',//鏈式調用必須換行
        'no-multiple-empty-lines': ['error',{
            'max': 3,
            'maxEOF': 1,
            'maxBOF': 1
        }],// 禁止出現超過三行的連續空行
        'no-whitespace-before-property': 'error', // 禁止屬性前有空格
        'nonblock-statement-body-position': [
            'error',
            'beside',
            {
                'overrides': {
                    'while': 'below'
                }
            }
        ],// 禁止 if 后面不加大括號而寫兩行代碼
        'object-curly-newline': [
            'error',
            {
                'multiline': true,
                'consistent': true
            }
        ],// 大括號內的首尾必須有換行
        'object-property-newline': 'error', // 對象字面量內的屬性每行必須只有一個

        'indent': [
            'error',
            4,
            {
                'SwitchCase': 1,
                'ignoredNodes': ['ConditionalExpression']
            }
        ],
        'quotes': [2, 'single'], // 單引號
        // 'no-console': ['error', { allow: ['info', 'error'] }],//不允許console,除了console.info和console.error
        'no-debugger': 0, // 不禁用debugger
        'no-var': 0, // 對var警告
        'semi': 0, // 不強制使用分號
        'no-irregular-whitespace': 0, // 不規則的空白不允許
        'no-trailing-spaces': 2, // 一行結束后面有空格就發出警告
        'eol-last': 0, // 文件以單一的換行符結束
        'no-unused-vars': [2, {
            'vars': 'all',
            'args': 'after-used'
        }], // 不能有聲明后未被使用的變量或參數
        'no-underscore-dangle': 0, // 標識符不能以_開頭或結尾
        'no-lone-blocks': 0, // 禁止不必要的嵌套塊
        'no-class-assign': 2, // 禁止給類賦值
        'no-cond-assign': 2, // 禁止在條件表達式中使用賦值語句
        'no-const-assign': 2, // 禁止修改const聲明的變量
        'no-delete-var': 2, // 不能對var聲明的變量使用delete操作符
        'no-dupe-keys': 2, // 在創建對象字面量時不允許鍵重復
        'no-duplicate-case': 2, // switch中的case標簽不能重復
        'no-useless-catch':0,//允許try catch
        'no-dupe-args': 2, // 函數參數不能重復
        'no-empty': 2, // 塊語句中的內容不能為空
        'no-invalid-this': 0, // 禁止無效的this,只能用在構造器,類,對象字面量
        'no-redeclare': 2, // 禁止重復聲明變量
        'no-spaced-func': 2, // 函數調用時 函數名與()之間不能有空格
        'no-this-before-super': 0, // 在調用super()之前不能使用this或super
        'no-undef': 2, // 不能有未定義的變量
        'no-use-before-define': 2, // 未定義前不能使用
        'camelcase': 0, // 強制駝峰法命名
        'jsx-quotes': [2, 'prefer-double'], // 強制在JSX屬性(jsx-quotes)中一致使用雙引號
        'react/display-name': 0, // 防止在React組件定義中丟失displayName
        'react/forbid-prop-types': [2, {'forbid': ['any']}], // 禁止某些propTypes
        'react/jsx-boolean-value': 2, // 在JSX中強制布爾屬性符號
        // 'react/jsx-closing-bracket-location': 1, // 在JSX中驗證右括號位置
        'react/jsx-curly-spacing': [2, {
            'when': 'never',
            'children': true
        }], // 在JSX屬性和表達式中加強或禁止大括號內的空格。
        'react/jsx-indent':['error',4],
        'react/jsx-indent-props': ['error',4], // 驗證JSX中的props縮進
        'react/jsx-key': 2, // 在數組或迭代器中驗證JSX具有key屬性
        'react/jsx-max-props-per-line': [1, {'maximum': 3}], // 限制JSX中單行上的props的最大數量
        'react/jsx-no-bind': 0, // JSX中不允許使用箭頭函數和bind
        'react/jsx-no-duplicate-props': 2, // 防止在JSX中重復的props
        'react/jsx-no-literals': 0, // 防止使用未包裝的JSX字符串
        'react/jsx-no-undef': 1, // 在JSX中禁止未聲明的變量
        'react/jsx-pascal-case': 0, // 為用戶定義的JSX組件強制使用PascalCase
        'react/jsx-sort-props': 2, // 強化props按字母排序
        'react/jsx-uses-react': 1, // 防止反應被錯誤地標記為未使用
        'react/jsx-uses-vars': 2, // 防止在JSX中使用的變量被錯誤地標記為未使用
        'react/no-danger': 0, // 防止使用危險的JSX屬性
        'react/no-did-mount-set-state': 0, // 防止在componentDidMount中使用setState
        'react/no-did-update-set-state': 1, // 防止在componentDidUpdate中使用setState
        'react/no-direct-mutation-state': 2, // 防止this.state的直接變異
        // 'react/no-multi-comp': 2, // 防止每個文件有多個組件定義
        'react/no-set-state': 0, // 防止使用setState
        'react/no-unknown-property': 2, // 防止使用未知的DOM屬性
        'react/prefer-es6-class': 2, // 為React組件強制執行ES5或ES6類
        'react/prop-types': 0, // 防止在React組件定義中丟失props驗證
        'react/react-in-jsx-scope': 2, // 使用JSX時防止丟失React
        'react/self-closing-comp': 0, // 防止沒有children的組件的額外結束標簽
        'react/sort-comp': 2, // 強制組件方法順序
        'no-extra-boolean-cast': 0, // 禁止不必要的bool轉換
        'react/no-array-index-key': 0, // 防止在數組中遍歷中使用數組key做索引
        'react/no-deprecated': 1, // 不使用棄用的方法
        'react/jsx-equals-spacing': 2, // 在JSX屬性中強制或禁止等號周圍的空格
        'comma-dangle': 2, // 對象字面量項尾不能有逗號
        'no-mixed-spaces-and-tabs': 0, // 禁止混用tab和空格
        'prefer-arrow-callback': 0, // 比較喜歡箭頭回調
        'arrow-parens': 0, // 箭頭函數用小括號括起來
        'arrow-spacing': 0, //= >的前/后括號
        'react/no-unescaped-entities': 'error' // 禁止出現 HTML 中的屬性,如 class
    }
};


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM