@ 边界/ 政策
评估依赖性政策
@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 参考
| 函数 | 参数 | 回返 |
|---|---|---|
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内建的核心.
类型参考
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。 。 。 .
捐款
欢迎贡献者. 请读一下 提供指南 财务报告和审定财务报表 行为守则。 。 。 .
许可证
麻省理工学院,看 自由主义 详细情况.