Index

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" 是一个字符串数组,其中每个字符串都是需要限制的名称。
  • "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" 选项的错误代码示例:

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

资源