|
|
@@ -11,11 +11,16 @@
|
|
|
* 5) `transition: all`
|
|
|
* 6) `v-for` 缺 `:key`(支持跨行)
|
|
|
* 7) 日期用 `toLocaleString` / `toLocaleDateString` / `toLocaleTimeString` 格式化
|
|
|
+ * 8) `v-for` 的 `:key` 是方法调用,且作用域内事件/v-model 传条目变量
|
|
|
+ * → uni-app 把 `:key` 表达式当作「找回条目」的属性路径,方法调用会让
|
|
|
+ * 运行时 `find(v => v['getXxx(item)'] === value)` 恒失败,handler 收到
|
|
|
+ * `undefined`(`Cannot read properties of undefined (reading 'id')`)。
|
|
|
+ * `:key` 必须是条目真实属性(`item.id`)或循环索引别名(`idx`)。
|
|
|
* WARN — 需人工确认
|
|
|
- * 8) `new Date(<非字面量>)`:iOS 解析空格分隔/Java 格式会失败,应改 `parseDate()`
|
|
|
+ * 9) `new Date(<非字面量>)`:iOS 解析空格分隔/Java 格式会失败,应改 `parseDate()`
|
|
|
* (`new Date()` 与 `new Date(Date 对象)` 不计入)
|
|
|
- * 9) `:class` / `:style` 内带参方法调用:uni-app 渲染函数下可用,
|
|
|
- * 但 AGENTS.md 列为禁用项,仅提示(WXML 原生表达式不支持)
|
|
|
+ * 10) `:class` / `:style` 内带参方法调用:uni-app 渲染函数下可用,
|
|
|
+ * 但 AGENTS.md 列为禁用项,仅提示(WXML 原生表达式不支持)
|
|
|
*
|
|
|
* 用法: node scripts/audit-mp-antipatterns.js [--json] [--error-only]
|
|
|
* 退出码: 0 无 ERROR / 1 存在 ERROR
|
|
|
@@ -45,6 +50,118 @@ function add(level, kind, file, line, text) {
|
|
|
findings.push({ level, kind, file, line, text: String(text).trim().slice(0, 140) })
|
|
|
}
|
|
|
|
|
|
+// ---------- 规则 8 辅助:模板标签树解析 ----------
|
|
|
+function escRe(s) { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') }
|
|
|
+
|
|
|
+// 顶层逗号切分(跨越括号/引号的内容视为内部)
|
|
|
+function splitTop(str) {
|
|
|
+ const out = []
|
|
|
+ let depth = 0, cur = '', q = null
|
|
|
+ for (const ch of str) {
|
|
|
+ if (q) { cur += ch; if (ch === q) q = null; continue }
|
|
|
+ if (ch === '"' || ch === "'") { q = ch; cur += ch; continue }
|
|
|
+ if ('([{'.includes(ch)) depth++
|
|
|
+ else if (')]}'.includes(ch)) depth--
|
|
|
+ if (ch === ',' && depth === 0) { out.push(cur.trim()); cur = ''; continue }
|
|
|
+ cur += ch
|
|
|
+ }
|
|
|
+ if (cur.trim()) out.push(cur.trim())
|
|
|
+ return out
|
|
|
+}
|
|
|
+
|
|
|
+function parseAttrs(raw) {
|
|
|
+ const attrs = {}
|
|
|
+ const re = /([:@]?[A-Za-z_][\w.:-]*)\s*=\s*(?:"([^"]*)"|'([^']*)')/g
|
|
|
+ let m
|
|
|
+ while ((m = re.exec(raw))) {
|
|
|
+ const name = m[1]
|
|
|
+ const val = m[2] !== undefined ? m[2] : m[3]
|
|
|
+ if (!(name in attrs)) attrs[name] = val
|
|
|
+ }
|
|
|
+ return attrs
|
|
|
+}
|
|
|
+
|
|
|
+// v-for="(item, idx) in list" 或 v-for="item in list"
|
|
|
+function parseVFor(expr) {
|
|
|
+ if (!expr) return null
|
|
|
+ const m = String(expr).trim().match(/^\s*(?:\(([^)]*)\)|([A-Za-z_$][\w$]*))\s+(?:in|of)\s+/)
|
|
|
+ if (!m) return null
|
|
|
+ const vars = (m[1] != null ? m[1] : m[2]).split(',').map(s => s.trim()).filter(Boolean)
|
|
|
+ if (!vars.length) return null
|
|
|
+ return { item: vars[0], index: vars[1] || null }
|
|
|
+}
|
|
|
+
|
|
|
+// :key 是否为方法调用表达式(含 item.getId() 这类挂在条目上的方法调用)
|
|
|
+function isMethodCallKey(expr) {
|
|
|
+ return /^[A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)*\s*\(/.test((expr || '').trim())
|
|
|
+}
|
|
|
+
|
|
|
+// 括号配平且调用括号延伸到末尾时才返回参数字符串,否则 null(保守不判)
|
|
|
+function callInner(expr) {
|
|
|
+ const t = String(expr).trim()
|
|
|
+ const i = t.indexOf('(')
|
|
|
+ if (i < 0) return null
|
|
|
+ let depth = 0
|
|
|
+ for (let k = i; k < t.length; k++) {
|
|
|
+ if (t[k] === '(') depth++
|
|
|
+ else if (t[k] === ')') { depth--; if (depth === 0) return k === t.length - 1 ? t.slice(i + 1, k) : null }
|
|
|
+ }
|
|
|
+ return null
|
|
|
+}
|
|
|
+
|
|
|
+// arg 是否是 alias 的裸引用或成员引用(含括号则编译器会预计算内联,安全)
|
|
|
+function refAlias(arg, aliases) {
|
|
|
+ for (const a of aliases) {
|
|
|
+ if (arg === a) return a
|
|
|
+ if (!arg.includes('(') && new RegExp('^' + escRe(a) + '(?:\\.[A-Za-z_$][\\w$]*|\\[[^\\]]*\\])+$').test(arg)) return a
|
|
|
+ }
|
|
|
+ return null
|
|
|
+}
|
|
|
+
|
|
|
+// 在元素子树内找「事件/v-model 传条目别名」的绑定
|
|
|
+function scanScoped(el, aliases, hits) {
|
|
|
+ for (const child of el.children) {
|
|
|
+ const inner = parseVFor(child.attrs['v-for'])
|
|
|
+ const childAliases = inner ? aliases.concat([inner.item]) : aliases
|
|
|
+ for (const [name, val] of Object.entries(child.attrs)) {
|
|
|
+ if (name === 'v-model') {
|
|
|
+ if (refAlias(val, childAliases)) hits.push({ line: child.line, via: 'v-model="' + val + '"' })
|
|
|
+ } else if (name.charAt(0) === '@' || name.indexOf('v-on:') === 0) {
|
|
|
+ const ia = callInner(val)
|
|
|
+ if (ia !== null && splitTop(ia).some(a => refAlias(a, childAliases))) hits.push({ line: child.line, via: name + '="' + val + '"' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ scanScoped(child, childAliases, hits)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 解析模板为标签树(line 为源文件绝对行号)
|
|
|
+function parseTree(tplBody, tplOffset, src) {
|
|
|
+ const clean = tplBody.replace(/<!--[\s\S]*?-->/g, m => m.replace(/[^\n]/g, ' '))
|
|
|
+ const tagRe = /<(\/)?([A-Za-z][\w-]*)((?:"[^"]*"|'[^']*'|[^'">])*)(\/)?>/g
|
|
|
+ const roots = []
|
|
|
+ const stack = []
|
|
|
+ let m
|
|
|
+ while ((m = tagRe.exec(clean))) {
|
|
|
+ const closing = !!m[1]
|
|
|
+ const tag = m[2]
|
|
|
+ const attrsRaw = m[3] || ''
|
|
|
+ const selfClose = !!m[4]
|
|
|
+ const line = src.slice(0, tplOffset + m.index).split('\n').length
|
|
|
+ if (closing) {
|
|
|
+ for (let i = stack.length - 1; i >= 0; i--) {
|
|
|
+ if (stack[i].tag === tag) { stack.length = i; break }
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ const el = { tag, raw: m[0], attrs: parseAttrs(attrsRaw), children: [], line }
|
|
|
+ const parent = stack[stack.length - 1]
|
|
|
+ if (parent) parent.children.push(el); else roots.push(el)
|
|
|
+ if (!selfClose) stack.push(el)
|
|
|
+ }
|
|
|
+ return roots
|
|
|
+}
|
|
|
+
|
|
|
for (const abs of collect(ROOT)) {
|
|
|
const rel = path.relative(ROOT, abs).split(path.sep).join('/')
|
|
|
if (SKIP_FILES.has(rel)) continue
|
|
|
@@ -92,6 +209,38 @@ for (const abs of collect(ROOT)) {
|
|
|
add('ERROR', 'v-for 缺 :key', rel, line, body.replace(/\s+/g, ' '))
|
|
|
}
|
|
|
|
|
|
+ // v-for 的 :key 是方法调用 → uni-app 会把该表达式当作「找回条目」的属性路径,
|
|
|
+ // 运行时 find(v => v['getXxx(item)'] === value) 恒失败 → handler 收到 undefined
|
|
|
+ if (tplStart !== -1 && tplEnd !== -1) {
|
|
|
+ const tplOffset = tplStart + tplMatch[0].length
|
|
|
+ const roots = parseTree(src.slice(tplOffset, tplEnd), tplOffset, src)
|
|
|
+ const walk = (els) => {
|
|
|
+ for (const el of els) {
|
|
|
+ const vf = parseVFor(el.attrs['v-for'])
|
|
|
+ const keyExpr = el.attrs[':key'] || el.attrs['v-bind:key']
|
|
|
+ if (vf && keyExpr && isMethodCallKey(keyExpr)) {
|
|
|
+ const hits = []
|
|
|
+ for (const [name, val] of Object.entries(el.attrs)) {
|
|
|
+ if (name === 'v-model') {
|
|
|
+ if (refAlias(val, [vf.item])) hits.push({ line: el.line, via: 'v-model="' + val + '"' })
|
|
|
+ } else if (name.charAt(0) === '@' || name.indexOf('v-on:') === 0) {
|
|
|
+ const ia = callInner(val)
|
|
|
+ if (ia !== null && splitTop(ia).some(a => refAlias(a, [vf.item]))) hits.push({ line: el.line, via: name + '="' + val + '"' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ scanScoped(el, [vf.item], hits)
|
|
|
+ if (hits.length) {
|
|
|
+ add('ERROR', ':key 方法调用 → 事件参数 undefined', rel, el.line,
|
|
|
+ ':key="' + keyExpr + '"(v-for=' + el.attrs['v-for'] + ');作用域内 ' + hits.length +
|
|
|
+ ' 处事件/v-model 传条目变量 → ' + hits[0].via)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ walk(el.children)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ walk(roots)
|
|
|
+ }
|
|
|
+
|
|
|
// CSS 选择器含中文(排除注释行)
|
|
|
for (const m of src.matchAll(/^<style[^>]*>([\s\S]*?)^<\/style>/gm)) {
|
|
|
const base = src.slice(0, m.index).split('\n').length
|