no-restricted-exports

禁止在导出中指定名称

在项目中,出于各种原因,某些名称可能不允许用作导出名称。

¥In a project, certain names may be disallowed from being used as exported names for various reasons.

规则详情

¥Rule Details

此规则不允许将指定名称用作导出名称。

¥This rule disallows specified names from being used as exported names.

选项

¥Options

默认情况下,此规则不允许任何名称。只有你在配置中指定的名称才会被禁止。

¥By default, this rule doesn’t disallow any names. Only the names you specify in the configuration will be disallowed.

此规则有一个对象选项:

¥This rule has an object option:

  • "restrictedNamedExports" 是一个字符串数组,其中每个字符串是一个要限制的名称。

    ¥"restrictedNamedExports" is an array of strings, where each string is a name to be restricted.

  • "restrictedNamedExportsPattern" 是表示正则表达式模式的字符串。与此模式匹配的命名导出将受到限制。此选项不适用于 default 命名导出。

    ¥"restrictedNamedExportsPattern" is a string representing a regular expression pattern. Named exports matching this pattern will be restricted. This option does not apply to default named exports.

  • "restrictDefaultExports" 是一个具有布尔属性的对象选项,用于限制某些默认导出声明。仅当 restrictedNamedExports 选项不包含 "default" 值时该选项才有效。允许以下属性:

    ¥"restrictDefaultExports" is an object option with boolean properties to restrict certain default export declarations. The option works only if the restrictedNamedExports option does not contain the "default" value. The following properties are allowed:

    • direct:限制 export default 声明。

      ¥direct: restricts export default declarations.

    • named:限制 export { foo as default }; 声明。

      ¥named: restricts export { foo as default }; declarations.

    • defaultFrom:限制 export { default } from 'foo'; 声明。

      ¥defaultFrom: restricts export { default } from 'foo'; declarations.

    • namedFrom:限制 export { foo as default } from 'foo'; 声明。

      ¥namedFrom: restricts export { foo as default } from 'foo'; declarations.

    • namespaceFrom:限制 export * as default from 'foo'; 声明。

      ¥namespaceFrom: restricts export * as default from 'foo'; declarations.

restrictedNamedExports

"restrictedNamedExports" 选项的错误代码示例:

¥Examples of incorrect code for the "restrictedNamedExports" option:

在线运行
/*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";

"restrictedNamedExports" 选项的正确代码示例:

¥Examples of correct code for the "restrictedNamedExports" option:

在线运行
/*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";

默认导出

¥Default exports

按照设计,"restrictedNamedExports" 选项不会禁止 export default 声明。如果将 "default" 配置为受限名称,则该限制将仅适用于命名的导出声明。

¥By design, the "restrictedNamedExports" option doesn’t disallow export default declarations. If you configure "default" as a restricted name, that restriction will apply only to named export declarations.

"restrictedNamedExports": ["default"] 选项的其他错误代码示例:

¥Examples of additional incorrect code for the "restrictedNamedExports": ["default"] option:

在线运行
/*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";

"restrictedNamedExports": ["default"] 选项的附加正确代码示例:

¥Examples of additional correct code for the "restrictedNamedExports": ["default"] option:

在线运行
/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default", "foo"] }]*/

export default function foo() {}

restrictedNamedExportsPattern

"restrictedNamedExportsPattern" 选项的错误代码示例:

¥Example of incorrect code for the "restrictedNamedExportsPattern" option:

在线运行
/*eslint no-restricted-exports: ["error", {
    "restrictedNamedExportsPattern": "bar$"
}]*/

export const foobar = 1;

"restrictedNamedExportsPattern" 选项的正确代码示例:

¥Example of correct code for the "restrictedNamedExportsPattern" option:

在线运行
/*eslint no-restricted-exports: ["error", {
    "restrictedNamedExportsPattern": "bar$"
}]*/

export const abc = 1;

请注意,此选项不适用于 export default 或任何 default 命名导出。如果你还想限制 default 导出,请使用 restrictDefaultExports 选项。

¥Note that this option does not apply to export default or any default named exports. If you want to also restrict default exports, use the restrictDefaultExports option.

restrictDefaultExports

此选项允许你限制某些 default 声明。仅当 restrictedNamedExports 选项不包含 "default" 值时该选项才有效。此选项接受以下属性:

¥This option allows you to restrict certain default declarations. The option works only if the restrictedNamedExports option does not contain the "default" value. This option accepts the following properties:

direct

"restrictDefaultExports": { "direct": true } 选项的错误代码示例:

¥Examples of incorrect code for the "restrictDefaultExports": { "direct": true } option:

在线运行
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "direct": true } }]*/

export default foo;
在线运行
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "direct": true } }]*/

export default 42;
在线运行
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "direct": true } }]*/

export default function foo() {}

named

"restrictDefaultExports": { "named": true } 选项的错误代码示例:

¥Examples of incorrect code for the "restrictDefaultExports": { "named": true } option:

在线运行
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "named": true } }]*/

const foo = 123;

export { foo as default };

defaultFrom

"restrictDefaultExports": { "defaultFrom": true } 选项的错误代码示例:

¥Examples of incorrect code for the "restrictDefaultExports": { "defaultFrom": true } option:

在线运行
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "defaultFrom": true } }]*/

export { default } from 'foo';
在线运行
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "defaultFrom": true } }]*/

export { default as default } from 'foo';

namedFrom

"restrictDefaultExports": { "namedFrom": true } 选项的错误代码示例:

¥Examples of incorrect code for the "restrictDefaultExports": { "namedFrom": true } option:

在线运行
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "namedFrom": true } }]*/

export { foo as default } from 'foo';

namespaceFrom

"restrictDefaultExports": { "namespaceFrom": true } 选项的错误代码示例:

¥Examples of incorrect code for the "restrictDefaultExports": { "namespaceFrom": true } option:

在线运行
/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "namespaceFrom": true } }]*/

export * as default from 'foo';

已知限制

¥Known Limitations

此规则不检查再导出声明中源模块的内容。特别是,如果你从另一个模块的导出中重新导出所有内容,则该导出可能包含受限名称。此规则无法检测此类情况。

¥This rule doesn’t inspect the content of source modules in re-export declarations. In particular, if you are re-exporting everything from another module’s export, that export may include a restricted name. This rule cannot detect such cases.


//----- 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 中引入。

资源