142 lines
5.7 KiB
JavaScript
142 lines
5.7 KiB
JavaScript
'use strict'
|
||
|
||
// 搬家等价性校验器(2026-10-28)
|
||
//
|
||
// 用途:做「纯搬移」类重构(把函数/组件从 app.js 搬到 src/xxx.js,或模块间搬)时,
|
||
// **自动证明"只搬没改"** —— 从 git 历史取搬家前的源码,与搬家后的源码做规范化比对。
|
||
//
|
||
// 为什么需要它:e2e 是"抽样验证行为",覆盖率是"看有没有路径丢失",
|
||
// 但两者都**不能证明**你没顺手改一行逻辑。纯搬移可以做到**逐字等价**,那就该逐字验。
|
||
//
|
||
// 用法:
|
||
// node scripts/verify-move.js --from HEAD~1 --name ImagePreview --to src/ui-preview.js
|
||
// node scripts/verify-move.js --from v3.24.5 --name SettingsModal --to src/ui-settings.js --verbose
|
||
//
|
||
// 退出码:0 = 等价(只搬没改);1 = 有差异(打印首个差异位置);2 = 用法/定位错误
|
||
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
const cp = require('child_process')
|
||
|
||
const root = path.resolve(__dirname, '..')
|
||
|
||
function arg(name, def) {
|
||
const i = process.argv.indexOf('--' + name)
|
||
return i >= 0 ? process.argv[i + 1] : def
|
||
}
|
||
const fromRev = arg('from')
|
||
const fname = arg('name')
|
||
const toFile = arg('to', 'app.js')
|
||
const verbose = process.argv.includes('--verbose')
|
||
|
||
if (!fromRev || !fname) {
|
||
console.error('用法: node scripts/verify-move.js --from <git rev> --name <函数名> [--to <目标文件>]')
|
||
process.exit(2)
|
||
}
|
||
|
||
function gitShow(rev, file) {
|
||
try { return cp.execFileSync('git', ['show', rev + ':' + file], { cwd: root, encoding: 'utf8' }) }
|
||
catch (e) { return null }
|
||
}
|
||
|
||
/** 按名字抽取函数/常量声明源码(花括号配平,跳过块注释与字符串里的括号) */
|
||
function extract(src, name) {
|
||
if (!src) return null
|
||
const patterns = [
|
||
new RegExp('^\\s*(?:const|let|var) ' + name + '\\s*=', 'm'),
|
||
new RegExp('^\\s*(?:async )?function ' + name + '\\s*\\(', 'm'),
|
||
]
|
||
let start = -1
|
||
for (const re of patterns) { const m = re.exec(src); if (m) { start = m.index; break } }
|
||
if (start < 0) return null
|
||
// 从 start 往后找第一个 { ,随后配平
|
||
let i = src.indexOf('{', start)
|
||
if (i < 0) return null
|
||
let depth = 0, inBlock = false, inLine = false, inStr = null
|
||
for (let j = i; j < src.length; j++) {
|
||
const c = src[j], d = src[j + 1]
|
||
if (inLine) { if (c === '\n') inLine = false; continue }
|
||
if (inBlock) { if (c === '*' && d === '/') { inBlock = false; j++ } continue }
|
||
if (inStr) { if (c === '\\') { j++; continue } if (c === inStr) inStr = null; continue }
|
||
if (c === '/' && d === '*') { inBlock = true; j++; continue }
|
||
if (c === '/' && d === '/') { inLine = true; j++; continue }
|
||
if (c === '"' || c === "'" || c === '`') { inStr = c; continue }
|
||
if (c === '{') depth++
|
||
else if (c === '}') { depth--; if (depth === 0) return src.slice(start, j + 1) }
|
||
}
|
||
return null
|
||
}
|
||
|
||
/** 规范化:去注释、去行首尾空白、去空行 —— 只比"代码语义文本",不管排版 */
|
||
function normalize(code) {
|
||
return code
|
||
.replace(/\/\*[\s\S]*?\*\//g, ' ')
|
||
.replace(/(^|[^:])\/\/[^\n]*/g, '$1')
|
||
.split('\n').map((l) => l.trim()).filter(Boolean).join('\n')
|
||
}
|
||
|
||
/**
|
||
* 二级比对:忽略**字符串外的一切空白**。
|
||
* 为什么需要:把函数搬进 src 模块时缩进会变(2 空格 → IIFE 内 4 空格),
|
||
* 严格比对会误报"不等价"。这一级只回答"代码字符是否完全相同"。
|
||
*/
|
||
function stripAllWhitespace(code) {
|
||
let out = ''
|
||
let inStr = null
|
||
for (let i = 0; i < code.length; i++) {
|
||
const c = code[i]
|
||
if (inStr) {
|
||
out += c
|
||
if (c === '\\') { out += code[i + 1] || ''; i++; continue }
|
||
if (c === inStr) inStr = null
|
||
continue
|
||
}
|
||
if (c === '"' || c === "'" || c === '`') { inStr = c; out += c; continue }
|
||
if (/\s/.test(c)) continue
|
||
out += c
|
||
}
|
||
return out
|
||
}
|
||
|
||
function firstDiff(a, b) {
|
||
const la = a.split('\n'), lb = b.split('\n')
|
||
const n = Math.max(la.length, lb.length)
|
||
for (let i = 0; i < n; i++) {
|
||
if (la[i] !== lb[i]) return { line: i + 1, before: la[i] === undefined ? '(缺失)' : la[i], after: lb[i] === undefined ? '(缺失)' : lb[i] }
|
||
}
|
||
return null
|
||
}
|
||
|
||
const beforeSrc = gitShow(fromRev, 'app.js')
|
||
if (!beforeSrc) { console.error('取不到 ' + fromRev + ':app.js'); process.exit(2) }
|
||
const before = extract(beforeSrc, fname)
|
||
if (!before) { console.error('在 ' + fromRev + ':app.js 里找不到 ' + fname); process.exit(2) }
|
||
|
||
let afterSrc
|
||
try { afterSrc = fs.readFileSync(path.join(root, toFile), 'utf8') } catch (e) { console.error('读不到 ' + toFile); process.exit(2) }
|
||
const after = extract(afterSrc, fname)
|
||
if (!after) { console.error('在 ' + toFile + ' 里找不到 ' + fname); process.exit(2) }
|
||
|
||
const na = normalize(before), nb = normalize(after)
|
||
if (na === nb) {
|
||
console.log('✓ 搬家等价: ' + fname + '(' + fromRev + ':app.js → ' + toFile + ')')
|
||
console.log(' 规范化后 ' + na.split('\n').length + ' 行完全相同 —— 只搬没改')
|
||
if (verbose) console.log('\n' + nb)
|
||
process.exit(0)
|
||
}
|
||
|
||
// 二级判定:仅空白/缩进差异(搬家进 IIFE 会改缩进)⇒ 仍算等价
|
||
if (stripAllWhitespace(na) === stripAllWhitespace(nb)) {
|
||
console.log('✓ 搬家等价: ' + fname + '(' + fromRev + ':app.js → ' + toFile + ')')
|
||
console.log(' 仅空白/缩进差异(字符序列完全一致) —— 只搬没改')
|
||
process.exit(0)
|
||
}
|
||
|
||
const d = firstDiff(na, nb)
|
||
console.log('✗ 搬家不等价: ' + fname + '(' + fromRev + ':app.js → ' + toFile + ')')
|
||
console.log(' 首个差异在第 ' + d.line + ' 行:')
|
||
console.log(' 搬家前: ' + String(d.before).slice(0, 200))
|
||
console.log(' 搬家后: ' + String(d.after).slice(0, 200))
|
||
console.log(' 提示: 纯搬移不应有差异;若确实要改逻辑,请单独一次提交并说明原因。')
|
||
process.exit(1)
|