no-restricted-exports
禁止在导出中使用指定名称
在项目中,某些名称可能因各种原因而被禁止用作导出名称。
规则详细信息
此规则禁止使用指定的名称作为导出名称。
选项
默认情况下,此规则不会禁止任何名称。只有您在配置中指定的名称才会被禁止。
此规则有一个对象选项
"restrictedNamedExports"
是一个字符串数组,每个字符串都是要限制的名称。"restrictedNamedExportsPattern"
是一个表示正则表达式模式的字符串。与该模式匹配的命名导出将被限制。此选项不适用于default
命名导出。"restrictDefaultExports"
是一个对象选项,具有布尔属性以限制某些默认导出声明。该选项仅在restrictedNamedExports
选项不包含"default"
值时才有效。允许以下属性direct
: 限制export default
声明。named
: 限制export { foo as default };
声明。defaultFrom
: 限制export { default } from 'foo';
声明。namedFrom
: 限制export { foo as default } from 'foo';
声明。namespaceFrom
: 限制export * as default from 'foo';
声明。
restrictedNamedExports
"restrictedNamedExports"
选项的不正确代码示例
/*eslint no-restricted-exports: ["error", {
"restrictedNamedExports": ["foo", "bar", "Baz", "a", "b", "c", "d", "e", "👍"]
}]*/
export const = 1;
export function () {}
export class {}
const a = {};
export { };
function someFunction() {}
export { someFunction as };
export { } from "some_module";
export { } from "some_module";
export { something as } from "some_module";
export { } from "some_module";
"restrictedNamedExports"
选项的正确代码示例
/*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";
默认导出
根据设计,"restrictedNamedExports"
选项不会禁止 export default
声明。如果将"default"
配置为受限制的名称,该限制将仅适用于命名导出声明。
"restrictedNamedExports": ["default"]
选项的其他不正确代码示例
/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default"] }]*/
function foo() {}
export { foo as };
/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default"] }]*/
export { } from "some_module";
"restrictedNamedExports": ["default"]
选项的其他正确代码示例
/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default", "foo"] }]*/
export default function foo() {}
restrictedNamedExportsPattern
"restrictedNamedExportsPattern"
选项的不正确代码示例
/*eslint no-restricted-exports: ["error", {
"restrictedNamedExportsPattern": "bar$"
}]*/
export const = 1;
"restrictedNamedExportsPattern"
选项的正确代码示例
/*eslint no-restricted-exports: ["error", {
"restrictedNamedExportsPattern": "bar$"
}]*/
export const abc = 1;
请注意,此选项不适用于 export default
或任何 default
命名导出。如果您还想限制 default
导出,请使用 restrictDefaultExports
选项。
restrictDefaultExports
此选项允许您限制某些 default
声明。该选项仅在restrictedNamedExports
选项不包含"default"
值时才有效。此选项接受以下属性
直接
"restrictDefaultExports": { "direct": true }
选项的不正确代码示例
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "direct": true } }]*/
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "direct": true } }]*/
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "direct": true } }]*/
命名
"restrictDefaultExports": { "named": true }
选项的不正确代码示例
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "named": true } }]*/
const foo = 123;
export { foo as };
defaultFrom
"restrictDefaultExports": { "defaultFrom": true }
选项的不正确代码示例
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "defaultFrom": true } }]*/
export { } from 'foo';
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "defaultFrom": true } }]*/
export { default as } from 'foo';
namedFrom
"restrictDefaultExports": { "namedFrom": true }
选项的不正确代码示例
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "namedFrom": true } }]*/
export { foo as } from 'foo';
namespaceFrom
"restrictDefaultExports": { "namespaceFrom": true }
选项的不正确代码示例
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "namespaceFrom": true } }]*/
export * as from '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
版本
此规则是在 ESLint v7.0.0-alpha.0 中引入的。