With import * as api from "./api", reading a name that ./api doesn't export gives undefined, and nothing fails until you call it. Assigning to api.save throws, because a namespace object is read-only.
The rule reads the imported file and checks api.name, const { name } = api and <api.Name />. A computed read like api[key] can't be checked, so it's reported too unless you allow it.
It reads JavaScript and TypeScript files, including JSX. With an ESLint config it only reads .js, .mjs and .cjs files, unless settings["import/extensions"] lists others.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"import/namespace" = "error" # or "warn"
Every option, set to its default:
bunfig.toml
[lint.rules]
"import/namespace" = ["error", {
allowComputed = false,
}]
Examples
Reported:
export function save(item) {}
export function load(id) {}
import * as api from "./api.js";
api.remove("draft");
const { update } = api;
api.save = () => {};
const action = "load";
api[action]("draft");
Not reported:
export function save(item) {}
export function load(id) {}
import * as api from "./api.js";
api.save({ id: "draft" });
const { load } = api;
load("draft");
Options
| Option | Default | What it does |
|---|---|---|
allowComputed | false | Allow computed reads such as api[key]. They aren't checked. |
export function save(item) {}
export function load(id) {}
import * as api from "./api.js";
const action = "load";
api[action]("draft");