288 lines
9.6 KiB
TypeScript
288 lines
9.6 KiB
TypeScript
/**
|
|
* 单一真值源: 反模式字典.
|
|
* 由 TS 自动生成 dacli/docs/anti-patterns.md (单向).
|
|
* installHelpHook 通过 enrichHelp() 注入到 --help.
|
|
*/
|
|
|
|
export interface AntiPattern {
|
|
/** 唯一 ID, 格式 AP-<command>-<n> */
|
|
id: string
|
|
/** 严重程度 */
|
|
severity: 'P0' | 'P1' | 'P2'
|
|
/** 反例 (1 行) */
|
|
wrong: string
|
|
/** 正例 (1-2 行) */
|
|
right: string
|
|
/** 根因 (1-2 句) */
|
|
rationale: string
|
|
/** 来源 (drift round / CLAUDE.md / decision number) */
|
|
source: string
|
|
/** 跨引用 (decisions 编号等) */
|
|
crossRefs?: string[]
|
|
}
|
|
|
|
export type CommandKey =
|
|
| 'form create'
|
|
| 'form update'
|
|
| 'form info'
|
|
| 'form delete'
|
|
| 'menu create'
|
|
| 'dict typeCreate'
|
|
| 'workflow create'
|
|
| 'dm create'
|
|
| 'di create'
|
|
| 'test run'
|
|
| 'app context'
|
|
|
|
/** 中央反模式字典 (按命令分组) */
|
|
export const ANTI_PATTERNS: Record<CommandKey, AntiPattern[]> = {
|
|
'form create': [
|
|
{
|
|
id: 'AP-fc-1',
|
|
severity: 'P0',
|
|
wrong: 'f_user_id (BIGINT 必填, 未指定FK关联关系)',
|
|
right: 'f_user_id (BIGINT FK 必填 relationForm 关联 user 表 显示字段 f_user_name)',
|
|
rationale: 'AI 默认推成 inputNumber, 列表显示 ID 数字而非名称',
|
|
source: 'drift/R17',
|
|
crossRefs: ['decisions/0001-fk-relation-form-required'],
|
|
},
|
|
{
|
|
id: 'AP-fc-2',
|
|
severity: 'P0',
|
|
wrong: 'f_status (int 必填)',
|
|
right: 'f_status (int 字典 userstatus:0正常/1冻结, 必填 select 组件)',
|
|
rationale: 'int + dictType 不会自动转 select 组件 (BS-新117 限制 1)',
|
|
source: 'drift/R17',
|
|
crossRefs: ['decisions/0002-form-create-ai-4-limitations'],
|
|
},
|
|
{
|
|
id: 'AP-fc-3',
|
|
severity: 'P1',
|
|
wrong: 'f_create_time (datetime 必填)',
|
|
right: 'f_create_time (datetime 必填 datePicker 列 format=yyyy-MM-dd HH:mm:ss)',
|
|
rationale: '列缺 format 时显示 ISO 时间戳 (BS-新117 限制 2)',
|
|
source: 'drift/R17',
|
|
crossRefs: ['decisions/0002-form-create-ai-4-limitations'],
|
|
},
|
|
{
|
|
id: 'AP-fc-4',
|
|
severity: 'P0',
|
|
wrong: '1 句话建多表 (含 FK 字段)',
|
|
right: '拆步: 先建被关联表 (不含 FK), 再 form update --fields 补 FK',
|
|
rationale: '主表先建时 FK 字段无 modelId, step machine 校验失败',
|
|
source: 'drift/R17',
|
|
crossRefs: ['decisions/0007-dacli-form-ai-not-in-dependency'],
|
|
},
|
|
{
|
|
id: 'AP-fc-5',
|
|
severity: 'P1',
|
|
wrong: '--tables 简化版 (缺 use*Permission 等关键字段)',
|
|
right: '传完整 JSON (含 usePermission/createPermission 等 16+ 字段)',
|
|
rationale: 'dacli 自动推导的 --tables 简化版会报 "Database exception"',
|
|
source: 'blindspots/BS-48',
|
|
},
|
|
{
|
|
id: 'AP-fc-6',
|
|
severity: 'P0',
|
|
wrong: 'AI 描述未提 FK 处理',
|
|
right: 'AI 描述结尾加 "本表不建FK字段"',
|
|
rationale: '物理表缺 varchar FK 列, form POST 报 "Field cannot be empty"',
|
|
source: 'drift/R17',
|
|
crossRefs: ['decisions/0003-form-update-fields-no-alter-table'],
|
|
},
|
|
{
|
|
id: 'AP-fc-7',
|
|
severity: 'P1',
|
|
wrong: '--enCode 含 - / _ / 空格 / 括号',
|
|
right: 'camelCase 无下划线, 例: tdsmyform / tdscompetition',
|
|
rationale: 'BS-56 正则 `[一-龥a-z0-9]+` 严格, 不允许这些字符',
|
|
source: 'drift/R12 BS-新38',
|
|
},
|
|
{
|
|
id: 'AP-fc-8',
|
|
severity: 'P0',
|
|
wrong: '建表后未跑 dacli form info 验证',
|
|
right: '必跑 `dacli form info <enCode>` 验证 4 项 (字段数/jnpfKey/relationForm/required)',
|
|
rationale: 'BS-新117 4 限制入生产会报 "Field cannot be empty"',
|
|
source: 'drift/R17',
|
|
crossRefs: ['decisions/0002-form-create-ai-4-limitations'],
|
|
},
|
|
],
|
|
'form update': [
|
|
{
|
|
id: 'AP-fu-1', severity: 'P0',
|
|
wrong: 'form update --fields 加 FK 字段 (varchar)',
|
|
right: 'form import --autoTable 重建物理表 / dm drop + form delete + form create 重建',
|
|
rationale: 'form update --fields 不会 ALTER 物理表 (BS-新117 限制 4)',
|
|
source: 'drift/R12',
|
|
crossRefs: ['decisions/0003-form-update-fields-no-alter-table'],
|
|
},
|
|
{
|
|
id: 'AP-fu-2', severity: 'P1',
|
|
wrong: '--fields 字段名不带 F_ 前缀',
|
|
right: '所有字段名必带 F_ 前缀',
|
|
rationale: '列名缺 F_ 前缀, 列表显示异常',
|
|
source: 'drift/R12 column_drift',
|
|
},
|
|
{
|
|
id: 'AP-fu-3', severity: 'P2',
|
|
wrong: '--fields 缺 category 字段',
|
|
right: '必传 category (用于菜单挂载)',
|
|
rationale: '菜单找不到对应 form',
|
|
source: 'blindspots/BS-新44',
|
|
},
|
|
],
|
|
'form info': [
|
|
{
|
|
id: 'AP-fi-1', severity: 'P1',
|
|
wrong: '建表后未跑 form info 验证',
|
|
right: '必跑 `dacli form info <enCode>` 检查字段数/jnpfKey/relationForm/required 4 项',
|
|
rationale: 'BS-新117 4 限制只能通过 form info 验证, 提前发现问题',
|
|
source: 'drift/R17',
|
|
},
|
|
],
|
|
'form delete': [
|
|
{
|
|
id: 'AP-fd-1', severity: 'P1',
|
|
wrong: 'form delete 不带 --cascade',
|
|
right: '删 form 前必带 --cascade 删关联菜单/物理表',
|
|
rationale: '否则留下孤立菜单和物理表',
|
|
source: 'lessons/jxcj1',
|
|
},
|
|
],
|
|
'menu create': [
|
|
{
|
|
id: 'AP-mc-1', severity: 'P0',
|
|
wrong: 'menu create --type=3 不传 moduleId',
|
|
right: '必传 --moduleId=<formId>',
|
|
rationale: '后端 400 (BS-drift-M-1)',
|
|
source: 'drift/R5',
|
|
crossRefs: ['decisions/0004-menu-create-type3-moduleid'],
|
|
},
|
|
{
|
|
id: 'AP-mc-2', severity: 'P0',
|
|
wrong: 'menu create --formId + type=3',
|
|
right: '用 type=3 --urlAddress=model/<enCode> --moduleId=<formId>',
|
|
rationale: '走 Actions/Module 时 form schema 缺主表信息报 "主表不存在"',
|
|
source: 'blindspots/BS-50',
|
|
},
|
|
{
|
|
id: 'AP-mc-3', severity: 'P1',
|
|
wrong: '跨 app 引用未传 --urlAddress',
|
|
right: '传 --urlAddress=model/<formEnCode>',
|
|
rationale: 'SPA 找不到 form 路由 404',
|
|
source: 'drift/R5',
|
|
},
|
|
],
|
|
'dict typeCreate': [
|
|
{
|
|
id: 'AP-dt-1', severity: 'P0',
|
|
wrong: 'dict typeCreate 不查就建',
|
|
right: '先 `dacli dict dataList` 看已有字典, 复用或加 <app>_ 前缀',
|
|
rationale: 'enCode 和 fullName 都查重 (全平台唯一, 跨 app)',
|
|
source: 'blindspots/BS-47',
|
|
crossRefs: ['decisions/0005-dict-typecreate-uniqueness'],
|
|
},
|
|
],
|
|
'workflow create': [
|
|
{
|
|
id: 'AP-wc-1', severity: 'P0',
|
|
wrong: 'workflow create --ai 不传 --startFormId',
|
|
right: '必传 --startFormId=<formId>',
|
|
rationale: '浏览器报 workFlowForm/undefined.vue',
|
|
source: 'blindspots/BS-E3',
|
|
crossRefs: ['decisions/0006-workflow-create-startformid'],
|
|
},
|
|
{
|
|
id: 'AP-wc-2', severity: 'P1',
|
|
wrong: 'workflow create --ai 不传 --flowXml',
|
|
right: '传 --flowXml=完整 BPMN XML',
|
|
rationale: 'AI 默认生 trivial 3-node BPMN, 不实用',
|
|
source: 'drift/R12 F3',
|
|
},
|
|
],
|
|
'dm create': [
|
|
{
|
|
id: 'AP-dm-1', severity: 'P0',
|
|
wrong: '凭印象写 dbLinkId',
|
|
right: '必跑 `dacli app context --json` 拿 dbLinkId',
|
|
rationale: '"Database connection failed" (BS-E14)',
|
|
source: 'blindspots/BS-E14',
|
|
},
|
|
],
|
|
'di create': [
|
|
{
|
|
id: 'AP-di-1', severity: 'P0',
|
|
wrong: 'di create 不传 --fieldJson',
|
|
right: '传 --fieldJson=<完整 schema JSON>',
|
|
rationale: '后端拿不到字段定义, 报 400',
|
|
source: 'drift/R9 BS-DI-1',
|
|
},
|
|
{
|
|
id: 'AP-di-2', severity: 'P0',
|
|
wrong: 'di create 不传 --parameterJson',
|
|
right: '传 --parameterJson=<参数 JSON>',
|
|
rationale: '后端拿不到参数定义',
|
|
source: 'drift/R9 BS-DI-2',
|
|
},
|
|
{
|
|
id: 'AP-di-3', severity: 'P0',
|
|
wrong: 'di create 不传 --dataConfigJson',
|
|
right: '传 --dataConfigJson=<数据配置 JSON>',
|
|
rationale: '后端拿不到数据源配置',
|
|
source: 'drift/R9 BS-DI-3',
|
|
},
|
|
],
|
|
'test run': [
|
|
{
|
|
id: 'AP-tr-1', severity: 'P0',
|
|
wrong: 'test run 推 sample 数据 f_xxx_id 为空',
|
|
right: '先 POST 关联表拿真实 ID, 再 POST 主表',
|
|
rationale: '含 FK 字段的 form POST 报 "Field cannot be empty"',
|
|
source: 'lessons/jxcj1',
|
|
crossRefs: ['decisions/0008-test-run-no-fk-injection'],
|
|
},
|
|
],
|
|
'app context': [
|
|
{
|
|
id: 'AP-ac-1', severity: 'P0',
|
|
wrong: 'dbLinkId 从印象写',
|
|
right: '必跑 `dacli app context --json` 拿 dbLinkId / existingEnCodes',
|
|
rationale: '"Database connection failed" 或 enCode 冲突',
|
|
source: 'lessons/jxcj1',
|
|
},
|
|
],
|
|
}
|
|
|
|
import type { DacliHelp } from '../core/help'
|
|
|
|
/** enrichHelp 返回类型 (DacliHelp with pitfalls/relatedRefs auto-populated) */
|
|
export interface DacliHelpEnriched extends DacliHelp {
|
|
pitfalls?: NonNullable<DacliHelp['pitfalls']>
|
|
relatedRefs?: NonNullable<DacliHelp['relatedRefs']>
|
|
}
|
|
|
|
/** 把 ANTI_PATTERNS 注入到 DacliHelp */
|
|
export function enrichHelp(fullCmd: string, local: DacliHelp): DacliHelpEnriched {
|
|
const patterns = ANTI_PATTERNS[fullCmd as CommandKey] ?? []
|
|
|
|
const localPitfalls = local.pitfalls ?? []
|
|
const patternPitfalls = patterns.map(p => ({
|
|
wrong: p.wrong,
|
|
right: p.right,
|
|
rationale: p.rationale,
|
|
severity: p.severity,
|
|
}))
|
|
const mergedPitfalls = [...localPitfalls, ...patternPitfalls]
|
|
|
|
const localRefs = local.relatedRefs ?? []
|
|
const patternRefs = patterns.flatMap(p => p.crossRefs ?? [])
|
|
const mergedRefs = [...new Set([...localRefs, ...patternRefs])]
|
|
|
|
return {
|
|
...local,
|
|
pitfalls: mergedPitfalls.length > 0 ? mergedPitfalls : undefined,
|
|
relatedRefs: mergedRefs.length > 0 ? mergedRefs : undefined,
|
|
}
|
|
}
|