简体   繁体   English

为什么在编译Typescript文件后应该执行TSlint?

[英]Why shoudl TSlint be executed after Typescript files is compiled?

According to the official TSLint, TSLint should executed only after the Typescript code is compiled. 根据官方的TSLint,TSLint仅应在Typescript代码编译后执行。 What is the reason behind this? 这背后的原因是什么?

The official TSLint doc: https://palantir.github.io/tslint/usage/cli/ TSLint官方文档: https ://palantir.github.io/tslint/usage/cli/

TSLint runs against a parsed AST (Abstract Syntax Tree) that it receives from TypeScript. TSLint针对从TypeScript接收到的已解析的AST(抽象语法树)运行。 If the AST it receives from TypeScript has errors, its rules' logic might make some incorrect assumptions. 如果从TypeScript接收到的AST有错误,则其规则的逻辑可能会做出一些不正确的假设。

Defining a few of those terms: 定义其中一些术语:

  • AST: Tree-like representation of your code that the linter will analyze. AST:Linter将分析的代码的树状表示形式。 You can play around with astexplorer.net to see what this looks like; 您可以使用astexplorer.net来看看它是什么样子。 it's pretty nifty! 这很漂亮! 😊 😊
  • Syntax errors: if you write code such as led foo = "bar"; 语法错误:如果您编写代码,例如led foo = "bar"; ( led instead of let ) . led而不是let TypeScript will create a different representation of your code than you might expect. TypeScript将创建与您预期不同的代码表示形式。
  • Semantic errors: if you write code that has no syntax errors but the type checker finds issues with, such as let foo: number = "bar"; 语义错误:如果编写的代码没有语法错误,但类型检查器发现问题,例如let foo: number = "bar"; .

TSLint has an issue open to discuss whether it shouldn't bother checking code that has syntax or semantic errors: https://github.com/palantir/tslint/issues/3808 TSLint有一个未解决的问题,可以讨论是否应该麻烦检查具有语法或语义错误的代码: https : //github.com/palantir/tslint/issues/3808

Two additional points in favor of not running TSLint on invalid source files: 还有两个要点:不要在无效的源文件上运行TSLint:

  • A lot of people have trouble differentiating between TS and TSLint errors, so it can be confusing when an invalid file results in a lot of lint errors 很多人在区分TS和TSLint错误时遇到麻烦,因此当无效文件导致很多皮棉错误时,可能会造成混淆
  • TS code that doesn't compile tends to have different linting issues after it gets fixed, so it's a bit of a waste to ask people to fix them along with the TS errors 固定后的TS代码往往会出现不同的掉毛问题,因此,要求人们将其与TS错误一起修复是很浪费的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM