UNPKG

@stacksjs/dtsx

Version:

A modern, fast .d.ts generation tool, powered by Bun.

7 lines (6 loc) 2.91 kB
// @bun import{Xd as p}from"./chunk-05vktpfb.js";import{Le as l}from"./chunk-sc0mtgkd.js";import{readFileSync as g}from"fs";import{readFile as m}from"fs/promises";import{resolve as f}from"path";function d(t){return t.replace(/\/\*[\s\S]*?\*\//g,"").replace(/^\s*\/\/.*$/gm,"").replace(/,\s*([}\]])/g,"$1")}function E(t,e){try{return{...JSON.parse(d(g(t,"utf8"))).compilerOptions,...e}}catch(r){throw Error(`Error reading tsconfig.json: ${r instanceof Error?r.message:String(r)}`)}}function y(t,e){let r=e.declarationsOnly?t.filter((s)=>s.endsWith(".d.ts")):t;if(e.include?.length){let s=e.include.map((n)=>new RegExp(n.replace(/\*/g,".*")));r=r.filter((n)=>s.some((o)=>o.test(n)))}if(e.exclude?.length){let s=e.exclude.map((n)=>new RegExp(n.replace(/\*/g,".*")));r=r.filter((n)=>!s.some((o)=>o.test(n)))}return r}async function u(t,e={}){let r=performance.now(),s=y(t,e),n=[];for(let i of s)try{let a=await m(i,"utf8");for(let c of p(a))n.push({file:i,line:c.line,column:c.column,message:c.message,code:Number(c.code.slice(4)),severity:"error",category:"Error"})}catch(a){n.push({file:i,line:0,column:0,message:a instanceof Error?a.message:String(a),code:1000,severity:"error",category:"Error"})}let o=e.maxErrors?n.slice(0,e.maxErrors):n;return{success:o.length===0,diagnostics:o,errorCount:o.length,warningCount:0,infoCount:0,filesChecked:s,durationMs:performance.now()-r}}function h(t,e={}){return u(t,{...e,declarationsOnly:!0})}async function O(t){let e=new Map;for(let r of t){let s=await m(r,"utf8").catch(()=>""),n=[];for(let o of l(s,r,!1,!0))if(o.isExported&&o.kind==="function"&&!o.returnType)n.push({line:1,column:1,message:"Exported function requires an explicit return type",declarationName:o.name,missingAnnotation:"return"});e.set(r,{compatible:n.length===0,issues:n})}return e}function C(t,e,r){let s=t.split(` `);if(e<1||e>s.length||r<1||r>s[e-1].length+1)return null;let n=r-1;for(let o=0;o<e-1;o++)n+=s[o].length+1;return n}function x(t,e,r){try{let s=g(t,"utf8"),n=C(s,e,r);if(n===null)return null;let o=l(s,t,!1).find((i)=>n>=(i.start??0)&&n<=(i.end??s.length));return o?.typeAnnotation??o?.returnType??o?.text??null}catch{return null}}function v(t,e,r){let s=x(t,e,r);return s?{type:s}:null}function R(t){if(t.success)return`\u2713 Type check passed (${t.filesChecked.length} files checked in ${t.durationMs}ms)`;return["\u2717 Type check failed",` ${t.errorCount} error(s), ${t.warningCount} warning(s)`,...t.diagnostics.map((e)=>`\u2717 ${e.file}:${e.line}:${e.column} ${e.message}`)].join(` `)}function I(t){return u(t.entrypoints.map((e)=>f(t.cwd,e)),{tsconfigPath:t.tsconfigPath,rootDir:t.root})}async function $(t,e){let s=(await Promise.all([u(t),h(e)])).flatMap((n)=>n.diagnostics).map((n)=>({sourceName:n.file,sourceType:n.message,dtsType:"<invalid>"}));return{valid:s.length===0,mismatches:s}} export{E as Od,u as Pd,h as Qd,O as Rd,x as Sd,v as Td,R as Ud,I as Vd,$ as Wd};