# no-restricted-exports
禁止在导出中指定名称
在项目中,出于各种原因,某些名称可能不允许用作导出名称。
# 规则详情
此规则不允许将指定名称用作导出名称。
# 选项
默认情况下,此规则不允许任何名称。只有您在配置中指定的名称才会被禁止。
此规则有一个对象选项:
"restrictedNamedExports"
是一个字符串数组,其中每个字符串是一个要限制的名称。
此规则的错误代码示例:
/*eslint no-restricted-exports: ["error", {
"restrictedNamedExports": ["foo", "bar", "Baz", "a", "b", "c", "d", "e", "👍"]
}]*/
export const foo = 1;
export function bar() {}
export class Baz {}
const a = {};
export { a };
function someFunction() {}
export { someFunction as b };
export { c } from "some_module";
export { "d" } from "some_module";
export { something as e } from "some_module";
export { "👍" } from "some_module";
此规则的正确代码示例:
/*eslint no-restricted-exports: ["error", {
"restrictedNamedExports": ["foo", "bar", "Baz", "a", "b", "c", "d", "e", "👍"]
}]*/
export const quux = 1;
export function myFunction() {}
export class MyClass {}
const a = {};
export { a as myObject };
function someFunction() {}
export { someFunction };
export { c as someName } from "some_module";
export { "d" as " d " } from "some_module";
export { something } from "some_module";
export { "👍" as thumbsUp } from "some_module";
# 默认导出
按照设计,此规则不允许 export default
声明。如果将 "default"
配置为受限名称,则该限制将仅适用于命名的导出声明。
此规则的其他错误代码示例:
/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default"] }]*/
function foo() {}
export { foo as default };
/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default"] }]*/
export { default } from "some_module";
此规则的附加正确代码示例:
/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default", "foo"] }]*/
export default function foo() {}
# 已知限制
此规则不检查再导出声明中源模块的内容。特别是,如果您从另一个模块的导出中重新导出所有内容,则该导出可能包含受限名称。此规则无法检测此类情况。
//----- some_module.js -----
export function foo() {}
//----- my_module.js -----
/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["foo"] }]*/
export * from "some_module"; // allowed, although this declaration exports "foo" from my_module