no-restricted-exports
Disallow the export names you list, or default exports
Some export names cause trouble. A module that exports a function named then looks like a promise, so await import("./module.js") calls that function instead of giving you the module. Your project may also want named exports only, so everything keeps the same name wherever it's imported.
The rule does nothing until you give it options.
Configure#
Off by default. To turn it on:
[lint.rules]
"no-restricted-exports" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
[lint.rules]
"no-restricted-exports" = ["error", {
restrictedNamedExports = [], # list of strings
# restrictedNamedExportsPattern = <string>,
restrictDefaultExports = {
# direct = <true or false>,
# named = <true or false>,
# defaultFrom = <true or false>,
# namedFrom = <true or false>,
# namespaceFrom = <true or false>,
},
}]Examples#
[lint.rules]
no-restricted-exports = ["error", { restrictedNamedExports = ["then"] }]{ "restrictedNamedExports": ["then"] }1 problemexport function thenthen is restricted from being used as an exported name.(callback) {
callback("ready");
}{ "restrictedNamedExports": ["then"] }No problemsexport function whenReady(callback) {
callback("ready");
}The rule checks the name that other modules see, not the local one:
{ "restrictedNamedExports": ["then"] }1 problemfunction whenReady(callback) {
callback("ready");
}
export { whenReady as thenthen is restricted from being used as an exported name. };Options#
| Option | Default | What it does |
|---|---|---|
restrictedNamedExports | [] | Names that can't be exported. |
restrictedNamedExportsPattern | none | A regular expression. Every exported name that matches it is reported, except default. |
restrictDefaultExports | {} | Which ways of writing a default export are reported. See the second table. |
With { restrictedNamedExportsPattern: "^_" }:
{ "restrictedNamedExportsPattern": "^_" }1 problemexport const _cache_cache is restricted from being used as an exported name. = new Map();Each key of restrictDefaultExports is false by default:
| Key | What it reports |
|---|---|
direct | export default value |
named | export { value as default } |
defaultFrom | export { default } from "./user.js" |
namedFrom | export { user as default } from "./user.js" |
namespaceFrom | export * as default from "./user.js" |
With { restrictDefaultExports: { direct: true, named: true } }:
{ "restrictDefaultExports": { "direct": true, "named": true } }1 problemexport default class User {}Exporting default is restricted.Any form you didn't turn on is still allowed:
{ "restrictDefaultExports": { "direct": true, "named": true } }No problemsexport { default } from "./user.js";