@CSS魔法
2017. 11. 10
编码规范的 “目的” 是什么?
设计思路:
编码规范需要包含哪些内容?
开发阶段如何使用 ESLint ?
如何设置 CLI ?
$ npm i -g eslint
$ npm i -g babel-eslint
如何使用 CLI ?
$ cd my/haojing/htdocs/assets
$ eslint .
有没有更简单使用方式?
$ cd my/haojing/htdocs/assets
$ npm install
$ npm run lint-js
如何设置 IDE ?
如何设置 IDE ?
如何落实编码规范?
✓
✓
✓
规范相关文档
https://github.com/baixing/Notes
ESLint 校验规则
可能会持续增加……
文件位置
htdocs/assets/mobile/.eslintrc.js
很快会扩展到 PC 站和构建脚本!
覆盖面
手机站脚本
其实一个命令就可以了……
$ eslint --fix my.js
……才怪!
居然也有文档
https://github.com/baixing/Notes
no-undef
禁止使用未定义的变量。
其实大多数时候我们是在引用一个全局变量。
//namespace
window.Auth = { /* ... */ }
//init
if (page && Auth[page]) {
Auth[page]()
}
//namespace
window.Auth = { /* ... */ }
//init
if (page && Auth[page]) {
Auth[page]()
}
//namespace
window.Auth = { /* ... */ }
const Auth = window.Auth
//init
if (page && Auth[page]) {
Auth[page]()
}
no-empty
禁止出现空的代码块。
包括 if / for / try / catch 等。
if (foo) {
} else {
bar()
}
if (foo) {
} else {
bar()
}
if (!foo) {
bar()
}
try {
bar()
} catch (e) {}
try {
bar()
} catch (e) {}
try {
bar()
} catch (e) {
// 静默失败
}
no-empty-function
禁止出现空函数。
Thank You!