react-starter-kit 学习之eslint 规则

发布时间:2019-08-14 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了react-starter-kit 学习之eslint 规则脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

react-start-kIT 是一个全栈的开发模板。前端配置了webpack + react 后端配置 exPress + SQLite + graphql 等,看到react-starter-kit过程中有很多坑。需要一个一个的走过。记录自己学习的历程,在开发过程中还是需要eslint,这是自我对代码风格的一种规范。

react-starter-kit 的目录结构

如上图,配置了很多开发过程中需要的配置文件。

学习.eslintrc.js

如下图查看码:

/**
 * React Starter Kit (https://www.reactstarterkit.com/)
 *
 * Copyright © 2014-present Kriasoft, LLC. All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */

// ESLint configuration
// http://eslint.org/docs/user-guide/configuring
module.exports = {
  parser: 'babel-eslint', // 解析引擎使用babel-eslint

  extends: [
    'airbnb',
    'plugin:flowtype/recommended',
    'plugin:css-modules/recommended',
    'prettier',
    'prettier/flowtype',
    'prettier/react'
  ],  // 这里配置继承规则

  plugins: ['flowtype', 'css-modules', 'prettier'], // 这里配置了eslint 插件,在Package.json 中可以查看到eslint-plugin-xxx 等等插件

  globals: {
    __DEV__: true
  }, // __DEV__ 全局变量开启

  env: {
    browser: true
  }, // 配置当前规则环境为浏览器环境

  rules: {
    // Forbid the use of extraneous packages
    // https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-extraneous-dependencies.md
    'import/no-extraneous-dependencies': ['error', {packageDir: '.'}], // 本条规则为了防止使用import xxx from 'xxx' 然而并没有使用会报错

    // Recommend not to leave any console.log in your code
    // Use console.error, console.warn and console.info instead
    // https://eslint.org/docs/rules/no-console
    'no-console': [
      'error',
      {
        allow: ['warn', 'error', 'info']
      }
    ], // 禁止使用console.log,否则报错,但是可以使用console.info console.warn console.error

    // Prefer destructuring from arrays and objects
    // http://eslint.org/docs/rules/prefer-destructuring
    'prefer-destructuring': [
      'error',
      {
        VariableDeclarator: {  // 开启了变量解构赋值
          array: true,
          object: true
        },
        AssignmentExpression: { // 开启了参数的结构赋值
          array: true,
          object: true
        }
      },
      {
        enforceForrenamedProperties: false // 可以赋值更改变量 e.g: const {bar: foo} = { bar: 1, food: 3} 将变量的值修改变量名为foo
      }
    ], // 这条规则我修改过,主要是es6的解构赋值

    // Ensure <a> tags are valid
    // https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.md
    'jsx-a11y/anchor-is-valid': [
      'error',
      {
        components: ['Link'],
        specialLink: ['to'],
        aspects: ['noHref', 'invalidHref', 'preferButton']
      }
    ], // 这条规则主要是让锚点有效, 组件为Link 

    // Allow .js files to use JSX syntax
    // https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-filename-extension.md
    'react/jsx-filename-extension': ['error', {extensions: ['.js', '.jsx']}], // 可以使用js/jsx 写react的jsx语法

    // Functional and class components are equivalent from React’s point of view
    // https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-stateless-function.md
    'react/prefer-stateless-function': 'off', // 可以使用 class xxx extend React.Component{} 或者 function xxx => <div></div> 这种方式

    // ESLint plugin for prettier formatting
    // https://github.com/prettier/eslint-plugin-prettier
    'prettier/prettier': [
      'error',
      {
        // https://github.com/prettier/prettier#options
        singleQuote: true,
        trailingComma: 'all'
      }
    ] // 定义使用单引号,并且保留,
  },

  settings: {
    // Allow absolute paths in imports, e.g. import Button from 'components/Button'
    // https://github.com/benmosher/eslint-plugin-import/tree/master/resolvers
    'import/resolver': {
      node: {
        moduleDirectory: ['node_modules', 'src']
      }
    } // 允许绝对路径导入
  }
}

脚本宝典总结

以上是脚本宝典为你收集整理的react-starter-kit 学习之eslint 规则全部内容,希望文章能够帮你解决react-starter-kit 学习之eslint 规则所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。