# @boundaries/policies 中文文档

规范地址：https://npmcn.com/package/%40boundaries/policies/

版本：1\.0\.1
npm 元数据更新时间：2026-11-05T11:47:49.276Z
中文译文更新时间：2026-11-08T12:35:20.674Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40boundaries/policies
原文对照：https://npmcn.com/package/%40boundaries/policies/?lang=original
项目主页：https://github.com/javierbrea/eslint-plugin-boundaries/tree/master/packages/policies#readme
源码仓库：https://github.com/javierbrea/eslint-plugin-boundaries

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：650 kB，15 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=18\.18
- 运行依赖：3 个
- 首次发布：2026-11-02T08:06:01.858Z
- 最新版本发布：2026-11-05T11:47:49.083Z
- 近 30 天下载量：94628（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：3（近 12 个月 3 个）

## 中文简介

评价依赖政策和@边界工具的信息

## 安装

```sh
npm install @boundaries/policies
```

也可以使用：`pnpm add @boundaries/policies`、`yarn add @boundaries/policies`、`bun add @boundaries/policies`

## README

[![License](https://img.shields.io/npm/l/eslint-plugin-boundaries.svg)](https://github.com/javierbrea/eslint-plugin-boundaries/blob/master/LICENSE) [![Build status](https://github.com/javierbrea/eslint-plugin-boundaries/actions/workflows/build.yml/badge.svg)](https://github.com/javierbrea/eslint-plugin-boundaries/actions?query=workflow%3Abuild+branch%3Amaster) [![Coverage Status](https://coveralls.io/repos/github/javierbrea/eslint-plugin-boundaries/badge.svg)](https://coveralls.io/github/javierbrea/eslint-plugin-boundaries) [![Quality Gate](https://sonarcloud.io/api/project_badges/measure?project=javierbrea_eslint-plugin-boundaries_policies&metric=alert_status)](https://sonarcloud.io/dashboard?id=javierbrea_eslint-plugin-boundaries_policies) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fjavierbrea%2Feslint-plugin-boundaries%2Fmaster%3Fmodule%3Dpolicies)](https://dashboard.stryker-mutator.io/reports/github.com/javierbrea/eslint-plugin-boundaries/master?module=policies)

[![NPM downloads](https://img.shields.io/npm/dm/@boundaries/policies.svg)](https://www.npmjs.com/package/@boundaries/policies) [![Last commit](https://img.shields.io/github/last-commit/javierbrea/eslint-plugin-boundaries.svg)](https://github.com/javierbrea/eslint-plugin-boundaries/commits) [![Last release](https://img.shields.io/github/release-date/javierbrea/eslint-plugin-boundaries.svg)](https://github.com/javierbrea/eslint-plugin-boundaries/releases)

# @ 边界/ 政策

> 评估依赖性政策 `@boundaries/eslint-plugin` 和他们的消息,没有ESLint.

## 目录

-   [导言](https://npmcn.com/package/%40boundaries/policies/#readme-introduction)
-   [特征](https://npmcn.com/package/%40boundaries/policies/#readme-features)
-   [安装](https://npmcn.com/package/%40boundaries/policies/#readme-installation)
    -   [浏览器支持](https://npmcn.com/package/%40boundaries/policies/#readme-browser-support)
-   [快速启动](https://npmcn.com/package/%40boundaries/policies/#readme-quick-start)
-   [核心概念](https://npmcn.com/package/%40boundaries/policies/#readme-core-concepts)
    -   [跳过依赖关系](https://npmcn.com/package/%40boundaries/policies/#readme-skipped-dependencies)
    -   [对政策的评价](https://npmcn.com/package/%40boundaries/policies/#readme-evaluation-of-the-policies)
    -   [纪念](https://npmcn.com/package/%40boundaries/policies/#readme-memoization)
-   [API 参考](https://npmcn.com/package/%40boundaries/policies/#readme-api-reference)
-   [类型参考](https://npmcn.com/package/%40boundaries/policies/#readme-type-reference)
-   [捐款](https://npmcn.com/package/%40boundaries/policies/#readme-contributing)
-   [许可证](https://npmcn.com/package/%40boundaries/policies/#readme-license)

## 导言

`@boundaries/policies` 决定依赖性与 `dependencies` 规则 [`@boundaries/eslint-plugin`](https://www.npmjs.com/package/@boundaries/eslint-plugin) does:它描述来自路径的文件和依赖性,说明规则是否检查依赖性,评价政策并构建规则报告的信息.

它以设置和规则选项为 [`@boundaries/config`](https://www.npmjs.com/package/@boundaries/config) 让他们恢复正常 ESLint插件在读取AST并解析所导入的模块后使用;一个没有ESLint的工具,如操场或MCP服务器,可以使用它来解释同样的判断.

> \[注\] 此软件包不会运行ESLint,读取文件,解析模块或打印任何东西. 主机通过文件的路径,以及每个依赖的解决路径,当它有一个;在评价时发现的问题作为诊断返回.

## 特征

-   **文件及依赖性说明** 从路径,通过匹配者 [`@boundaries/elements`](https://www.npmjs.com/package/@boundaries/elements)
-   **检查 `dependencies` 规则** (a) 决定是否对依赖性进行评估,并说明不进行评估的原因
-   **政策评价** 与规则的语义: 与决定相匹配的最后政策, `disallow` 优先于 `allow` 在政策范围内,以及 `default` 选项决定何时没有政策匹配
-   **决策政策和匹配**,对于允许的依赖性也
-   **插件报告的信息**、 默认和自定义, 包含 Handlebars 和 遗留 `${...}` 模板
-   **在节点.js和浏览器中运行**,不包含 ESLint

## 安装

安装软件包 `@boundaries/config` 使设置和规则选项正常化:

```bash
npm install @boundaries/policies @boundaries/config
```

### 浏览器支持

该软件包不依赖于ESLint或Node.js,因此它也在浏览器中运行. 为浏览器所构建的 Bundlers, 如 Webpack, Vite 或 esbuild , 选择它的浏览器通过 `browser` 其状况 `exports` 字段,且该构建不需要多填充节点.js模块或像 Globals 这样的 `process`。 。 。 .

## 快速启动

将配置规范化,然后描述和评价依赖性:

```ts
import { normalizeConfig } from "@boundaries/config";
import { describeDependency, evaluateDependency } from "@boundaries/policies";

const { settings, rules } = normalizeConfig(
  {
    settings: {
      "boundaries/elements": [
        { type: "helper", pattern: "helpers/*" },
        { type: "component", pattern: "components/*/*" },
      ],
    },
    rules: {
      "boundaries/dependencies": [
        2,
        {
          default: "disallow",
          policies: [
            {
              from: { element: { type: "component" } },
              allow: { to: { element: { type: "helper" } } },
            },
          ],
        },
      ],
    },
  },
  { rootPath: "/project" }
).value;

const dependency = describeDependency(settings, {
  from: "/project/src/helpers/format/index.js",
  to: "/project/src/components/atoms/Price/index.js",
  source: "../../components/atoms/Price",
  kind: "value",
  nodeKind: "import",
});

const evaluation = evaluateDependency(
  settings,
  rules.dependencies!.options,
  dependency
);

// {
//   checked: true,
//   allowed: false,
//   policyIndex: null,
//   match: null,
//   message: 'There is no policy allowing dependencies from elements of type "helper" to elements of type "component"',
//   diagnostics: [],
// }
```

## 核心概念

### 跳过依赖关系

这个 `dependencies` 规则并不评估每一种依赖. `getDependencySkipReason` 返回此顺序下的第一个应用原因,或 `null`数字 :

| 原因 | 何时 |
| --- | --- |
| `from-ignored` | 导入文件被忽略 |
| `from-unknown` | 导入文件匹配没有元素和文件描述符 |
| `to-ignored` | 目标是一个被忽略的本地文件 |
| `not-local` | 目标为外部或核心,以及 `checkAllOrigins` 没有 `true` |
| `unknown-local` | 目标为局部,匹配没有元素和没有文件描述符,以及 `checkUnknownLocals` 没有 `true` |
| `internal` | 两个文件都属于同一元素,以及 `checkInternals` 没有 `true` |

它只读到 `check*` 选项,因此它使用用户撰写或规范化的选项. `evaluateDependency` 返回时 `{ checked: false, reason }` 当有原因时.

`DEPENDENCY_SKIP_REASONS_MAP` 将理由作为常数,从 `FROM_IGNORED: "from-ignored"` 改为 `INTERNAL: "internal"`,比较一个不写出其字符串的理由.

### 对政策的评价

`evaluatePolicies` 合并 `from`, (中文(简体) ), `to` 财务报告和审定财务报表 `dependency` 每个策略的选取者及其每个条目 `allow` 财务报告和审定财务报表 `disallow`,与对他们的依赖相匹配:

-   最后一个匹配的政策决定.
-   在政策范围内, `allow` 未评价何时 `disallow` 相匹配 .
-   当政策不匹配时 `default` 选项决定 `policyIndex` 财务报告和审定财务报表 `match` 为: `null`。 。 。 .

当不允许依赖时, `message` 是插件报告的信息: 自定义 `message` 或带有其模板或默认消息的选项。 例外是习惯 `message` 无效的语法或未知的帮助器: `evaluatePolicies` 丢出一个错误,以 Handlebars 错误命名信件及其设置策略或选项 `cause`。 。 。 .

### 纪念

配对者被常态化设置的特征所纪念,而每种政策的合并选择者被常态化政策的身份所纪念,在 `WeakMap`s. 在配置不改变时通过相同的正态对象,并在配置改变时通过新的对象;旧条目与它们一起收集。 成果与纪念 `@boundaries/elements`所以他们不能变异.

## API 参考

| 函数 | 参数 | 回返 |
| --- | --- | --- |
| `getMatcher` | `settings: SettingsNormalized` | 这个 `Matcher` 联 合 国 `@boundaries/elements` 设置 |
| `describeEntity` | `settings: SettingsNormalized`, (中文(简体) ), `filePath: string` | 这个 `EntityDescription` 文件:元素、文件类别和模块来源 |
| `describeDependency` | `settings: SettingsNormalized`, (中文(简体) ), `dependency: DependencyDescriptorOptions` | 这个 `DependencyDescription` 抚养人的权利 |
| `getDependencySkipReason` | `options: DependencySkipOptions | undefined`, (中文(简体) ), `dependency: DependencyDescription` | 第一个 `DependencySkipReason` 适用,或 `null` |
| `evaluatePolicies` | `settings: SettingsNormalized`, (中文(简体) ), `options: DependenciesRuleOptionsNormalized`, (中文(简体) ), `dependency: DependencyDescription` | A 类 `PolicyEvaluation` |
| `evaluateDependency` | `settings: SettingsNormalized`, (中文(简体) ), `options: DependenciesRuleOptionsNormalized`, (中文(简体) ), `dependency: DependencyDescription` | A 类 `DependencyEvaluation` |
| `customErrorMessage` | `template: string`, (中文(简体) ), `dependency: DependencyDescription`, (中文(简体) ), `policyIndex?: number | null`, (中文(简体) ), `match?: DependencySingleSelectorMatchResult | null` | 带有模板的自定义消息 |
| `elementDescriptionMessage` | `element: ElementDescription | ElementParent`, (中文(简体) ), `properties: string[]`, (中文(简体) ), `options?: { singleElement?: boolean; includeNullValues?: boolean }` | 对一个元素的描述, 如插件信件所写的 |

`DependencyDescriptorOptions` 输入 `@boundaries/elements`数字 : `from` 财务报告和审定财务报表 `to` 是路径( r)`to` 是目标已决的道路,当目标未定的时候, `source` 是已写入的导入指定符, `kind` 实值 `value` 或者说 `type`, (中文(简体) ), `nodeKind` 是依赖节点的名称( q)`import`, (中文(简体) ), `export`, (中文(简体) ), `require`, (中文(简体) ), `dynamic-import`,或 `name` 附加依赖性节点)和 `specifiers` 是指指定的进出口。 没有解析器的宿主通过 `to` 仅针对它知道的文件 : 默认 `boundaries/flag-as-external` 设置,未解决的非相对源是外部的,或者一个节点.js内建的核心.

## 类型参考

```ts
type DependencySkipReason =
  | "from-ignored"
  | "from-unknown"
  | "to-ignored"
  | "not-local"
  | "unknown-local"
  | "internal";

type DependencySkipOptions = {
  checkAllOrigins?: boolean;
  checkUnknownLocals?: boolean;
  checkInternals?: boolean;
};

type PolicyEvaluation = {
  /** Index in options.policies of the policy that decided, or null when options.default decided */
  policyIndex: number | null;
  /** What the deciding selector matched, or null when options.default decided */
  match: DependencySingleSelectorMatchResult | null;
  /** Problems found while matching, with paths from the root of the options */
  diagnostics: Diagnostic[];
} & (
  | { allowed: true; message?: undefined }
  /** message: the message the plugin reports */
  | { allowed: false; message: string }
);

type DependencyEvaluation =
  | ({ checked: true } & PolicyEvaluation)
  | { checked: false; reason: DependencySkipReason };
```

自定义信件模板收到的上下文类型也会导出 : `CustomMessageTemplateContext`, (中文(简体) ), `CustomMessageTemplatePolicyContext`, (中文(简体) ), `CustomMessageTemplatePolicySelectorContext` 财务报告和审定财务报表 `CustomMessageTemplatePolicyEntitySelectorContext`。 。 。 . `Diagnostic` 并导出设置和选项类型 `@boundaries/config`,并按 `@boundaries/elements`。 。 。 .

## 捐款

欢迎贡献者. 请读一下 [提供指南](https://npmcn.com/package/.github/CONTRIBUTING.md) 财务报告和审定财务报表 [行为守则](https://npmcn.com/package/.github/CODE_OF_CONDUCT.md)。 。 。 .

## 许可证

麻省理工学院,看 [自由主义](https://npmcn.com/package/%40boundaries/policies/LICENSE) 详细情况.

## 常见问题

### @boundaries/policies 是什么？

评价依赖政策和@边界工具的信息

### 如何安装 @boundaries/policies？

在项目目录运行 npm install @boundaries/policies，或使用 pnpm add / yarn add / bun add。

### @boundaries/policies 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @boundaries/policies 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @boundaries/policies 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=18\.18。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @boundaries/policies 可以商用吗？

@boundaries/policies 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @boundaries/policies 的下载量有多少？

根据 npm 官方下载统计，@boundaries/policies 在 2026/10/09 – 2026/11/07 这 30 天内被下载 94,628 次，日均约 3,154 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @boundaries/policies 还在维护吗？

最新版本 1\.0\.1 发布于 2026/11/05（3 天前）；至今共发布 3 个版本，其中过去 12 个月发布了 3 个；首次发布于 2026/11/02。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

