import/first 风格
它的作用
禁止在导入语句之前出现任何非导入语句,除非是指令。
为什么这是个问题?
值得注意的是,导入语句会被提升(hoisted),这意味着在导入模块之间的任意语句之前,导入的模块就会被求值。 将所有导入语句集中放在文件顶部,可以避免由于该语言规范特性而带来的意外行为。
示例
此规则的 错误 示例:
js
import { x } from "./foo";
export { x };
import { y } from "./bar";此规则的 正确 示例:
js
import { x } from "./foo";
import { y } from "./bar";
export { x, y };配置
此规则接受以下字符串值之一:
"absolute-first"
强制绝对导入在相对导入之前列出。
使用 "absolute-first" 时的 错误 示例:
js
import { x } from "./foo";
import { y } from "bar";使用 "absolute-first" 时的 正确 示例:
js
import { y } from "bar";
import { x } from "./foo";"disable-absolute-first"
禁用绝对导入优先的行为。 这是默认行为。
如何使用
要通过配置文件或 CLI 启用 此规则,可以使用:
json
{
"plugins": ["import"],
"rules": {
"import/first": "error"
}
}bash
oxlint --deny import/first --import-plugin