unicorn/prefer-export-from
Require export ... from to re-export instead of an import and a separate export
has suggestions
An import followed by an export of the same name takes two statements, often far apart, to say one thing. export { parse } from "./parser.js" says it in one place and doesn't add a name to the file's scope.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/prefer-export-from" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"unicorn/prefer-export-from" = ["error", {
checkUsedVariables = true,
}]Examples#
example.js1 problem
import { parse } from "./parser.js";Prefer re-exporting directly from the source module.
export { parse };example.js2 problems
import Button from "./button.js";Prefer re-exporting directly from the source module.
import * as icons from "./icons.js";Prefer re-exporting directly from the source module.
export default Button;
export { icons };example.jsNo problems
export { parse } from "./parser.js";
export { default } from "./button.js";
export * as icons from "./icons.js";Options#
| Option | Default | What it does |
|---|---|---|
checkUsedVariables | true | Also report an import that the file uses for something other than exporting it. |
By default, this is reported even though the file needs the import for parse(""):
example.js1 problem
import { parse } from "./parser.js";Prefer re-exporting directly from the source module.
export { parse };
export const empty = parse("");To fix it, keep the import and re-export from the module as well:
example.jsNo problems
import { parse } from "./parser.js";
export { parse } from "./parser.js";
export const empty = parse("");With false, the rule skips an import statement as soon as the file uses one of its names:
example.jswith
{ "checkUsedVariables": false }No problemsimport { parse } from "./parser.js";
export { parse };
export const empty = parse("");