151 lines
6.5 KiB
JavaScript
151 lines
6.5 KiB
JavaScript
// 契约:小程序内 `this._私有方法(...)` 调用必须在同文件有定义
|
||||
|
|
//
|
|||
|
|
// 由来(2026-09-19):运行期崩溃
|
|||
|
|
// TypeError: _this._releaseIdempotencyKey is not a function
|
|||
|
|
// 定位:miniapp/utils/api.js 里定义了 `releaseIdempotencyKey()`,
|
|||
|
|
// 但成功回调写成了 `this._releaseIdempotencyKey(...)`(**多一个下划线**)。
|
|||
|
|
//
|
|||
|
|
// 为什么危害远大于「一个方法名写错」:
|
|||
|
|
// 该调用位于 `wx.request` 的 success 回调内、**在 `resolve()` 之前**。
|
|||
|
|
// 抛错 → `resolve` 永不执行 → 该 Promise **永不 settle** → 页面永远停在 loading。
|
|||
|
|
// 即:一个字符的笔误,把「所有成功请求」变成「所有页面卡死」。
|
|||
|
|
//
|
|||
|
|
// 为什么既有门禁全都抓不到:
|
|||
|
|
// - `miniapp tsc --noEmit` 只检查 .ts;`utils/api.js` 是 **.js**,不在类型检查范围内;
|
|||
|
|
// - UI 契约门禁是「源码内容断言」(grep 关键字 / 数条目),不做符号解析。
|
|||
|
|
// 又是一处**覆盖盲区**(与 WXML 结构、`pass` 空壳同族)。
|
|||
|
|
//
|
|||
|
|
// 判据范围(**刻意收窄,宁少不误**):
|
|||
|
|
// 只检查**下划线前缀**的私有方法调用 `this._x(` / `self._x(` / `that._x(` / `_this._x(`,
|
|||
|
|
// 要求同文件内存在其定义(方法简写 `_x(`、类字段 `_x =`、对象属性 `_x:`、prototype 赋值)。
|
|||
|
|
// 不做通用「方法是否存在」检查 —— 那会把 `this.setData` / `this.selectComponent`
|
|||
|
|
// 这类运行时注入的宿主方法全部误报,守卫一旦有假阳性就会被绕过(本项目已有先例)。
|
|||
|
|
|
|||
|
|
import assert from 'node:assert/strict'
|
|||
|
|
import { readFileSync, readdirSync } from 'node:fs'
|
|||
|
|
import { extname, join, relative, resolve } from 'node:path'
|
|||
|
|
|
|||
|
|
const root = resolve(import.meta.dirname, '..')
|
|||
|
|
// SCAN_ROOT 仅用于负向注入自证(指向临时目录),默认 miniapp/
|
|||
|
|
const scanRoot = resolve(root, process.env.MINIAPP_SCAN_ROOT ?? 'miniapp')
|
|||
|
|
|
|||
|
|
const SKIP_DIRS = new Set(['node_modules', 'miniprogram_npm', 'dist', 'build', '.git'])
|
|||
|
|
|
|||
|
|
function walk(dir, out = []) {
|
|||
|
|
let entries
|
|||
|
|
try {
|
|||
|
|
entries = readdirSync(dir, { withFileTypes: true })
|
|||
|
|
} catch {
|
|||
|
|
return out
|
|||
|
|
}
|
|||
|
|
for (const e of entries) {
|
|||
|
|
const p = join(dir, e.name)
|
|||
|
|
if (e.isDirectory()) {
|
|||
|
|
if (!SKIP_DIRS.has(e.name)) walk(p, out)
|
|||
|
|
} else if (['.js', '.ts'].includes(extname(e.name)) && !e.name.endsWith('.d.ts')) {
|
|||
|
|
out.push(p)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return out
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const RECEIVERS = ['this', 'self', 'that', '_this']
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 收集同文件内定义的私有成员名。
|
|||
|
|
*
|
|||
|
|
* 定义形态(必须都覆盖,否则产生假阳性):
|
|||
|
|
* 1. 方法简写 / 函数声明:`_x() {}`、`function _x()`
|
|||
|
|
* 2. 类字段 / 对象属性:`_x: ...`、`_x = ...`
|
|||
|
|
* 3. 实例属性赋值:`this._x = ...`(**首版漏了这条** —— `_unsub` 就属于这种,
|
|||
|
|
* 它由 `this._unsub = matchStore.bindToPage(...)` 定义、在 `onUnload` 里调用,
|
|||
|
|
* 首版判据把它误报成「未定义」。注意正则前缀必须允许 `this.` 这种带点的形式)
|
|||
|
|
* 4. prototype 赋值:`.prototype._x =`
|
|||
|
|
*
|
|||
|
|
* 例外:赋值为 `undefined` / `null` 不算定义(后面调用照样会抛)。
|
|||
|
|
*/
|
|||
|
|
function collectDefinitions(src, names) {
|
|||
|
|
const defined = new Set()
|
|||
|
|
for (const n of names) {
|
|||
|
|
const esc = n.replace(/[$]/g, '\\$')
|
|||
|
|
const patterns = [
|
|||
|
|
new RegExp(`(^|[^\\w$.])${esc}\\s*\\(`, 'm'), // 方法简写 _x(
|
|||
|
|
// 注意 lookahead 里必须再吃一次 `\s*`:写成 `=\s*(?!undefined|null)`
|
|||
|
|
// 会因 `\s*` 回溯到零宽而在「= null」上误判成功(实测踩到)。
|
|||
|
|
new RegExp(`(^|[^\\w$.])${esc}\\s*[:=]\\s*(?!\\s*(?:undefined|null)\\b)`, 'm'),
|
|||
|
|
new RegExp(`\\b(?:this|self|that|_this)\\.${esc}\\s*=\\s*(?!\\s*(?:undefined|null)\\b)`, 'm'),
|
|||
|
|
new RegExp(`\\.prototype\\.${esc}\\s*=`, 'm'), // prototype 赋值
|
|||
|
|
]
|
|||
|
|
if (patterns.some((re) => re.test(src))) defined.add(n)
|
|||
|
|
}
|
|||
|
|
return defined
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const files = walk(scanRoot)
|
|||
|
|
assert.ok(files.length > 0, `未在 ${scanRoot} 下找到任何 .js/.ts —— 扫描清单可能失配,拒绝报绿`)
|
|||
|
|
|
|||
|
|
const violations = []
|
|||
|
|
|
|||
|
|
for (const file of files) {
|
|||
|
|
const src = readFileSync(file, 'utf8')
|
|||
|
|
|
|||
|
|
// 1) 找出所有 `接收者._私有名(` 调用
|
|||
|
|
const callRe = new RegExp(`\\b(${RECEIVERS.join('|')})\\.(_[A-Za-z_$][\\w$]*)\\s*\\(`, 'g')
|
|||
|
|
const calls = new Map() // name -> firstLine
|
|||
|
|
let m
|
|||
|
|
while ((m = callRe.exec(src))) {
|
|||
|
|
const name = m[2]
|
|||
|
|
if (!calls.has(name)) calls.set(name, src.slice(0, m.index).split('\n').length)
|
|||
|
|
}
|
|||
|
|
if (calls.size === 0) continue
|
|||
|
|
|
|||
|
|
// 2) 同文件内必须能找到定义
|
|||
|
|
const defined = collectDefinitions(src, [...calls.keys()])
|
|||
|
|
for (const [name, line] of calls) {
|
|||
|
|
if (!defined.has(name)) {
|
|||
|
|
violations.push(
|
|||
|
|
`${relative(root, file).replace(/\\/g, '/')}:${line} 调用了 ${RECEIVERS[0]}.${name}() 但同文件无该定义`,
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3) 负向自检:确认判据本身有牙(一个「只调用、无定义」的样例必须被判红)
|
|||
|
|
{
|
|||
|
|
const sample = 'class T {\n go() {\n return this._missingHelper(1)\n }\n}\n'
|
|||
|
|
const callRe = new RegExp(`\\b(${RECEIVERS.join('|')})\\.(_[A-Za-z_$][\\w$]*)\\s*\\(`, 'g')
|
|||
|
|
const found = [...sample.matchAll(callRe)].map((x) => x[2])
|
|||
|
|
assert.ok(found.includes('_missingHelper'), '自检失败:判据未能识别 this._x( 调用')
|
|||
|
|
assert.deepEqual(
|
|||
|
|
[...collectDefinitions(sample, ['_missingHelper'])],
|
|||
|
|
[],
|
|||
|
|
'自检失败:判据把未定义的方法误判为已定义',
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 4) 反向自检:**假阳性**防线。
|
|||
|
|
// `this._unsub = expr` 这种「运行时赋值定义」必须被认成有定义,
|
|||
|
|
// 否则守卫会把合法代码报成缺陷(首版即如此,误报 3 个页面)。
|
|||
|
|
{
|
|||
|
|
const assignDefined = 'Page({\n onLoad() {\n this._unsub = store.bind(this)\n },\n onUnload() {\n if (this._unsub) this._unsub()\n },\n})\n'
|
|||
|
|
assert.deepEqual(
|
|||
|
|
[...collectDefinitions(assignDefined, ['_unsub'])],
|
|||
|
|
['_unsub'],
|
|||
|
|
'自检失败:`this._x = 值` 形式未被认成定义(会导致假阳性)',
|
|||
|
|
)
|
|||
|
|
const assignNull = 'Page({\n onLoad() {\n this._late = null\n },\n go() {\n this._late()\n },\n})\n'
|
|||
|
|
assert.deepEqual(
|
|||
|
|
[...collectDefinitions(assignNull, ['_late'])],
|
|||
|
|
[],
|
|||
|
|
'自检失败:赋值为 null/undefined 不应被认成可用定义',
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
assert.deepEqual(
|
|||
|
|
violations,
|
|||
|
|
[],
|
|||
|
|
`以下私有方法调用在同文件内找不到定义(会抛 TypeError;若位于 resolve 之前将导致 Promise 永不 settle):\n${violations.join('\n')}`,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
console.log(`Miniapp self-method contracts passed(扫描 ${files.length} 个文件)`)
|