@CSS魔法
2017. 11. 10
(45min)
编码规范的 “目的” 是什么?
编码规范需要包含哪些内容?
如何设计一套可落地的编码规范?
设计思路:
如何落实编码规范?
常见的 JS 校验工具:
最流行、功能最丰富、
高度可配置、插件化、
易扩展……
✓
ESLint 校验规则
可能会持续增加……
规范相关文档
https://github.com/*******/*****
ESLint 如何集成到现有工作流?
补充:主流框架的脚手架通常都已集成 ESLint。
开发阶段如何使用 ESLint ?
如何设置 ESLint CLI ?
$ npm i -g eslint
$ npm i -g babel-eslint
如何使用 ESLint CLI ?
$ cd path/to/my/project
$ eslint .
有没有更简单的使用方式?
$ cd path/to/my/project
$ npm install
$ npm run lint-js
当然,需要提前定义好开发依赖和 npm script
如何设置 IDE ?(WebStorm 原生支持)
IDE 的提示效果:
其实一个命令就可以了……
$ eslint --fix my.js
……才怪!
居然也有文档
https://github.com/*******/*****
no-undef
eqeqeq
no-use-before-define
no-loop-func
no-extra-boolean-cast
no-constant-condition
no-implicit-coercion
no-used-expressions
...
最容易撞的线:
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!