useImportRestrictions
诊断类别:lint/nursery/useImportRestrictions
¥Diagnostic Category: lint/nursery/useImportRestrictions
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
-
灵感来自:
import-access/eslint-plugin-import-access
¥Inspired from:
import-access/eslint-plugin-import-access
不允许包私有导入。
¥Disallows package private imports.
此规则强制执行以下限制:
¥This rules enforces the following restrictions:
包私有可见性
Section titled 包私有可见性¥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
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).
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).
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).
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
¥Related links