| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- // 静态自检(只读):SFC 能否编译 + 模板用到的标识符是否都在 <script setup> 里存在。
- // 这不是 build/dev,只是用仓库已有的 @vue/compiler-sfc 做一次语法与绑定核对。
- import fs from 'node:fs'
- import { createRequire } from 'node:module'
- const require = createRequire(import.meta.url)
- const { parse, compileScript, compileTemplate } = require('@vue/compiler-sfc')
- let failed = 0
- const ok = (name, cond) => { if (!cond) failed += 1; console.log(`${cond ? 'PASS' : 'FAIL'} ${name}`) }
- for (const file of [
- 'src/modules/cockpit/prototypes/AnchorPrototype.vue',
- 'src/pages/cockpit/index.vue',
- ]) {
- const src = fs.readFileSync(file, 'utf8')
- const { descriptor, errors } = parse(src, { filename: file })
- ok(`${file} 解析`, errors.length === 0)
- if (errors.length) { errors.forEach((e) => console.log(' -', e.message)); continue }
- try {
- const script = compileScript(descriptor, { id: 'proto' })
- ok(`${file} script 编译`, true)
- if (descriptor.template) {
- const tpl = compileTemplate({ source: descriptor.template.content, filename: file, id: 'proto' })
- ok(`${file} template 编译`, tpl.errors.length === 0)
- tpl.errors.forEach((e) => console.log(' -', e.message || e))
- }
- } catch (error) {
- ok(`${file} 编译`, false)
- console.log(' -', error.message)
- }
- }
- // 逐个核对 AnchorPrototype 模板里的顶层标识符是否真的有定义
- const file = 'src/modules/cockpit/prototypes/AnchorPrototype.vue'
- const src = fs.readFileSync(file, 'utf8')
- const { descriptor } = parse(src, { filename: file })
- const script = compileScript(descriptor, { id: 'proto' })
- const bindings = new Set(Object.keys(script.bindings || {}))
- const content = descriptor.scriptSetup.content
- const required = [
- 'ANCHOR_FORMS', 'deviceVisual', 'effectLabel', 'shapeChar', 'toneOf',
- 'variant', 'point', 'pointVisual', 'plan', 'layout', 'panelStyle', 'arrowStyle', 'stateText',
- 'deviceCount', 'pointIndex', 'barEl', 'cfg', 'statusOrder', 'selectedCode', 'selectDevice',
- 'closePanel', 'choose', 'cycle', 'rebuild', 'samplePoints', 'flyToPoints',
- ]
- const missing = required.filter((name) => !bindings.has(name) && !new RegExp(`(function|const|let)\\s+${name}\\b`).test(content))
- ok('模板/脚本引用的标识符全部有定义', missing.length === 0)
- if (missing.length) console.log(' 缺失:', missing.join(', '))
- // 关键纪律:不能残留未清理的调试语句
- ok('无 console.log 残留', !/console\.(log|debug)\(/.test(content))
- ok('无 debugger 残留', !/\bdebugger\b/.test(content))
- console.log(`\n${failed === 0 ? '静态自检全部通过' : `${failed} 项失败`}`)
- process.exit(failed === 0 ? 0 : 1)
|