React Native开发的一种代码规范:Eslint + FlowType

时间:2022-09-25 17:13:58

【这篇随笔记录的很简单,没有涉及具体的Eslint规则解释以及FlowType的类型说明和使用等,只是链接了所需的若干文档】

js开发很舒服,但是代码一多起来就参差不齐,难以阅读了。所以加上一些代码规范以及检测报错会保证项目代码的健康程度,我这里使用的是Eslint + FlowType来进行代码规范的(我还不会TypeScript,所以就没有和TS的对比了)。

达到的目标:

  • Eslint
    • 对代码的缩进、格式等有规定
    • ...诸多Eslint的规定,具体参见Eslint文档。
  • FlowType
    • 所有的方法参数都有明确的类型声明和返回类型声明

具体的环境配置方法:

  • Eslint
    • 参考 Eslint Getting Started进行环境配置(我使用的是airbnb的,并且有所修改)。
    • 配置.eslintrc文件,指定属于你自己的规则。
    • (可选)设置git提交,在eslint检测通过后才可以。修改git的/.git/hooks/pre-commit文件(没有的话,新建一个),修改为如下所示。(这里可以用ln -s将.git/hooks链接到git仓库里的自己创建的gitHooks目录,可以使用git管理这些文件,默认的.git/目录是被git忽视的,无法直接管理。)
#! /usr/bin/env python

import sys
import os # in "<project_dir>/gitHooks"
os.chdir(os.path.dirname(__file__))
# in "<project_dir>"
os.chdir('..') def runCommand(command):
return os.popen(command).read().strip('\n') cachedFiles = runCommand('git diff --name-only --cached --diff-filter=ACMR') if not cachedFiles:
sys.exit(0) files = cachedFiles.split('\n')
filePaths = '' folderPath = os.getcwd()
for file in files:
if file.endswith('.js'):
filePaths += os.path.join(file) + ' ' if not filePaths.strip():
sys.exit(0) checkStylePath = ''
checkStyleCommand = './node_modules/.bin/eslint {files}'.format(files=filePaths)
if os.system(checkStyleCommand) == 0:
sys.exit(0)
else:
sys.exit(1)
  • FlowType
    • 参考FlowType的eslint引导,将其中的规则copy到eslintrc文件里。可以根据自己的要求修改。
    • FlowType的具体type定义使用参考FlowType

项目报错,但是想修改这个eslint rule的步骤(针对WebStorm)

  • 查看错误原因(指针指着红线就可以了),copy里面的原因,此处为"spaced-comment"React Native开发的一种代码规范:Eslint + FlowType
  • 在node_module目录下全局搜索错误原因,从搜索结果里挨个找,可以找到air-bnb的文件,叫作eslint-config-airbnb-base...,从里面可以查看具体的规则说明,可以通过注释的链接跳转到详细的网页。
  • 在网页上查看具体规则说明,并修改自己的eslintrc文件的rule。

我的.eslintrc.js

module.exports = {
env: {
es6: true,
node: true,
},
extends: 'airbnb',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
// let babel-eslint parse, because type definition will be error with eslint parser.
parser: "babel-eslint",
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
},
plugins: [
'react',
"flowtype",
],
rules: {
// 0 for 'off', 1 for 'warning',2 for 'error',
// indent by 4
"indent": ["error", 4, {
"SwitchCase": 1,
"FunctionDeclaration": {
"parameters": "first"
},
"FunctionExpression": {
"parameters": "first"
}
}],
// Enforce JSX indentation
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-indent.md
'react/jsx-indent': ['error', 4],
// Validate props indentation in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-indent-props.md
'react/jsx-indent-props': ['error', 4],
// max length of one line
"max-len": ["error", 130],
// should have space after "{" and before "}"
"object-curly-spacing": ["error", "never"],
// When there is only a single export from a module, prefer using default export over named export.
'import/prefer-default-export': 'off',
// http://eslint.org/docs/rules/quotes
"quotes": ["off"],
// https://eslint.org/docs/rules/object-curly-newline
'object-curly-newline': ['error', {
ObjectExpression: {minProperties: 4, multiline: true, consistent: true},
ObjectPattern: {minProperties: 4, multiline: true, consistent: true},
// it is not necessary to do with import and export( WebStorm does not supprt quick format to this )
// ImportDeclaration: {minProperties: 4, multiline: true, consistent: true},
// ExportDeclaration: {minProperties: 4, multiline: true, consistent: true},
}],
// http://eslint.org/docs/rules/no-underscore-dangle
"no-underscore-dangle": [0],
// allow
"no-unused-expressions": 0,
// allow use of variables before they are defined
"no-use-before-define": 0,
// only .js and .jsx files may have JSX,
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-filename-extension.md
'react/jsx-filename-extension': [
2,
{
extensions: ['.js', '.jsx'],
},
],
// Validate whitespace in and around the JSX opening and closing brackets
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/jsx-tag-spacing.md
'react/jsx-tag-spacing': ['error', {
closingSlash: 'never',
beforeSelfClosing: 'never',
afterOpening: 'never',
beforeClosing: 'never',
}],
// do not require all requires be top-level to allow static require for <Image/>
// https://eslint.org/docs/rules/global-require
'global-require': 0,
// enforces no braces where they can be omitted
// https://eslint.org/docs/rules/arrow-body-style
'arrow-body-style': [1, 'as-needed', {
requireReturnForObjectLiteral: false,
}], // below is flowType lint
// https://github.com/gajus/eslint-plugin-flowtype
"flowtype/boolean-style": [
2,
"boolean"
],
"flowtype/define-flow-type": 1,
"flowtype/delimiter-dangle": [
2,
"never"
],
"flowtype/generic-spacing": [
2,
"never"
],
"flowtype/no-mixed": 2,
"flowtype/no-primitive-constructor-types": 2,
"flowtype/no-types-missing-file-annotation": 2,
"flowtype/no-weak-types": 2,
"flowtype/object-type-delimiter": [
2,
"comma"
],
"flowtype/require-parameter-type": 2,
"flowtype/require-return-type": [
2,
"always",
{
"annotateUndefined": "never"
}
],
"flowtype/require-valid-file-annotation": 2,
"flowtype/semi": [
2,
"always"
],
"flowtype/space-after-type-colon": [
2,
"always"
],
"flowtype/space-before-generic-bracket": [
2,
"never"
],
"flowtype/space-before-type-colon": [
2,
"never"
],
"flowtype/type-id-match": [
2,
"^([A-Z][a-z0-9]+)+Type$"
],
"flowtype/union-intersection-spacing": [
2,
"always"
],
"flowtype/use-flow-type": 1,
"flowtype/valid-syntax": 1
},
};

