TSLint-Angular 配置详解

本文详细介绍了如何配置 TSLint 以规范 TypeScript 和 Angular 项目的代码风格,包括安装步骤、编辑器插件、常用规则及其应用场景。重点讲解了 TSLint 规则如 arrow-return-shorthand、callable-types、class-name 等,旨在提升代码质量与可读性。同时,还提及了 Angular 特有的 TSLint 规则,如禁用 ViewEncapsulation.None,使用 @Input 和 @Output 等。
摘要由CSDN通过智能技术生成

代码风格和语义的检查工具,帮助规范 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值