这一篇主要介绍代码规范相关的内容。javascript
咱们一般使用lint工具来检查代码不规范的地方,如下是将 eslint、typescript 和 webpack 结合使用的例子。java
首先安装依赖:node
$ npm i -D eslint babel-eslint eslint-loader eslint-plugin-jsx-control-statements $ npm i -D eslint-plugin-react @typescript-eslint/parser @typescript-eslint/eslint-plugin
而后在根目录新建eslint配置文件.eslintrc.js
:react
module.exports = { "root": true, "env": { "browser": true, "node": true, "es6": true, // "jquery": true "jest": true, "jsx-control-statements/jsx-control-statements": true // 可以在jsx中使用if,须要配合另外的babel插件使用 }, "parser": "@typescript-eslint/parser", "parserOptions": { "sourceType": 'module', "ecmaFeatures": { "jsx": true, "experimentalObjectRestSpread": true } }, "globals": { // "wx": "readonly", }, "extends": [ "eslint:recommended", "plugin:react/recommended", "plugin:jsx-control-statements/recommended", // 须要另外配合babel插件使用 ], "settings": { "react": { "version": "detect" // 自动读取已安装的react版本 } }, "plugins": ["@typescript-eslint", "react", "jsx-control-statements"], "rules": { "no-extra-semi": 0, // 禁止没必要要的分号 "quotes": ['error', 'single'], // 强制使用单引号 "no-unused-vars": 0 // 不容许未定义的变量 // ...你本身的配置 } };
咱们可能但愿检查或不检查某些特定的文件,能够在根目录新建.eslintignore
,如下配置不检查src目录之外的js文件:jquery
**/*.js !src/**/*.js
还须要配置webpack,才能在开发时启用eslint:webpack
// webpack.base.js module: { rules: [ // 把这个配置放在全部loader以前 { enforce: 'pre', test: /\.tsx?$/, exclude: /node_modules/, include: [APP_PATH], loader: 'eslint-loader', options: { emitWarning: true, // 这个配置须要打开,才能在控制台输出warning信息 emitError: true, // 这个配置须要打开,才能在控制台输出error信息 fix: true // 是否自动修复,若是是,每次保存时会自动修复能够修复的部分 } } ] }
除了约束开发时的编码规范外,咱们通常还但愿在提交代码时自动格式化代码,但咱们只但愿处理当前提交的代码,而不是整个代码库,不然会把提交记录搞得乱七八糟,prettier和lint-staged能够完成这项任务。git
先安装工具:es6
$ npm i -D prettier eslint-plugin-prettier eslint-config-prettier $ npm i -D lint-staged
在根目录增长prettier配置.prettierrc.js
,一样的也能够增长忽略配置.prettierignore
(建议配置为与lint忽略规则一致):github
// 这个配置须要与eslint一致,不然在启用 eslint auto fix 的状况下会形成冲突 module.exports = { "printWidth": 120, //一行的字符数,若是超过会进行换行,默认为80 "tabWidth": 2, "useTabs": false, // 注意:makefile文件必须使用tab,视具体状况忽略 "singleQuote": true, "semi": true, "trailingComma": "none", //是否使用尾逗号,有三个可选值"<none|es5|all>" "bracketSpacing": true, //对象大括号直接是否有空格,默认为true,效果:{ foo: bar } };
修改eslint配置.eslintrc.js
:web
module.exports = { "extends": [ "eslint:recommended", "plugin:react/recommended", "plugin:jsx-control-statements/recommended", // 须要另外配合babel插件使用 "prettier" // 注意顺序 ], "plugins": ["@typescript-eslint", "react", "jsx-control-statements", "prettier"], // 注意顺序 "rules": { "prettier/prettier": 2, // 这样prettier的提示可以以错误的形式在控制台输出 } };
而后咱们要配置lint-staged
,在提交代码时自动格式化代码。
修改package.json
:
"husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "src/**/*.{jsx,js,tsx,ts}": [ "prettier --write", "eslint --fix", "git add" ] }
有些编辑器可以根据配置提示会自动格式化代码,咱们能够为各类编辑器提供一个统一的配置。
在根目录新建.editorconfig
便可,注意不要与已有的lint规则冲突:
root = true [*] charset = utf-8 indent_style = space indent_size = 2 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true
使用jest能够帮助咱们测试代码,在项目中使用jest的实现方式有不少种,文本不具体展开讨论,只提供一些必备的工具和配置。
必备工具:
$ npm i -D jest babel-jest ts-jest @types/jest
参考配置jest.config.js
,测试文件均放在__test__
目录中:
module.exports = { transform: { '^.+\\.tsx?$': 'ts-jest', }, testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], };