代码风格和语义的检查工具,帮助规范 TS 和 Angular 的代码书写;
(TSLint 的官方文档全英文且极其简略,不友好 : ( )
安装:
=> cd smartisan2017
=> cnpm install // 安装相关依赖,包括 codelyzer、tslint-angular 及依赖
全局安装 tslint & typescript
cnpm install -g tslint typescript
安装编辑器插件
https://palantir.github.io/tslint/usage/third-party-tools/
(VSCode, Atom, WebStorm)
如果编辑器不可用,只好用 CLI
// After install Depandences
=> cd <Project>
=> tslint -c tslint.json <文件相对路径> | -p <对应模块的 tsconfig.json>
// 会在命令行中,输出错误的位置和原因
{
"rulesDirectory": ["codelyzer"],
"rules": {
// 箭头函数简化:Suggests to convert () => { return x; } to () => x.
"arrow-return-shorthand": true,
// An interface or literal type with just a call signature can be written as a function type.
// 坐等有识之士,
// TT:一个接口可调用,需要声明为 Function 类型
"callable-types": true,
// 类采用 PascalCase 命名法,大驼峰命名法
"class-name": true,
// 强制单行注释格式:'//' 后必跟空格
"comment-format": [true, "check-space"],
// 任何情况下,if / for / do / while 后必须通过 {} 包裹代码块
"curly": true,
// 不允许使用 @deprecated 声明废弃的 API,应该升级或删除(基本用不到。。)
"deprecation": {
"severity": "warn"
},
// 文件必须用新的行结束,原因参考:
// https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline
"eofline": true,
// 当使用 for