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"
值时,此选项才有效。此选项接受以下属性
direct
"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 } }]*/
named
"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 中引入。