React Native开发的一种代码规范:Eslint + FlowType的更多相关文章

  1. 开发一个项目之代码规范ESLint

    ESLint{ "rules": { "semi": ["error", "always"], } }error lev ...

  2. React Native开发技术周报2

    (1).资讯 1.React Native 0.22_rc版本发布 添加了热自动重载功能 (2).技术文章 1.用 React Native 设计的第一个 iOS 应用 我们想为用户设计一款移动端的应 ...

  3. Hybrid App 和 React Native 开发那点事

    简介:Hybrid App(混合模式移动应用)开发是指介于Web-app.Native-App这两者之间的一种开发模式,兼具「Native App 良好用户交互体验的优势」和「Web App 跨平台开 ...

  4. 《React Native 精解与实战》书籍连载「Node&period;js 简介与 React Native 开发环境配置」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  5. Mac配置React Native开发环境

    一直觉得学习一样东西,不动手怎么也学不会,就像学习swift,看了视频没有动手操作,记住的也就那么点,自己写出东西不是这里有问题就是那里出错. 所以,以后学习自己要多动手. 现在我的学习任务就是: 提 ...

  6. React Native开发入门

    目录: 一.前言 二.什么是React Native 三.开发环境搭建 四.预备知识 五.最简单的React Native小程序 六.总结 七.参考资料   一.前言 虽然只是简单的了解了一下Reac ...

  7. React Native开发技术周报1

    (一).资讯 1.React Native 0.21版本发布,最新版本功能特点,修复的Bug可以看一下已翻译 重要:如果升级 Android 项目到这个版本一定要读! 我们简化了 Android 应用 ...

  8. React Native 开发之 &lpar;02&rpar; 用Sublime 3作为React Native的开发IDE

    Sublime Text是一个代码编辑器.也是HTML和散文先进的文本编辑器.漂亮的用户界面和非凡的功能,例如:迷你地图,多选择Python插件,代码段等等.完全可自定义键绑定,菜单和工具栏等等.漂亮 ...

  9. React Native 开发笔记

    ReactNativeDemo 学习ReactNative开发,搭建ReactNative第一个项目 React Native 开发笔记 1.安装Homebrew $ /usr/bin/ruby -e ...

随机推荐

  1. 解析Javascript事件冒泡机制

    本资源引自: 解析Javascript事件冒泡机制 - 我的程序人生 - 博客频道 - CSDN.NET http://blog.csdn.net/luanlouis/article/details/ ...

  2. call 和 apply使用

      call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向.因为 JavaScript 的函数存在「定义时上下文」 ...

  3. 写给自己看的Linux运维基础&lpar;三&rpar; - Mono

    如果使用ubuntu,可使用apg-get安装完mono,xsp,mod_mono,我的yum源并没有找到mono可安装,网上也没找到CentOS的源,最后选择下载编译安装. Stackoverflo ...

  4. runc create container 流程分析

    1.// runc/create.go Action: func(context *cli.Context) error 首先调用spec, err := setupSpec(context)加载配置 ...

  5. python 爬取天猫美的评论数据

    笔者最近迷上了数据挖掘和机器学习,要做数据分析首先得有数据才行.对于我等平民来说,最廉价的获取数据的方法,应该是用爬虫在网络上爬取数据了.本文记录一下笔者爬取天猫某商品的全过程,淘宝上面的店铺也是类似 ...

  6. Android源码浅析(一)——VMware Workstation Pro和Ubuntu Kylin 16&period;04 LTS安装配置

    Android源码浅析(一)--VMware Workstation Pro和Ubuntu Kylin 16.04 LTS安装配置 最近地方工作,就是接触源码的东西了,所以好东西还是要分享,系列开了这 ...

  7. Linux内核分析 实验一 ——by王玥

    一.实验内容 1)实验部分(以下命令为实验楼64位Linux虚拟机环境下适用,32位Linux环境可能会稍有不同) 使用 gcc –S –o main.s main.c -m32 命令编译成汇编代码, ...

  8. 使用github搭建个人html网站

    前言:搭建个人网站早就想做了,最近有空就宅在家学习,突然发现github就可以搭建个人的纯html网站,于是开始了这项工作.转载请注明出处:https://www.cnblogs.com/yuxiao ...

  9. Internet History&comma; Technology and Security &lpar;Week4&rpar;

    Week4. We are now moving into Week 4! This week, we will be covering commercialization and growth. T ...

  10. 【Linux 命令】- tar 命令

    语法 tar [-ABcdgGhiklmMoOpPrRsStuUvwWxzZ][-b <区块数目>][-C <目的目录>][-f <备份文件>][-F <Sc ...