Skip to content
← Back to rules

import/unambiguous Restriction

它的作用

如果一个 module 可能会被误解析为 script,而不是作为一个纯粹的 ES 模块,则发出警告。

为什么这不好?

对于仅支持 ESM 的环境,含义不明确的文件可能会导致意外结果和问题。

示例

以下是此规则的错误代码示例:

js
function x() {}

(function x() {
  return 42;
})();

以下是此规则的正确代码示例:

js
import "foo";
function x() {
  return 42;
}

export function x() {
  return 42;
}

(function x() {
  return 42;
})();
export {}; // 将仅有副作用的文件标记为 'module' 的简单方法,无需任何 imports/exports

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["import"],
  "rules": {
    "import/unambiguous": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["import"],
  rules: {
    "import/unambiguous": "error",
  },
});
bash
oxlint --deny import/unambiguous --import-plugin

版本

此规则是在 v0.11.1 中添加的。

参考资料