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