useValidNamespaceImports (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.8 - Diagnostic Category:
lint/nursery/useValidNamespaceImports - This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
- Same as
import/namespace
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useValidNamespaceImports": "error" } } }}Description
Section titled “Description”Require that names read from a namespace import are exported by the imported module.
A namespace import, such as import * as utils from "./utils.js", puts every export of a
module into one object. Reading a name that the module doesn’t export gives undefined,
which usually means the name has a typo, or the export was renamed or removed.
This rule reports:
- reading a name from a namespace import that the imported module doesn’t export,
including names read through destructuring, such as
const { name } = utils, and JSX elements, such as<Components.Button />; - assigning a value to a member of a namespace import, which throws an error at runtime because namespace imports are read-only;
- namespace imports and
export * as name fromstatements of a module that has no exports.
The rule also follows namespaces that a module exports, such as
export * as helpers from "./helpers.js", so utils.helpers.missing is checked as well.
The rule only checks modules that use import or export statements. It skips CommonJS
modules and TypeScript modules that use export =, because their exports can’t be
determined reliably.
Note that if you use TypeScript, you probably don’t need this rule, since TypeScript already reports these mistakes.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”export const add = (a, b) => a + b;import * as utils from "./utils.js";
utils.subtract(2, 1);/invalid-read.js:3:7 lint/nursery/useValidNamespaceImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The namespace utils has no export named subtract.
1 │ import * as utils from "./utils.js";
2 │
> 3 │ utils.subtract(2, 1);
│ ^^^^^^^^
4 │
ℹ Reading a name that a module doesn't export gives undefined.
ℹ Check the spelling of the name, or export it from the module.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
import * as utils from "./utils.js";
const { subtract } = utils;/invalid-destructuring.js:3:9 lint/nursery/useValidNamespaceImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The namespace utils has no export named subtract.
1 │ import * as utils from "./utils.js";
2 │
> 3 │ const { subtract } = utils;
│ ^^^^^^^^
4 │
ℹ Reading a name that a module doesn't export gives undefined.
ℹ Check the spelling of the name, or export it from the module.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
import * as utils from "./utils.js";
utils.add = () => 0;/invalid-assignment.js:3:1 lint/nursery/useValidNamespaceImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The members of the namespace utils can't be reassigned.
1 │ import * as utils from "./utils.js";
2 │
> 3 │ utils.add = () => 0;
│ ^^^^^^^^^
4 │
ℹ Namespace imports are read-only, so this assignment throws an error at runtime.
ℹ To change a value that a module exports, export a function from that module that changes it.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
export const add = (a, b) => a + b;import * as utils from "./utils.js";
utils.add(2, 1);const { add } = utils;See Also
Section titled “See Also”- If you want to report imports of names that a module doesn’t export, see
noUnresolvedImports. - If you want to report reading a namespace import with a computed key, such as
utils[name], seenoDynamicNamespaceImportAccess.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.