Skip to content

useImportRestrictions

诊断类别:lint/nursery/useImportRestrictions

¥Diagnostic Category: lint/nursery/useImportRestrictions

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

不允许包私有导入。

¥Disallows package private imports.

此规则强制执行以下限制:

¥This rules enforces the following restrictions:

¥Package private visibility

所有导出的符号(例如类型、函数或其他可能导出的内容)都被视为 “包私有”。这意味着驻留在同一个目录中的模块以及这些 “sibling” 模块的子模块可以导入它们,而文件系统中较远的任何其他模块都被禁止导入它们。可以通过从索引文件重新导出来扩展符号的可见性。

¥All exported symbols, such as types, functions or other things that may be exported, are considered to be “package private”. This means that modules that reside in the same directory, as well as submodules of those “sibling” modules, are allowed to import them, while any other modules that are further away in the file system are restricted from importing them. A symbol’s visibility may be extended by re-exporting from an index file.

注释:

¥Notes:

  • 此规则仅适用于相对导入。外部依赖除外。

    ¥This rule only applies to relative imports. External dependencies are exempted.

  • 此规则仅适用于 JavaScript 和 TypeScript 文件的导入。图片或 CSS 文件等资源的导入除外。

    ¥This rule only applies to imports for JavaScript and TypeScript files. Imports for resources such as images or CSS files are exempted.

来源:https://github.com/uhyo/eslint-plugin-import-access

¥Source: https://github.com/uhyo/eslint-plugin-import-access

¥Examples

¥Invalid

// Attempt to import from `foo.js` from outside its `sub` module.
import { fooPackageVariable } from "./sub/foo.js";
code-block.js:2:36 lint/nursery/useImportRestrictions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Importing package private symbols is prohibited from outside the module directory.

1 │ // Attempt to import from foo.js from outside its sub module.
> 2 │ import { fooPackageVariable } from “./sub/foo.js”;
^^^^^^^^^^^^^^
3 │

Please import from ./sub instead (you may need to re-export the symbol(s) from ./sub/foo.js).

// Attempt to import from `bar.ts` from outside its `aunt` module.
import { barPackageVariable } from "../aunt/bar.ts";
code-block.js:2:36 lint/nursery/useImportRestrictions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Importing package private symbols is prohibited from outside the module directory.

1 │ // Attempt to import from bar.ts from outside its aunt module.
> 2 │ import { barPackageVariable } from “../aunt/bar.ts”;
^^^^^^^^^^^^^^^^
3 │

Please import from ../aunt instead (you may need to re-export the symbol(s) from ../aunt/bar.ts).

// Assumed to resolve to a JS/TS file.
import { fooPackageVariable } from "./sub/foo";
code-block.js:2:36 lint/nursery/useImportRestrictions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Importing package private symbols is prohibited from outside the module directory.

1 │ // Assumed to resolve to a JS/TS file.
> 2 │ import { fooPackageVariable } from “./sub/foo”;
^^^^^^^^^^^
3 │

Please import from ./sub instead (you may need to re-export the symbol(s) from ./sub/foo).

// If the `sub/foo` module is inaccessible, so is its index file.
import { fooPackageVariable } from "./sub/foo/index.js";
code-block.js:2:36 lint/nursery/useImportRestrictions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Importing package private symbols is prohibited from outside the module directory.

1 │ // If the sub/foo module is inaccessible, so is its index file.
> 2 │ import { fooPackageVariable } from “./sub/foo/index.js”;
^^^^^^^^^^^^^^^^^^^^
3 │

Please import from ./sub/index.js instead (you may need to re-export the symbol(s) from ./sub/foo/index.js).

¥Valid

// Imports within the same module are always allowed.
import { fooPackageVariable } from "./foo.js";
// Resources (anything other than JS/TS files) are exempt.
import { barResource } from "../aunt/bar.png";
// A parent index file is accessible like other modules.
import { internal } from "../../index.js";
// If the `sub` module is accessible, so is its index file.
import { subPackageVariable } from "./sub/index.js";
// Library imports are exempt.
import useAsync from "react-use/lib/useAsync";

¥Related links