eslint-rule-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFiori ESLint Rule Developer
Fiori ESLint 规则开发者
Add a new ESLint rule to following the established patterns in the monorepo.
@sap-ux/eslint-plugin-fiori-tools为添加新的ESLint规则,请遵循单体仓库中的既定模式。
@sap-ux/eslint-plugin-fiori-tools⚡ Efficiency rules — read before doing anything
⚡ 效率规则 — 开始操作前必读
- Use the tool directly — never
Reador spawn Explore subagents for file readingBash cat - Read everything in one parallel batch — each fast path lists the exact files; read them all at once in a single turn
- Run only the new test file during development — not the full package suite:
bash
NODE_OPTIONS="--experimental-vm-modules" npx jest --testPathPatterns="sap-[rule-name]" --no-coverage
- 直接使用工具 — 切勿使用
Read或启动Explore子代理来读取文件Bash cat - 批量并行读取所有文件 — 每个快速路径都会列出确切的文件;在单次操作中一次性读取所有文件
- 开发期间仅运行新的测试文件 — 不要运行完整的包测试套件:
bash
NODE_OPTIONS="--experimental-vm-modules" npx jest --testPathPatterns="sap-[rule-name]" --no-coverage
Step 1 — Identify the rule type, then read the reference file
步骤1 — 确定规则类型,然后读取参考文件
| Type | Use when | Reference file |
|---|---|---|
| Annotation rule | Validates | |
| Manifest JSON rule | Validates | |
| Flex change file rule | Validates | |
| JavaScript / TypeScript rule | Validates JS/TS application source code (UI5 patterns, global variables, deprecated APIs) | |
Infer from the request:
- Rule name — pattern
sap-[kebab-case-name] - OData version — V2 only, V4 only, or both
- Page scope — which page types to check. Default: all page types (list report, object page, etc.) unless the spec explicitly restricts the scope. Do not limit to one page type based on the examples in the spec.
- Auto-fix — yes/no
- Severity — or
error. Rules inwarnMUST berecommended-for-s4hana.warn
OData version determines the linker file — always follow the stated version:
| OData version | Linker file | Manifest root |
|---|---|---|
| V2 only | | |
| V4 only | | |
| Both | both linker files | both roots |
Read the matching reference file immediately, then read all the files it lists in a single parallel batch. If the rule spans multiple types (e.g. annotations + manifest, or manifest + flex changes), read all matching reference files and combine their templates and access patterns.
| 类型 | 适用场景 | 参考文件 |
|---|---|---|
| 注解规则 | 验证 | |
| Manifest JSON规则 | 验证 | |
| Flex变更文件规则 | 验证 | |
| JavaScript / TypeScript规则 | 验证JS/TS应用源代码(UI5模式、全局变量、已弃用API) | |
从请求中推断:
- 规则名称 — 遵循格式
sap-[kebab-case-name] - OData版本 — 仅V2、仅V4或两者都支持
- 页面范围 — 需要检查哪些页面类型。默认:所有页面类型(列表报表、对象页面等),除非规范明确限制范围。不要根据规范中的示例将范围限制为单一页面类型。
- 自动修复 — 是/否
- 严重程度 — 或
error。warn中的规则必须设为recommended-for-s4hana。warn
OData版本决定链接器文件 — 请严格遵循指定版本:
| OData版本 | 链接器文件 | Manifest根节点 |
|---|---|---|
| 仅V2 | | |
| 仅V4 | | |
| 两者都支持 | 两个链接器文件 | 两个根节点 |
立即读取匹配的参考文件,然后一次性批量读取其中列出的所有文件。如果规则涉及多种类型(例如注解+Manifest,或Manifest+Flex变更),请读取所有匹配的参考文件并结合它们的模板和访问模式。
Steps 2–10: Implementation Checklist
步骤2–10:实施检查清单
Step 2 — Add diagnostic constant (annotation, manifest JSON, and flex change rules only; skip for JS/TS rules)
步骤2 — 添加诊断常量(仅适用于注解、Manifest JSON和Flex变更规则;JS/TS规则可跳过)
In :
packages/eslint-plugin-fiori-tools/src/language/diagnostics.tstypescript
export const MY_RULE = 'sap-my-new-rule';
export interface MyRuleDiagnostic {
type: typeof MY_RULE;
// ... fields from the reference file's "Required diagnostic fields" table
}
// Add MyRuleDiagnostic to the Diagnostic union at the bottom在中:
packages/eslint-plugin-fiori-tools/src/language/diagnostics.tstypescript
export const MY_RULE = 'sap-my-new-rule';
export interface MyRuleDiagnostic {
type: typeof MY_RULE;
// ... 参考文件中“必填诊断字段”表格中的字段
}
// 在底部将MyRuleDiagnostic添加到Diagnostic联合类型中Step 3 — Implement the rule
步骤3 — 实现规则
Use the template from the reference file for your rule type.
Code quality requirements for every new or modified function:
- JSDoc — add a JSDoc block (,
@param) to every new function. When modifying an existing function, update its JSDoc to reflect any signature or behaviour changes.@returns - Cognitive complexity ≤ 15 — enforced by . If a function exceeds 15, extract branches or loops into well-named helper functions until the complexity falls within the limit. Do not inline complex logic in a single function to avoid this.
sonarjs/cognitive-complexity
使用对应规则类型的参考文件中的模板。
所有新增或修改函数的代码质量要求:
- JSDoc — 为每个新增函数添加JSDoc块(包含、
@param)。修改现有函数时,更新其JSDoc以反映签名或行为的任何变化。@returns - 认知复杂度 ≤ 15 — 由强制实施。如果函数复杂度超过15,将分支或循环提取到命名清晰的辅助函数中,直到复杂度符合要求。不要为了规避此限制而将复杂逻辑内联到单个函数中。
sonarjs/cognitive-complexity
Step 4 — Register the rule
步骤4 — 注册规则
src/rules/index.tstypescript
import sapMyNewRule from './sap-my-new-rule.js';
// ...
[MY_RULE]: sapMyNewRule,src/index.tsfioriLanguageConfigbaseFioriToolsRulestypescript
'@sap-ux/fiori-tools/sap-my-new-rule': 'warn',If the rule should also apply to S/4HANA projects, add it to the config in as well. Rules in this config must use severity:
recommended-for-s4hanasrc/index.ts'warn'typescript
// In the recommended-for-s4hana config rules object:
'@sap-ux/fiori-tools/sap-my-new-rule': 'warn',src/rules/index.tstypescript
import sapMyNewRule from './sap-my-new-rule.js';
// ...
[MY_RULE]: sapMyNewRule,src/index.tsfioriLanguageConfigbaseFioriToolsRulestypescript
'@sap-ux/fiori-tools/sap-my-new-rule': 'warn',如果该规则也适用于S/4HANA项目,请同时将其添加到中的配置中。此配置中的规则必须使用严重程度:
src/index.tsrecommended-for-s4hana'warn'typescript
// 在recommended-for-s4hana配置的rules对象中:
'@sap-ux/fiori-tools/sap-my-new-rule': 'warn',Step 5 — Write tests
步骤5 — 编写测试
Use the test template from the reference file; run only the new test file (see efficiency rules at the top).
Always check , never , in arrays. When a rule message contains interpolated data (e.g. ), checking only would accept any value for that placeholder and miss regressions. Use the fully resolved string instead:
messagemessageIderrors{{tableType}}messageIdtypescript
// ✅ Correct — verifies the interpolated value
errors: [
{
message:
'"TreeTable" is not supported in the flexible column layout with a draft-enabled service.'
}
]
// ❌ Wrong — does not verify the data interpolated into the message
errors: [{ messageId: 'sap-my-new-rule' }]If tests show 0 errors when violations are expected, check the debug checklist at the bottom of the reference file.
使用参考文件中的测试模板;仅运行新的测试文件(参见顶部的效率规则)。
始终检查数组中的,而非。 当规则消息包含插值数据(例如)时,仅检查会接受占位符的任何值,从而遗漏回归问题。请改用完全解析后的字符串:
errorsmessagemessageId{{tableType}}messageIdtypescript
// ✅ 正确 — 验证插值后的值
errors: [
{
message:
'"TreeTable" is not supported in the flexible column layout with a draft-enabled service.'
}
]
// ❌ 错误 — 未验证消息中插入的数据
errors: [{ messageId: 'sap-my-new-rule' }]如果测试显示预期存在违规但错误数为0,请查看参考文件底部的调试检查清单。
Step 6 — Write documentation
步骤6 — 编写文档
Create .
Read for structure. Key sections:
packages/eslint-plugin-fiori-tools/docs/rules/sap-[rule-name].mdpackages/eslint-plugin-fiori-tools/docs/rules/TEMPLATE.md- H1 title — one sentence describing the rule, with the rule ID in parentheses
- Intro paragraph — 2–3 sentences: what it detects, why it was introduced (motivation belongs here, not in a separate H2), and what to do instead
- ## Rule Details — how the rule works; warning message; "The following patterns are considered warnings:" + "The following patterns are not considered warnings" code examples
- ### How to Fix — steps to remediate (omit if obvious from the examples)
- ## False Positives — optional; include only if the rule can produce false positives
- ## Bug Report — link to GitHub issues
- ## Further Reading — optional; only include if you have a real, verifiable URL
创建。
阅读了解文档结构。关键章节:
packages/eslint-plugin-fiori-tools/docs/rules/sap-[rule-name].mdpackages/eslint-plugin-fiori-tools/docs/rules/TEMPLATE.md- H1标题 — 一句话描述规则,括号中包含规则ID
- 介绍段落 — 2-3句话:规则检测内容、引入原因(动机应放在此处,而非单独的H2章节)以及替代方案
- ## 规则详情 — 规则工作方式;警告消息;“以下模式会被视为警告:” + “以下模式不会被视为警告”代码示例
- ### 修复方法 — 修复步骤(如果从示例中可明显看出则可省略)
- ## 误报情况 — 可选;仅当规则可能产生误报时包含
- ## Bug报告 — GitHub问题链接
- ## 延伸阅读 — 可选;仅包含真实可验证的URL
Step 7 — Update README
步骤7 — 更新README
In , do two things:
packages/eslint-plugin-fiori-tools/README.md-
Add your new rule at the top of the rules table within the version column:
newmarkdown| new | [sap-my-new-rule](docs/rules/sap-my-new-rule.md) | Short description | | ✅ | -
Backfill any pendingversions: if any rows still show
newfrom prior rule additions that have since been released, look up each rule's release version innewand replacepackages/eslint-plugin-fiori-tools/CHANGELOG.mdwith that version. After this cleanup, your newly added rule should be the only row showingnew.new
在中,需完成两项操作:
packages/eslint-plugin-fiori-tools/README.md-
添加新规则 — 在规则表格的顶部添加新规则,版本列标注:
newmarkdown| new | [sap-my-new-rule](docs/rules/sap-my-new-rule.md) | 简短描述 | | ✅ | -
补全所有待处理的版本:如果之前添加的规则仍有行显示
new且已发布,请在new中查找每个规则的发布版本,将packages/eslint-plugin-fiori-tools/CHANGELOG.md替换为对应版本。完成清理后,新添加的规则应是唯一显示new的行。new
Step 8 — Run full quality gates (once)
步骤8 — 运行完整质量检查(一次)
Run to auto-fix ESLint errors and apply Prettier formatting across all modified files, then verify no issues remain:
lint:fixbash
undefined运行自动修复ESLint错误并对所有修改文件应用Prettier格式化,然后验证是否无问题残留:
lint:fixbash
undefinedFix lint errors and apply Prettier formatting
修复lint错误并应用Prettier格式化
pnpm --filter @sap-ux/eslint-plugin-fiori-tools lint:fix
pnpm --filter @sap-ux/eslint-plugin-fiori-tools lint:fix
Verify no remaining issues
验证是否无残留问题
pnpm --filter @sap-ux/eslint-plugin-fiori-tools lint
If `lint` reports errors after `lint:fix`:
- **`sonarjs/cognitive-complexity`** — resolve per the cognitive complexity guidance in Step 3.
- **`prettier/prettier`** — a formatting issue could not be auto-fixed. Apply the suggested change manually (usually a line that is too long or a multiline expression that needs restructuring).
- **`@typescript-eslint/no-unsafe-*`** — replace `any` casts or untyped values with proper interfaces or `unknown` + type guards.
Do not proceed to Step 9 until `pnpm lint` exits with code 0.
Then confirm all tests still pass:
```bash
pnpm --filter @sap-ux/eslint-plugin-fiori-tools testpnpm --filter @sap-ux/eslint-plugin-fiori-tools lint
如果`lint`在`lint:fix`后仍报告错误:
- **`sonarjs/cognitive-complexity`** — 按照步骤3中的认知复杂度指南解决。
- **`prettier/prettier`** — 存在无法自动修复的格式问题。手动应用建议的更改(通常是过长的行或需要重构的多行表达式)。
- **`@typescript-eslint/no-unsafe-*`** — 将`any`类型转换或未类型化的值替换为合适的接口或`unknown` + 类型守卫。
在`pnpm lint`返回代码0之前,不要进入步骤9。
然后确认所有测试仍通过:
```bash
pnpm --filter @sap-ux/eslint-plugin-fiori-tools testStep 9 — Create changeset
步骤9 — 创建变更集
bash
pnpm csetSelect , choose for new rules:
@sap-ux/eslint-plugin-fiori-toolsminorFEAT: add sap-my-new-rule rule for [short description]bash
pnpm cset选择,为新规则选择版本:
@sap-ux/eslint-plugin-fiori-toolsminorFEAT: add sap-my-new-rule rule for [简短描述]Step 10 — Report
步骤10 — 报告
Summarize what was done:
- Rule name and type — rule ID and which type (annotation / manifest JSON / flex change / JS/TS)
- Files created — rule implementation, test file, doc file
- Files modified — diagnostics.ts, rules/index.ts, src/index.ts, README.md
- Test results — number of valid and invalid cases, all passing
- Changeset — package, bump type (), summary line
minor
总结已完成的工作:
- 规则名称和类型 — 规则ID及类型(注解 / Manifest JSON / Flex变更 / JS/TS)
- 创建的文件 — 规则实现文件、测试文件、文档文件
- 修改的文件 — diagnostics.ts、rules/index.ts、src/index.ts、README.md
- 测试结果 — 有效和无效用例数量,全部通过
- 变更集 — 包、版本更新类型()、摘要行
minor
Quick Reference: Key Files
快速参考:关键文件
| Purpose | Path |
|---|---|
| Rule implementation | |
| Rule registry | |
| Plugin config & exports | |
| Diagnostic constants | |
| Annotation helper utilities | |
| Annotation index key format | |
| Linker types | |
| V2 linker | |
| V4 linker | |
| Rule factory | |
| Rule fixer | |
| Test helper | |
| Rule docs template | |
| README rules table | |
| 用途 | 路径 |
|---|---|
| 规则实现 | |
| 规则注册表 | |
| 插件配置与导出 | |
| 诊断常量 | |
| 注解辅助工具 | |
| 注解索引键格式 | |
| 链接器类型 | |
| V2链接器 | |
| V4链接器 | |
| 规则工厂 | |
| 规则修复器 | |
| 测试辅助工具 | |
| 规则文档模板 | |
| README规则表格 | |
check()
context access by rule type
check()按规则类型划分的check()
上下文访问方式
check()| Rule type | Use in | Why |
|---|---|---|
| Annotation | | Only check annotations referenced from pages — never scan all entity annotations. |
| Manifest JSON | | Requires linked pages to find manifest config paths |
| Flex change | | Guard on |
| JavaScript / TypeScript | Standard ESLint | JS/TS rules don't use the Fiori project model; use |
| 规则类型 | 在 | 原因 |
|---|---|---|
| 注解 | | 仅检查页面引用的注解 — 切勿扫描所有实体注解。 |
| Manifest JSON | | 需要链接页面来查找manifest配置路径 |
| Flex变更 | | 首先检查 |
| JavaScript / TypeScript | 标准ESLint | JS/TS规则不使用Fiori项目模型;使用 |