JavaScript 常见问题解答
我的 lint 规则之一在纯 JavaScript 文件上无法正常工作
¥One of my lint rules isn't working correctly on a pure JavaScript file
这是可以预料到的 - ESLint 规则不会故意检查文件扩展名,因为这会在使用非标准扩展名的环境中导致问题(例如,.vue
和 .md
文件都可以包含要进行 linted 的 TypeScript 代码)。
¥This is to be expected - ESLint rules do not check file extensions on purpose, as it causes issues in environments that use non-standard extensions (for example, a .vue
and a .md
file can both contain TypeScript code to be linted).
如果你有一些纯 JavaScript 代码,不想对其应用某些 lint 规则,那么你可以使用 ESLint 的 files
配置 来执行以下操作:
¥If you have some pure JavaScript code that you do not want to apply certain lint rules to, then you can use ESLint's files
configuration to either:
-
(推荐)仅在 TypeScript 文件扩展名上启用 TypeScript 特定的规则
¥(recommended) only enable TypeScript-specific rules on TypeScript file extensions
-
关闭 JavaScript 专用文件扩展名上的 TypeScript 特定规则。
¥turn off TypeScript-specific rules on JavaScript-only file extensions
我应该在转译的输出 JavaScript 文件上运行 ESLint 吗?
¥Should I run ESLint on transpiled output JavaScript files?
不。
¥No.
源 TypeScript 文件包含输出 JavaScript 文件的所有内容以及类型注释。对输出 JavaScript 文件进行 linting 并没有什么好处。
¥Source TypeScript files have all the content of output JavaScript files, plus type annotations. There's no benefit to also linting output JavaScript files.