使用eslint校验代码
每个程序员都有自己的编码习惯,最常见的莫过于: 有的人写代码一行代码结尾必须加分号 ;,有的人觉得不加分号 ; 更好看; 有的人写代码一行代码不会超过 80 个字符,认为这样看起来简洁明了,有的人喜欢把所有逻辑都写在一行代码上,觉得别人看不懂的代码很牛逼;
所以使用使用统一的规则来约束代码风格非常重要。
$ npm install eslint --save-dev
eslint --init
$ npm run lint:create
> 20170811@0.1.0 lint:create D:\code\test\20170811
> eslint --init
? How would you like to configure ESLint? Answer questions about your style
? Are you using ECMAScript 6 features? Yes
? Are you using ES6 modules? Yes
? Where will your code run? Browser
? Do you use CommonJS? Yes
? Do you use JSX? Yes
? Do you use React? Yes
? What style of indentation do you use? Tabs
? What quotes do you use for strings? Double
? What line endings do you use? Windows
? Do you require semicolons? Yes
? What format do you want your config file to be in? JavaScript
Installing eslint-plugin-react@latest
package.json
"scripts": {
"test": "react-scripts test --env=jsdom",
"lint": "eslint src",
"lint:create": "eslint --init"
}
setting.json中设置
"eslint.validate": [],
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"files.eol": "\n",
"editor.tabSize": 2
.eslintrc文件
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: ['airbnb-base'],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
rules: {
quotes: [1, 'single'],
'quote-props': [2, 'as-needed'],
'comma-dangle': [1, 'always-multiline'],
'eol-last': [0, 'always'],
},
};
使用prettier自动修复
vscode安装第一个 setting.json中设置
"prettier.trailingComma": "all",
"prettier.vueIndentScriptAndStyle": true,
"git.ignoreMissingGitWarning": true,
"prettier.requireConfig": false,
"prettier.singleQuote": true,
"prettier.useTabs": false,
"prettier.tabWidth": 2,
|