ESLint
ESLint
代码检查工具!从 TSLint 到 ESLint - 掘金 (juejin.cn)
Roadmap: TSLint -> ESLint · Issue #4534 · palantir/tslint (github.com)
Linting your TypeScript Codebase | TypeScript ESLint (typescript-eslint.io)
2019 年 1 月,TypeScript 官方决定全面采用 ESLint,之后也发布 typescript-eslint 项目,以集中解决 TypeScript 和 ESLint 兼容性问题。而之前的两个 lint 解决方案都将弃用:
typescript-eslint-parser已停止维护- 在完成
ESLint功能后,将弃用TSLint并帮助用户迁移到ESLint
安装
pnpm i eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin -D
@typescript-eslint/parser为ESLint提供解析器。
@typescript-eslint/eslint-plugin它作为ESLint默认规则的补充,提供了一些额外的适用于ts语法的规则。
配置文件
.eslintrc.js
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
};
如上是最小化的一个配置文件
parser: '@typescript-eslint/parser,'告诉
ESLint使用@typescript-eslint/parser作为parser package这可以使
ESLint可以理解TypeScript语法不这样写的话会使
ESLint像往常解析JS一样解析TS, 自然就会报错plugins: [ '@typescript-eslint', ],告诉
ESLint加载安装好的@typescript-eslint/eslint-pluginplugin package这将允许你在代码库中使用这些
rulesextends: [ 'eslint:recommended', 'plugin:@typescript-eslint/recommended', ],
extends属性告诉ESLint你的配置extends(扩展)了给定配置
'eslint:recommended'是ESLint内置的 "推荐配置" ---- 他给出一个小的,合理的rules集, 这些rules是众所周知的最佳实践的lint'plugin:@typescript-eslint/recommended'是官方的 "建议配置" --- 它就像eslint:recomment一样, 只不过它只针对TypeScript-specific插件中的rules
module报错:'module' is not defined. eslint(no-undef)
typescript-eslint config: .eslintrc file 'module' is not defined - Stack Overflow
env里加上node:true即可解决module.exports = { root: true, parser: "@typescript-eslint/parser", // Specifies the ESLint parser plugins: ["@typescript-eslint"], extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], env: { node: true, // 解决 module 报错 }, };
配置 ignore files
在根目录下再创建一个 .eslintignore 文件, 它会告诉 ESLint 不要 lint 哪些文件(夹)
# don't lint build output (make sure it's set to your correct build folder name)
dist

