From d55ec5b219c1b1a8ddf9d9999fa6b359c7b3927f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 14 Jun 2026 19:28:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(dacli):=20form=20lint=20CLI=20=E5=85=A5?= =?UTF-8?q?=E5=8F=A3=20(=E5=A4=8D=E7=94=A8=20=5Fblindspots)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/form/index.ts | 4 +- src/commands/form/lint.ts | 143 +++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 src/commands/form/lint.ts diff --git a/src/commands/form/index.ts b/src/commands/form/index.ts index 30b1ff2..d2cc730 100644 --- a/src/commands/form/index.ts +++ b/src/commands/form/index.ts @@ -13,9 +13,10 @@ import { buildFormCloneCommand } from './clone.ts' import { buildFormValidateCommand } from './validate.ts' import { buildFormFixColumnDataCommand } from './fixcolumndata.ts' import { buildFormCodegenCommand } from './codegen.ts' +import { buildFormLintCommand } from './lint.ts' export function buildFormCommand(): Command { - const cmd = new Command('form').description('表单 CRUD (list/info/create/release/delete/export/import/clone/validate/fixcolumndata/codegen)') + const cmd = new Command('form').description('表单 CRUD (list/info/create/release/delete/export/import/clone/validate/fixcolumndata/codegen/lint)') cmd.addCommand(buildFormListCommand()) cmd.addCommand(buildFormInfoCommand()) cmd.addCommand(buildFormCreateCommand()) @@ -27,5 +28,6 @@ export function buildFormCommand(): Command { cmd.addCommand(buildFormValidateCommand()) cmd.addCommand(buildFormFixColumnDataCommand()) cmd.addCommand(buildFormCodegenCommand()) + cmd.addCommand(buildFormLintCommand()) return cmd } diff --git a/src/commands/form/lint.ts b/src/commands/form/lint.ts new file mode 100644 index 0000000..2780049 --- /dev/null +++ b/src/commands/form/lint.ts @@ -0,0 +1,143 @@ +/** + * commands/form/lint.ts — 设计期 lint (无需 JNPF server) + * + * 用法: + * dacli form lint --from-script=docs/{app}/scripts/03_forms.py + * dacli form lint --from-design=docs/{app}/design-jnpf.html + * dacli form lint --from-script=... --from-design=... + * + * 复用 _blindspots.ts 5 类检查 + */ +import { Command } from 'commander' +import { readFileSync } from 'node:fs' +import { resolve } from 'node:path' +import { installAiHelpHook } from '../../core/help.ts' +import { checkBs31, checkBs32, checkBs23, type BlindspotIssue } from './_blindspots.ts' +import { parsePythonForms, parseDesignHtml, type FormDef, type CmdBlock } from './_parsers.ts' +import { printJson, log } from '../../core/log.ts' + +interface LintReport { + forms: number + fields: number + blindspots: Record + issues: BlindspotIssue[] + cmdBlocks: { total: number; issues: string[] } + exitCode: 0 | 1 +} + +function lintScript(scriptPath: string): { forms: FormDef[]; issues: BlindspotIssue[] } { + const src = readFileSync(scriptPath, 'utf-8') + const forms = parsePythonForms(src) + const issues: BlindspotIssue[] = [] + for (const f of forms) { + // 把 fields 转成 validate.ts 期望的 formData 形态 + const formData = { fields: f.fields.map(fd => ({ + __config__: { label: fd.label, jnpfKey: fd.type }, + __vModel__: fd.vmodel, + }))} + issues.push(...checkBs31(JSON.stringify(formData))) + issues.push(...checkBs32(JSON.stringify(formData))) + } + return { forms, issues } +} + +function lintDesign(designPath: string): { blocks: CmdBlock[]; issues: BlindspotIssue[]; blockIssues: string[] } { + const src = readFileSync(designPath, 'utf-8') + const blocks = parseDesignHtml(src) + const blockIssues: string[] = [] + for (const b of blocks) { + if (b.commandKind === 'dm' && !b.hasDbLinkId) blockIssues.push('dm create 缺 --dbLinkId') + if (b.commandKind === 'form' && !b.hasWebType) blockIssues.push('form create 缺 --webType') + if (b.commandKind === 'form' && !b.hasAutoRelease) blockIssues.push('form create 缺 --autoRelease (盲点 28 防御)') + } + // BS-23 需运行时 columnData, 设计稿无法直接判断, 标 N/A 由 form validate 兜底 + return { blocks, issues: [], blockIssues } +} + +export function buildFormLintCommand(): Command { + const cmd = new Command('lint') + .description('设计期 lint: 复用 5 类盲点规则, 不需 JNPF server') + .option('--from-script ', '03_forms.py 路径') + .option('--from-design ', 'design-jnpf.html 路径') + .option('--json', '原始 JSON 输出', false) + .action((opts: { fromScript?: string; fromDesign?: string; json?: boolean }) => { + if (!opts.fromScript && !opts.fromDesign) { + log.error('必须传 --from-script 或 --from-design') + process.exit(1) + } + const forms: FormDef[] = [] + const allIssues: BlindspotIssue[] = [] + let blocks: CmdBlock[] = [] + let blockIssues: string[] = [] + if (opts.fromScript) { + const r = lintScript(resolve(opts.fromScript)) + forms.push(...r.forms) + allIssues.push(...r.issues) + } + if (opts.fromDesign) { + const r = lintDesign(resolve(opts.fromDesign)) + blocks = r.blocks + blockIssues = r.blockIssues + allIssues.push(...r.issues) + } + // 汇总 — BS-23 需运行时 columnData, 设计稿无 columnData 全文, 标 N/A 由 form validate 兜底 + // BS-15/V2 设计稿无完整 tables/formData JSON, 也标 N/A + const bs: Record = { + 'BS-23': { pass: true, fail: 0, n_a: 1 }, + 'BS-31': { pass: !allIssues.some(i => i.id === 'BS-31'), fail: 0, n_a: 0 }, + 'BS-32': { pass: !allIssues.some(i => i.id === 'BS-32'), fail: 0, n_a: 0 }, + 'BS-15': { pass: true, fail: 0, n_a: 1 }, + 'BS-V2': { pass: true, fail: 0, n_a: 1 }, + } + for (const i of allIssues) bs[i.id].fail++ + const report: LintReport = { + forms: forms.length, + fields: forms.reduce((s, f) => s + f.fields.length, 0), + blindspots: bs, + issues: allIssues, + cmdBlocks: { total: blocks.length, issues: blockIssues }, + exitCode: allIssues.length === 0 && blockIssues.length === 0 ? 0 : 1, + } + if (opts.json) { + printJson(report) + } else { + log.info(`解析 ${report.forms} form, ${report.fields} 字段, ${report.cmdBlocks.total} 命令块`) + for (const [id, s] of Object.entries(bs)) { + const sym = s.fail === 0 ? '✓' : '✗' + log.info(` ${sym} ${id.padEnd(6)} ${s.fail === 0 ? 'PASS' : `FAIL (${s.fail})`}${s.n_a ? ' N/A' : ''}`) + } + if (blockIssues.length > 0) { + log.info(` ✗ DACLI-CMD 命令完整性 FAIL (${blockIssues.length} issues)`) + for (const x of blockIssues) log.info(` - ${x}`) + } else if (report.cmdBlocks.total > 0) { + log.info(` ✓ DACLI-CMD 命令完整性 PASS`) + } + if (allIssues.length > 0) { + log.info('') + log.info(`前 3 issues:`) + for (const i of allIssues.slice(0, 3)) log.info(` - [${i.id}] ${i.path}: ${i.message}`) + } + } + process.exit(report.exitCode) + }) + + installAiHelpHook(cmd, { + purpose: '设计期 lint: 复用 5 类盲点规则 (BS-23/31/32/15/V2), 不需 JNPF server. 阶段 2 末必跑, exit 0 才能进阶段 4.', + optional: { + '--from-script ': '03_forms.py 路径 (含 FORMS 列表)', + '--from-design ': 'design-jnpf.html 路径 (含
dacli 命令块)', + '--json': '原始 JSON 输出', + }, + example: [ + 'dacli form lint --from-script=docs/tdss/scripts/03_forms.py', + 'dacli form lint --from-script=... --from-design=...', + ], + notes: [ + 'BS-23 检查 design 整体是否含 4 个 use*Permission (粗略扫描)', + 'BS-31/32 检查每个 form 字段 label / decimal 类型', + 'BS-15/V2 在设计期无法直接判断, 标 N/A (运行时由 form validate 兜底)', + ], + }) + + return cmd +} \ No newline at end of file