cmvr-wms/scripts/projectdev/vue3-cell-overflow-audit.cjs

32 lines
2.8 KiB
JavaScript
Raw Normal View History

// Source audit: text columns may truncate; interactive and status cells must not.
const fs=require('node:fs'),path=require('node:path')
const root=path.resolve(__dirname,'../..'),src=path.join(root,'ruoyi-ui-vue3/src')
const {parse:sfc}=require(path.join(root,'ruoyi-ui-vue3/node_modules/@vue/compiler-sfc'))
const {parse}=require(path.join(root,'ruoyi-ui-vue3/node_modules/@vue/compiler-dom'))
const files=dir=>fs.readdirSync(dir,{withFileTypes:true}).flatMap(e=>e.isDirectory()?files(path.join(dir,e.name)):e.name.endsWith('.vue')?[path.join(dir,e.name)]:[])
const attr=(n,key)=>n.props?.find(p=>p.type===6&&p.name===key)?.value?.content
const bound=(n,key)=>n.props?.find(p=>p.type===7&&p.arg?.content===key)?.exp?.content
const buttons=new Set(['el-button','el-dropdown'])
const interactive=new Set(['el-switch','el-checkbox','el-radio','el-input-number','el-input','el-select','el-date-picker','el-cascader','purchase-cell','item-picker','product-picker','purchase-purpose','item-category-select','remote-picker','supplier-picker'])
const status=new Set(['el-tag','dict-tag','el-progress'])
function tags(n,out=new Set()){if(n.type===1)out.add(n.tag);for(const c of n.children||[])tags(c,out);return out}
const report={files:0,columns:0,guarded:0,candidates:[],textColumns:0}
for(const file of files(src)){
const source=fs.readFileSync(file,'utf8'),block=sfc(source).descriptor.template;if(!block)continue
report.files++
function walk(n){if(n.type===1&&n.tag==='el-table-column'){
report.columns++;const label=attr(n,'label')||bound(n,'label')||'',type=attr(n,'type'),children=tags(n)
const controls=[...children].filter(t=>interactive.has(t)),actions=[...children].filter(t=>buttons.has(t)),badges=[...children].filter(t=>status.has(t))
const role=controls.length?'business-control-cell':actions.length?'business-operation-cell':badges.length||/状态|^结果$/.test(label)?'business-status-cell':null
const cls=(attr(n,'class-name')||'')+' '+(bound(n,'class-name')||'')
if(role&&!['expand','selection','index'].includes(type)){
if(/business-(operation|status|control)-cell/.test(cls))report.guarded++
else {const last=n.props.at(-1)?.loc.end.offset??n.loc.start.offset+n.tag.length+1;report.candidates.push({file:path.relative(root,file).replace(/\\/g,'/'),line:block.loc.start.line+n.loc.start.line-1,label,prop:attr(n,'prop'),width:attr(n,'width')||attr(n,'min-width'),controls,actions,badges,role,offset:block.loc.start.offset+n.loc.start.offset,tag:block.content.slice(n.loc.start.offset,block.content.indexOf('>',last)+1)})}
}else report.textColumns++
}
for(const c of n.children||[])walk(c)}
walk(parse(block.content))
}
module.exports=report
if(require.main===module){console.log(JSON.stringify(report,null,2));if(process.argv.includes('--check')&&report.candidates.length)process.exitCode=1}