@boundaries/policies

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

Evaluation of the dependency policies and messages of @boundaries tools

原始使用文档
文档目录14 节
  1. Table of Contents
  2. Introduction
  3. Features
  4. Installation
  5. Browser Support
  6. Quick Start
  7. Core Concepts
  8. Skipped Dependencies
  9. Evaluation of the Policies
  10. Memoization
  11. API Reference
  12. Type Reference
  13. Contributing
  14. License

License Build status Coverage Status Quality Gate Mutation testing badge

NPM downloads Last commit Last release

@boundaries/policies

Evaluation of the dependency policies of @boundaries/eslint-plugin and their messages, without ESLint.

Table of Contents

Introduction

@boundaries/policies decides dependencies exactly as the dependencies rule of @boundaries/eslint-plugin does: it describes files and dependencies from their paths, tells whether the rule checks a dependency, evaluates the policies and builds the message that the rule reports.

It takes the settings and the rule options as @boundaries/config normalizes them. The ESLint plugin uses it after reading the AST and resolving the imported modules; a tool without ESLint, such as a playground or an MCP server, can use it to explain the same verdicts.

[!NOTE] This package does not run ESLint, read files, resolve modules or print anything. The host passes the paths of the files, and the resolved path of each dependency when it has one; the problems found while evaluating are returned as diagnostics.

Features

  • File and dependency descriptions from paths, through the matcher of @boundaries/elements
  • The checks of the dependencies rule that decide whether a dependency is evaluated, with the reason when it is not
  • Policy evaluation with the rule's semantics: the last policy that matches decides, disallow takes precedence over allow within a policy, and the default option decides when no policy matches
  • The deciding policy and match, for allowed dependencies too
  • The messages the plugin reports, default and custom, with Handlebars and legacy ${...} templates
  • Runs in Node.js and in browsers, without ESLint

Installation

Install the package, with @boundaries/config to normalize the settings and rule options:

npm install @boundaries/policies @boundaries/config

Browser Support

The package does not depend on ESLint or Node.js, so it also runs in browsers. Bundlers that build for browsers, such as webpack, Vite or esbuild, select its browser build through the browser condition of its exports field, and that build needs no polyfills of Node.js modules or globals like process.

Quick Start

Normalize a config, then describe and evaluate a dependency:

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: [],
// }

Core Concepts

Skipped Dependencies

The dependencies rule does not evaluate every dependency. getDependencySkipReason returns the first reason that applies, in this order, or null:

ReasonWhen
from-ignoredThe importing file is ignored
from-unknownThe importing file matches no element and no file descriptor
to-ignoredThe target is a local file that is ignored
not-localThe target is external or core, and checkAllOrigins is not true
unknown-localThe target is local and matches no element and no file descriptor, and checkUnknownLocals is not true
internalBoth files belong to the same element, and checkInternals is not true

It reads only the check* options, so it takes the options as the user wrote them or normalized. evaluateDependency returns { checked: false, reason } when there is a reason.

DEPENDENCY_SKIP_REASONS_MAP holds the reasons as constants, from FROM_IGNORED: "from-ignored" to INTERNAL: "internal", to compare a reason without writing its string.

Evaluation of the Policies

evaluatePolicies merges the from, to and dependency selectors of each policy with each entry of its allow and disallow, and matches the dependency against them:

  • The last policy that matches decides.
  • Within a policy, allow is not evaluated when disallow matched.
  • When no policy matches, the default option decides, and policyIndex and match are null.

When the dependency is not allowed, message is the message the plugin reports: the custom message of the deciding policy, or of the options, rendered with its template, or the default message. The exception is a custom message that fails to render, such as one with invalid Handlebars syntax or an unknown helper: evaluatePolicies throws an error that names the message and the policy that sets it, or the options, with the Handlebars error as its cause.

Memoization

The matcher is memoized by the identity of the normalized settings, and the merged selectors of each policy by the identity of the normalized policy, in WeakMaps. Pass the same normalized objects while the config does not change, and new ones when it does; the old entries are collected with them. Results are shared with the memoization of @boundaries/elements, so they must not be mutated.

API Reference

FunctionParametersReturns
getMatchersettings: SettingsNormalizedThe Matcher of @boundaries/elements for the settings
describeEntitysettings: SettingsNormalized, filePath: stringThe EntityDescription of the file: element, file categories and module origin
describeDependencysettings: SettingsNormalized, dependency: DependencyDescriptorOptionsThe DependencyDescription of the dependency
getDependencySkipReasonoptions: DependencySkipOptions | undefined, dependency: DependencyDescriptionThe first DependencySkipReason that applies, or null
evaluatePoliciessettings: SettingsNormalized, options: DependenciesRuleOptionsNormalized, dependency: DependencyDescriptionA PolicyEvaluation
evaluateDependencysettings: SettingsNormalized, options: DependenciesRuleOptionsNormalized, dependency: DependencyDescriptionA DependencyEvaluation
customErrorMessagetemplate: string, dependency: DependencyDescription, policyIndex?: number | null, match?: DependencySingleSelectorMatchResult | nullThe custom message rendered with its template
elementDescriptionMessageelement: ElementDescription | ElementParent, properties: string[], options?: { singleElement?: boolean; includeNullValues?: boolean }A description of an element, as the plugin's messages write it

DependencyDescriptorOptions is the input of @boundaries/elements: from and to are paths (to is the resolved path of the target, absent when nothing resolves it), source is the import specifier as written, kind is value or type, nodeKind is the name of the dependency node (import, export, require, dynamic-import, or the name of an additional dependency node) and specifiers are the named imports or exports. A host without a resolver passes to only for the files it knows: with the default boundaries/flag-as-external setting, an unresolved non-relative source is external, or core for a Node.js built-in.

Type Reference

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 };

The types of the context that custom message templates receive are exported too: CustomMessageTemplateContext, CustomMessageTemplatePolicyContext, CustomMessageTemplatePolicySelectorContext and CustomMessageTemplatePolicyEntitySelectorContext. Diagnostic and the settings and options types are exported by @boundaries/config, and the description and selector types by @boundaries/elements.

Contributing

Contributors are welcome. Please read the contributing guidelines and code of conduct.

License

MIT, see LICENSE for details.

版本与发布记录

@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 是什么?

Evaluation of the dependency policies and messages of @boundaries tools

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