import/named Nursery
作用
验证所有命名导入都属于所引用模块中的命名导出集合。
对于 export,验证所有命名导出都存在于所引用模块中。
注意:对于包,插件会从 jsnext:main(已弃用)或 module 中查找导出的名称,如果它们存在于 package.json 中的话。Redux 的 npm 模块包含这个键,因此可以被 lint,例如。
被忽略的模块路径,或者不能明确识别为 ES 模块的路径,在导入时不会被报告。请注意,Flow 中使用的类型导入和导出始终会被忽略。
为什么这很糟糕?
导入或导出在所引用模块中不存在的名称,可能会导致运行时错误和混淆。它可能暗示某些功能可用,但实际上并不可用,从而使代码更难维护和理解。此规则有助于确保你的代码准确反映可用的导出,从而提高可靠性。
示例
给定
js
// ./foo.js
export const foo = "我太 foo 了";此规则的错误代码示例:
js
// ./baz.js
import { notFoo } from "./foo";
// 重新导出
export { notFoo as defNotBar } from "./foo";
// 如果可用,将跟随 'jsnext:main'
import { dontCreateStore } from "redux";此规则的正确代码示例:
js
// ./bar.js
import { foo } from "./foo";
// 重新导出
export { foo as bar } from "./foo";
// 默认情况下,不会分析没有 jsnext:main 的 node_modules
// (import/ignore 设置)
import { SomeNonsenseThatDoesntExist } from "react";如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["import"],
"rules": {
"import/named": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["import"],
rules: {
"import/named": "error",
},
});bash
oxlint --deny import/named --import-plugin版本
此规则是在 v0.0.13 中添加的。
