@stacksjs/dtsx
Version:
A modern, fast .d.ts generation tool, powered by Bun.
4 lines (3 loc) • 1.72 kB
JavaScript
// @bun
function I(w,D){let E=1,F=1;for(let G=0;G<D;G++)if(w.charCodeAt(G)===10)E++,F=1;else F++;return{line:E,column:F}}function O(w){let D=[],E=[],F=[],G={")":"(","]":"[","}":"{"},J=0,M=0,H=-1;for(let j=0;j<w.length;j++){let z=w.charCodeAt(j),A=w.charCodeAt(j+1);if(H!==-1){if(z===42&&A===47)E.push({start:H,end:j+2}),H=-1,j++;continue}if(J){if(z===92)j++;else if(z===J)E.push({start:M,end:j+1}),J=0;continue}if(z===47&&A===42){H=j,j++;continue}if(z===47&&A===47){let K=j;while(j<w.length&&w.charCodeAt(j)!==10)j++;E.push({start:K,end:j});continue}if(z===39||z===34||z===96){J=z,M=j;continue}let B=w[j];if(B==="("||B==="["||B==="{")F.push({char:B,offset:j});else if(G[B]){let K=F.pop();if(!K||K.char!==G[B])D.push({...I(w,j),message:`Unexpected closing delimiter ${B}`,code:"DTSX1004"})}}if(J)D.push({...I(w,M),message:"Unterminated string literal",code:"DTSX1002"});if(H!==-1)D.push({...I(w,H),message:"Unterminated block comment",code:"DTSX1003"});for(let j of F)D.push({...I(w,j.offset),message:`Unclosed delimiter ${j.char}`,code:"DTSX1001"});let N=[[/\b(?:interface|class|enum)\s*(?=[{=])/g,"Declaration name expected"],[/\bfunction\s*(?=\()/g,"Function name expected"],[/\(\s*:/g,"Parameter name expected"]];for(let[j,z]of N)for(let A of w.matchAll(j)){if(E.some((B)=>A.index>=B.start&&A.index<B.end))continue;D.push({...I(w,A.index),message:z,code:"DTSX1005"})}for(let j of w.matchAll(/\btype\s*(?=[{=])/g)){if(E.some((A)=>j.index>=A.start&&j.index<A.end))continue;let z=w.slice(0,j.index).trimEnd();if(z.endsWith("import")||z.endsWith("export"))continue;D.push({...I(w,j.index),message:"Declaration name expected",code:"DTSX1005"})}return D.sort((j,z)=>j.line-z.line||j.column-z.column)}
export{O as Xd};