参考网址一
EB命名规范
- 文件名采用短横线连接的方式
- 公共组件采取大驼峰的命名方式
- 原则上不推荐采用中文拼音命名,想用中文拼音的必须全拼且加上注释
- CSS中class命名采取短横线连接的方式
- 变量声明时,能用 const 就不用 let。全局常量需使用大写,如 USER_ID
JavaScript编码规范
- 字符串使用单引号
- 语句结束省略分号
- 使用 === 进行等于判断
- 代码缩进采取两空格
CSS编码规范
- 不推荐行内样式
- 推荐使用less
vue相关规范
父子组件props规范
设置引用类型的默认值应该要是一个函数,返回一个对象
// good
props: {
objectExample: {
type: Object,
default: () => {
return {}
}
}
}
// good
props: {
objectExample: {
type: Object,
default() {
return {}
}
},
}
// bad
props: ['objectExample']
// bad
props: {
objectExample: {
type: Object,
default: {}
}
}
注释规范
- 关键判断注释
- 关键变量注释
- 头部注释及函数注释,如下:
注释采用VScode编辑器的插件koroFileHeader生成
生成文件头部注释ctrl+alt+i
例如:
<!--
* @Author: litaolin
* @Date: 2020-08-12 11:32:44
* @LastEditTime: 2020-08-26 10:51:36
* @LastEditors: litaolin
* @Description: live
-->
在光标处生成函数注释ctrl+alt+t
例如:
/**
* @description: live
* @param name {string}
* @param id {number}
* @return {boolean}
*/
VScode setting.json配置
{
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
"git.autofetch": true,
"editor.tabSize": 2,
"eslint.codeActionsOnSave ": true, // 每次保存的时候将代码按eslint格式进行修复
"prettier.eslintIntegration": true, //让prettier使用eslint的代码格式进行校验
"prettier.semi": false, //去掉代码结尾的分号
"prettier.singleQuote": true, //使用单引号替代双引号
"javascript.format.insertSpaceBeforeFunctionParenthesis": true, //让函数(名)和后面的括号之间加个空格
"vetur.format.defaultFormatter.html": "js-beautify-html", //格式化.vue中html
"vetur.format.defaultFormatter.js": "vscode-typescript", //让vue中的js按编辑器自带的ts格式进行格式化
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
"wrap_attributes": "force-aligned" //属性强制折行对齐
}
},
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "html",
"autoFix": true
},
{
"language": "vue",
"autoFix": true
}
],
// 头部注释
"fileheader.customMade": {
// 头部注释默认字段
"Author": "李滔林",
"Date": "Do not edit", // 设置后默认设置文件生成时间
"LastEditTime": "Do not edit", // 设置后,保存文件更改默认更新最后编辑时间
"LastEditors": "李滔林", // 设置后,保存文件更改默认更新最后编辑人
"Description": ""
},
"editor.detectIndentation": false,
"editor.quickSuggestions": {
"strings": true
},
"fileheader.configObj": {
"createHeader": true,
"autoAdd": false,
"prohibitAutoAdd": ["json", "md"] // 禁止.json .md文件,自动添加头部注释
},
"[javascript]": {
"editor.defaultFormatter": "HookyQR.beautify"
},
"window.zoomLevel": 0,
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"git.enableSmartCommit": true,
"git.confirmSync": false
}
VScode 相关插件(欢迎补充)
必选
- eslint // 配置见setting.json
- koroFileHeader // 文件注释和函数注释插件,约定配置项已在setting.json中配置
- vetur // 支持vue插件
推荐选择
- ant design vue helper // ant design vue的html提示插件
- auto close tag // html标签闭合插件
- auto rename tag //html标签重命名同步插件
- bracket pair colorizer // 花括号颜色插件
- open in browser // 浏览器打开插件
ESlint规则
规则对照地址
eslint错误级别:
- “off” or 0 - 关闭规则
- “warn” or 1 - 将规则视为一个警告(不会影响退出码)
- “error” or 2 - 将规则视为一个错误 (退出码为1)
module.exports = {
root: true,
env: { // 运行环境
browser: true,
node: true
},
parserOptions: { // ES6解析相关配置
parser: 'babel-eslint'
},
extends: ['plugin:vue/recommended', 'eslint:recommended', '@vue/standard'], // 基础配置中的已启用的规则继承
// 启用eslint的推荐规则
rules: { // 验证规则
"vue/max-attributes-per-line": [2, { // 多个特性是否换行
"singleline": 10,
"multiline": {
"max": 1,
"allowFirstLine": false
}
}],
"vue/singleline-html-element-content-newline": "off", // html标签及内容是否换行
"vue/multiline-html-element-content-newline":"off",
"vue/name-property-casing": ["error", "PascalCase"],
"vue/no-v-html": "off",
'accessor-pairs': 2, // 在对象中使用getter/setter
'arrow-spacing': [2, { // =>函数的=>前后都至少需要一个空格
'before': true,
'after': true
}],
'block-spacing': [2, 'always'], // 强制在代码块中开括号({)前和闭括号(})后有空格
'brace-style': [2, '1tbs', { // 强制在代码块中使用一致的大括号风格
'allowSingleLine': true // 左大括号与控制语句在同一行上
}], // 右大括号应与左大括号位于同一行,或位于前一个块之后的行上
'camelcase': [0, { // 关闭强制属性使用骆驼拼写法命名约定
'properties': 'always'
}],
'comma-dangle': [2, 'never'], // 禁止末尾逗号
'comma-spacing': [2, { // 强制在逗号前后使用一致的空格
'before': false, // (默认)禁止在逗号前使用空格
'after': true // (默认)要求在逗号后使用一个或多个空格
}],
'comma-style': [2, 'last'], // 强制使用一致的逗号风格 (默认) 要求逗号放在数组元素、对象属性或变量声明之后,且在同一行
'constructor-super': 2, // 要求在构造函数中有 super() 的调用
'curly': [2, 'all'], // 可以不写 (默认)强制所有控制语句使用一致的括号风格且必须使用括号
'dot-location': [2, 'property'], // 强制点号操作符应该和属性在同一行
'eol-last': 2, // 要求文件末尾存在一行空行
'eqeqeq': ["error", "always", {"null": "ignore"}], // 要求使用 === 和 !==
'generator-star-spacing': [2, { // 强制 generator 函数中 * 号周围使用一致的空格
'before': true, // *前后都需要空格
'after': true
}],
'handle-callback-err': [2, '^(err|error)$'], // 可以不写 要求nodejs和commonjs回调函数中有容错处理nodejs
'indent': [2, 2, { // 强制 switch 语句中的 case 子句的缩进2个空格
'SwitchCase': 1
}],
'jsx-quotes': [2, 'prefer-single'], // 强制所有不包含单引号的 JSX 属性值使用单引号
'key-spacing': [2, { // 对象字面量的键和值之间使用一致的空格
'beforeColon': false, // 禁止在对象字面量的键和冒号之间存在空格
'afterColon': true // 要求在对象字面量的冒号和值之间存在至少有一个空格
}],
'keyword-spacing': [2, { // 强制在关键字前后使用一致的空格
'before': true, // 要求在关键字之前至少有一个空格
'after': true // 要求在关键字之后至少有一个空格
}],
'new-cap': [2, { // 要求构造函数首字母大写
'newIsCap': true, // 要求调用 new 操作符时有首字母大小的函数
'capIsNew': false // 允许调用首字母大写的函数时没有 new 操作符
}],
'new-parens': 2, // 当使用 new 关键字调用没有参数的构造函数时,强制括号
'no-array-constructor': 2, // 禁用 Array 构造函数
'no-caller': 2, // 禁用 arguments.caller 或 arguments.callee
'no-console': 'off', // 允许使用console
'no-class-assign': 2, // 禁止修改类声明的变量
'no-cond-assign': 2, // 禁止在条件语句中出现赋值操作符 eslint:recommended
'no-const-assign': 2, // 禁止修改 const 声明的变量 eslint:recommended
'no-control-regex': 0, // 禁止在正则表达式中使用控制字符 eslint:recommended
'no-delete-var': 2, // 禁止删除变量 eslint:recommended
'no-dupe-args': 2, // 禁止 function 定义中出现重名参数 eslint:recommended
'no-dupe-class-members': 2, // 禁止类成员中出现重复的名称 eslint:recommended
'no-dupe-keys': 2, // 禁止对象字面量中出现重复的 key eslint:recommended
'no-duplicate-case': 2, // 禁止出现重复的 case 标签 eslint:recommended
'no-empty-character-class': 2, // 禁止在正则表达式中使用空字符集 eslint:recommended
'no-empty-pattern': 2, // 禁止使用空解构模式 eslint:recommended
'no-eval': 2, // 禁用 eval()
'no-ex-assign': 2, // 禁止对 catch 子句的参数重新赋值 eslint:recommended
'no-extend-native': 2, // 禁止扩展原生类型
'no-extra-bind': 2, // 禁止不必要的 .bind() 调用
'no-extra-boolean-cast': 2, // 禁止不必要的布尔转换 eslint:recommended
'no-extra-parens': [2, 'functions'],
'no-fallthrough': 2,
'no-floating-decimal': 2,
'no-func-assign': 2,
'no-implied-eval': 2,
'no-inner-declarations': [2, 'functions'],
'no-invalid-regexp': 2,
'no-irregular-whitespace': 2,
'no-iterator': 2,
'no-label-var': 2,
'no-labels': [2, {
'allowLoop': false,
'allowSwitch': false
}],
'no-lone-blocks': 2,
'no-mixed-spaces-and-tabs': 2,
'no-multi-spaces': 2,
'no-multi-str': 2,
'no-multiple-empty-lines': [2, {
'max': 1
}],
'no-native-reassign': 2,
'no-negated-in-lhs': 2,
'no-new-object': 2,
'no-new-require': 2,
'no-new-symbol': 2,
'no-new-wrappers': 2,
'no-obj-calls': 2,
'no-octal': 2,
'no-octal-escape': 2,
'no-path-concat': 2,
'no-proto': 2,
'no-redeclare': 2,
'no-regex-spaces': 2,
'no-return-assign': [2, 'except-parens'],
'no-self-assign': 2,
'no-self-compare': 2,
'no-sequences': 2,
'no-shadow-restricted-names': 2,
'no-spaced-func': 2,
'no-sparse-arrays': 2,
'no-this-before-super': 2,
'no-throw-literal': 2,
'no-trailing-spaces': 2,
'no-undef': 2,
'no-undef-init': 2,
'no-unexpected-multiline': 2,
'no-unmodified-loop-condition': 2,
'no-unneeded-ternary': [2, {
'defaultAssignment': false
}],
'no-unreachable': 2,
'no-unsafe-finally': 2,
'no-unused-vars': [2, {
'vars': 'all',
'args': 'none'
}],
'no-useless-call': 2,
'no-useless-computed-key': 2,
'no-useless-constructor': 2,
'no-useless-escape': 0,
'no-whitespace-before-property': 2,
'no-with': 2,
'one-var': [2, {
'initialized': 'never'
}],
'operator-linebreak': [2, 'after', {
'overrides': {
'?': 'before',
':': 'before'
}
}],
'padded-blocks': [2, 'never'],
'quotes': [2, 'single', {
'avoidEscape': true,
'allowTemplateLiterals': true
}],
'semi': [2, 'never'],
'semi-spacing': [2, {
'before': false,
'after': true
}],
'space-before-blocks': [2, 'always'],
'space-before-function-paren': [2, 'never'],
'space-in-parens': [2, 'never'],
'space-infix-ops': 2,
'space-unary-ops': [2, {
'words': true,
'nonwords': false
}],
'spaced-comment': [2, 'always', {
'markers': ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']
}],
'template-curly-spacing': [2, 'never'],
'use-isnan': 2,
'valid-typeof': 2,
'wrap-iife': [2, 'any'],
'yield-star-spacing': [2, 'both'],
'yoda': [2, 'never'],
'prefer-const': 2,
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
'object-curly-spacing': [2, 'always', {
objectsInObjects: false
}],
'array-bracket-spacing': [2, 'never']
}
}
ESlint规则详解
no-cond-assign 禁止条件表达式中出现赋值操作符
// bad
if (x = 0) {
...
}
// good
if (x === 0) {
...
}
no-dupe-args 禁止 function 定义中出现重名参数
// bad
function foo(a, b, a) {
}
// good
function foo(a, b, c) {
}
no-extra-bind 禁止不必要的 .bind() 调用
// bad
var boundGetName = (function getName() {
return "ESLint";
}).bind({ name: "ESLint" });
console.log(boundGetName());
// good
var boundGetName = (function getName() {
return this.name;
}).bind({ name: "ESLint" });
console.log(boundGetName());
no-extra-boolean-cast 禁止不必要的布尔转换
// bad
if (!!foo) {
// ...
}
// good
if (foo) {
// ...
}