test(dacli): form-lint 单测 T1.1-T1.5 (parsePythonForms, checkBs31/32)
This commit is contained in:
1 parent
d55ec5b219
commit
7edf55bc88
3 files changed
+141
-27
No files matched your search
@@ -90,7 +90,9 @@ export function checkBs31(formDataRaw: unknown): BlindspotIssue[] {
|
||||
}
|
||||
|
||||
const MONEY_KEYWORDS = ['price', 'amount', 'money', 'quantity', 'sum', 'total', 'fee', 'cost', 'score', 'qty']
|
||||
const MONEY_REGEX = new RegExp(`\\b(${MONEY_KEYWORDS.join('|')})\\b`, 'i')
|
||||
// 用 lookahead/lookbehind 实现"独立 token"匹配: 允许 _/字母数字边界, 防 "consumer" 误中 "sum"
|
||||
// (?<![a-z0-9]) = 前面不是字母数字; (?![a-z0-9]) = 后面不是字母数字
|
||||
const MONEY_REGEX = new RegExp(`(?<![a-z0-9])(${MONEY_KEYWORDS.join('|')})(?![a-z0-9])`, 'i')
|
||||
|
||||
export function checkBs32(formDataRaw: unknown): BlindspotIssue[] {
|
||||
const issues: BlindspotIssue[] = []
|
||||
|
||||
@@ -24,33 +24,49 @@ export function parsePythonForms(src: string): FormDef[] {
|
||||
const m = src.match(/FORMS\s*=\s*\[([\s\S]*?)\n\]/m)
|
||||
if (!m) return []
|
||||
const body = m[1]
|
||||
// 找顶层 [ 块 (字段列表)
|
||||
let i = 0
|
||||
while (i < body.length) {
|
||||
if (body[i] === '[') {
|
||||
const fieldsStart = i + 1
|
||||
let depth = 1; let j = fieldsStart
|
||||
while (j < body.length && depth > 0) {
|
||||
if (body[j] === '[') depth++
|
||||
else if (body[j] === ']') depth--
|
||||
j++
|
||||
}
|
||||
const fieldsBlock = body.slice(fieldsStart, j - 1)
|
||||
// 找前 2 个字符串 (en_code, full_name) — 简化: 假设出现在 [ 块前的行
|
||||
// 这里用更稳: 反向找 ([ 块前的 2 个 '字符串'
|
||||
const beforeFields = body.slice(Math.max(0, i - 200), i)
|
||||
const strs = [...beforeFields.matchAll(/'([^']+)'/g)].map(m => m[1])
|
||||
const enCode = strs[strs.length - 2] || '?'
|
||||
const fullName = strs[strs.length - 1] || '?'
|
||||
// 抽每个 (type, vmodel, label, req) 元组 (允许 5 元素含 extra dict)
|
||||
const fields: FieldDef[] = []
|
||||
for (const tm of fieldsBlock.matchAll(/\(\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*(True|False)/g)) {
|
||||
fields.push({ type: tm[1], vmodel: tm[2], label: tm[3], required: tm[4] === 'True' })
|
||||
}
|
||||
if (fields.length > 0) forms.push({ enCode, fullName, fields })
|
||||
i = j
|
||||
|
||||
// 第一步: 找所有 form 头元组 (enCode, fullName, [)
|
||||
// form 头的特征: 行首缩进 + ( + 'enCode', 'fullName', 紧跟 [
|
||||
// 区别于 field 元组 (4 个字符串 + True/False): 头只有 2 字符串 + [
|
||||
const formHeaders: Array<{ enCode: string; fullName: string; startIdx: number }> = []
|
||||
const formHeaderRe = /^\s*\(\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*\[/gm
|
||||
let mm: RegExpExecArray | null
|
||||
while ((mm = formHeaderRe.exec(body)) !== null) {
|
||||
formHeaders.push({ enCode: mm[1], fullName: mm[2], startIdx: mm.index })
|
||||
}
|
||||
if (formHeaders.length === 0) return []
|
||||
|
||||
// 第二步: 找所有 field 元组 (type, vmodel, label, required[, extra])
|
||||
// field 元组至少 4 个字符串 + True/False, 可能是 5 元 (含 extra dict)
|
||||
// label 允许空字符串: 用 '[^']*' (0+ chars) 而非 '[^']+' (1+ chars)
|
||||
const fieldRe = /\(\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*'([^']*)'\s*,\s*(True|False)/g
|
||||
const allFields: Array<{ type: string; vmodel: string; label: string; required: boolean; idx: number }> = []
|
||||
while ((mm = fieldRe.exec(body)) !== null) {
|
||||
allFields.push({ type: mm[1], vmodel: mm[2], label: mm[3], required: mm[4] === 'True', idx: mm.index })
|
||||
}
|
||||
|
||||
// 第三步: 对每个 form 头, 找其 [ ... ] 内的 field 元组
|
||||
for (let k = 0; k < formHeaders.length; k++) {
|
||||
const header = formHeaders[k]
|
||||
// 从 header.startIdx 之后找 [ 块
|
||||
const bracketStart = body.indexOf('[', header.startIdx)
|
||||
if (bracketStart < 0) continue
|
||||
let depth = 1
|
||||
let j = bracketStart + 1
|
||||
while (j < body.length && depth > 0) {
|
||||
if (body[j] === '[') depth++
|
||||
else if (body[j] === ']') depth--
|
||||
j++
|
||||
}
|
||||
i++
|
||||
const bracketEnd = j - 1
|
||||
// 收集此范围内的 field
|
||||
const fields: FieldDef[] = []
|
||||
for (const f of allFields) {
|
||||
if (f.idx > bracketStart && f.idx < bracketEnd) {
|
||||
fields.push({ type: f.type, vmodel: f.vmodel, label: f.label, required: f.required })
|
||||
}
|
||||
}
|
||||
if (fields.length > 0) forms.push({ enCode: header.enCode, fullName: header.fullName, fields })
|
||||
}
|
||||
return forms
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user