@boundaries/policies

中文文档
1.0.1发布于 2026/11/05MITTypeScriptESM + CJS近 30 天下载 9.5万

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

中文使用文档
文档目录14 节
  1. 目录
  2. 导言
  3. 特征
  4. 安装
  5. 浏览器支持
  6. 快速启动
  7. 核心概念
  8. 跳过依赖关系
  9. 对政策的评价
  10. 纪念
  11. API 参考
  12. 类型参考
  13. 捐款
  14. 许可证

License Build status Coverage Status Quality Gate Mutation testing badge

NPM downloads Last commit Last release

@ 边界/ 政策

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

目录

导言

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

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

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

特征

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

安装

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

npm install @boundaries/policies @boundaries/config

浏览器支持

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

快速启动

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

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。 。 。 .

纪念

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

API 参考

函数参数回返
getMatchersettings: SettingsNormalized这个 Matcher 联 合 国 @boundaries/elements 设置
describeEntitysettings: SettingsNormalized, (中文(简体) ), filePath: string这个 EntityDescription 文件:元素、文件类别和模块来源
describeDependencysettings: SettingsNormalized, (中文(简体) ), dependency: DependencyDescriptorOptions这个 DependencyDescription 抚养人的权利
getDependencySkipReasonoptions: DependencySkipOptions | undefined, (中文(简体) ), dependency: DependencyDescription第一个 DependencySkipReason 适用,或 null
evaluatePoliciessettings: SettingsNormalized, (中文(简体) ), options: DependenciesRuleOptionsNormalized, (中文(简体) ), dependency: DependencyDescriptionA 类 PolicyEvaluation
evaluateDependencysettings: SettingsNormalized, (中文(简体) ), options: DependenciesRuleOptionsNormalized, (中文(简体) ), dependency: DependencyDescriptionA 类 DependencyEvaluation
customErrorMessagetemplate: string, (中文(简体) ), dependency: DependencyDescription, (中文(简体) ), policyIndex?: number | null, (中文(简体) ), match?: DependencySingleSelectorMatchResult | null带有模板的自定义消息
elementDescriptionMessageelement: 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内建的核心.

类型参考

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。 。 。 .

捐款

欢迎贡献者. 请读一下 提供指南 财务报告和审定财务报表 行为守则。 。 。 .

许可证

麻省理工学院,看 自由主义 详细情况.

版本与发布记录

@boundaries/policies 共发布 3 个版本,首次发布于 2026/11/02;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.0.1latest2026/11/053 天前
1.0.02026/11/026 天前
0.0.0-stage2026/11/026 天前

关于 @boundaries/policies 的常见问题

@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 和提交记录判断。