@stacksjs/dtsx
Version:
A modern, fast .d.ts generation tool, powered by Bun.
5 lines (4 loc) • 5.9 kB
JavaScript
// @bun
import{hd as W}from"./chunk-09ss191z.js";function Y(q,z,b,A={}){return{kind:q,name:z,text:b,isExported:A.isExported??!1,...A}}function Z(q){if(typeof structuredClone==="function")return structuredClone(q);return JSON.parse(JSON.stringify(q))}function X(q,z,b=null){for(let A of q){z.enter?.(A,b);let G=z[A.kind];if(G)G(A,b);if(A.members&&A.members.length>0)X(A.members,z,A);z.leave?.(A,b)}}function C(q,z){let b=[];return X(q,{enter:(A)=>{if(z(A))b.push(A)}}),b}async function _(q,z,b){let A=[],G=q.length;for(let F=0;F<q.length;F++){let H=q[F],I={...b,index:F,total:G,allDeclarations:q},L=await z(H,I);if(L===null)continue;else if(L===void 0)A.push(H);else if(Array.isArray(L))A.push(...L);else A.push(L)}return A}function j(...q){return async(z,b)=>{let A=z;for(let G of q){if(A===null)return null;if(Array.isArray(A)){let F=[];for(let H of A){let I=await G(H,b);if(I===null)continue;if(I===void 0)F.push(H);else if(Array.isArray(I))F.push(...I);else F.push(I)}A=F.length>0?F:null}else{let F=await G(A,b);if(F===null)A=null;else if(F===void 0);else A=F}}return A}}function O(q,z){let b=new Set(Array.isArray(q)?q:[q]);return async(A,G)=>{if(b.has(A.kind))return z(A,G);return}}function S(q,z){return async(b,A)=>{if(q(b))return z(b,A);return}}function U(q){let z={createDeclaration:Y,cloneDeclaration:Z,modifyText:(b,A)=>({...b,text:A}),addModifier:(b,A)=>({...b,modifiers:[...b.modifiers||[],A]}),removeModifier:(b,A)=>({...b,modifiers:(b.modifiers||[]).filter((G)=>G!==A)})};return W({name:q.name,version:q.version,description:q.description,onBeforeFile:q.beforeParse?async(b)=>{return q.beforeParse(b.content,{filePath:b.filePath,phase:"before"})}:void 0,onDeclarations:q.transform?async(b)=>{return _(b.declarations,q.transform,{filePath:b.filePath,sourceCode:b.sourceCode,...z})}:void 0,onAfterFile:q.afterGenerate?async(b)=>{return q.afterGenerate(b.content,{filePath:b.filePath,phase:"after"})}:void 0})}function B(q,z){let b=typeof q==="string"?new RegExp(q):q;return(A)=>{if(b.test(A.name)){let G=typeof z==="function"?z(A.name,A):A.name.replace(b,z),F=A.text.replace(new RegExp(`\\b${Q(A.name)}\\b`),G);return{...A,name:G,text:F}}return}}function $(q,z){return(b)=>{if(b.kind==="import")return;if(z&&!z(b))return;let A=`${q}${b.name}`,G=b.text.replace(new RegExp(`\\b${Q(b.name)}\\b`),A);return{...b,name:A,text:G}}}function E(q,z){return(b)=>{if(b.kind==="import")return;if(z&&!z(b))return;let A=`${b.name}${q}`,G=b.text.replace(new RegExp(`\\b${Q(b.name)}\\b`),A);return{...b,name:A,text:G}}}function h(q){let z=typeof q==="function"?q:(b)=>{return(typeof q==="string"?new RegExp(q):q).test(b.name)};return(b)=>{if(z(b))return null;return}}function K(q){return(z)=>{let b=q(z);if(!b)return;let G=(Array.isArray(b)?b:[b]).map((F)=>{if(F.startsWith("/**"))return F;if(F.startsWith("/*"))return F;return`/** ${F} */`});return{...z,leadingComments:[...z.leadingComments||[],...G]}}}function N(q){return(z)=>{if(!z.typeAnnotation)return;let b=q(z.typeAnnotation,z);if(b===null)return null;if(b===void 0)return;let A=z.text.replace(new RegExp(`:[ ]*${Q(z.typeAnnotation)}`),`: ${b}`);return{...z,typeAnnotation:b,text:A}}}function D(q){return O("function",(z)=>{if(!z.returnType)return;let b=q(z.returnType,z);if(b===null)return null;if(b===void 0)return;let A=z.text.replace(new RegExp(`\\):[ ]*${Q(z.returnType)}`),`): ${b}`);return{...z,returnType:b,text:A}})}function R(q){return O(["function","class"],(z)=>{if(!z.parameters)return;let b=q(z.parameters,z);if(b===null)return null;if(b===void 0)return;let A=b.map((F)=>{let H="";if(F.rest)H+="...";if(H+=F.name,F.optional)H+="?";if(F.type)H+=`: ${F.type}`;if(F.defaultValue)H+=` = ${F.defaultValue}`;return H}).join(", "),G=z.text.replace(/\([^)]*\)/,`(${A})`);return{...z,parameters:b,text:G}})}function J(q,z){return(b)=>{if(z&&!z(b))return;if(b.modifiers?.includes(q))return;let A=[...b.modifiers||[],q],G=b.text;if(!G.includes(q)){let F=["function","class","interface","type","enum","const","let","var"];for(let H of F){let I=G.indexOf(H);if(I>=0){G=`${G.slice(0,I)}${q} ${G.slice(I)}`;break}}}return{...b,modifiers:A,text:G}}}var M=O(["interface","type"],(q)=>{let z=q.text.replace(/(\n\s*)(\w+)(\??:\s)/g,"$1readonly $2$3");if(z===q.text)return;return{...q,text:z}}),v=O(["interface","type"],(q)=>{let z=q.text.replace(/(\w+)\?:/g,"$1:");if(z===q.text)return;return{...q,text:z}}),P=O(["interface","type"],(q)=>{let z=q.text.replace(/(\w+)(?<!\?):/g,"$1?:");if(z===q.text)return;return{...q,text:z}});function k(q){let z=new RegExp(`@(${q.join("|")})\\b[^@]*`,"g");return(b)=>{if(!b.leadingComments?.length)return;let A=b.leadingComments.map((F)=>{return F.replace(z,"").replace(/\n\s*\*\s*\n/g,`
`)});if(!A.some((F,H)=>F!==b.leadingComments[H]))return;return{...b,leadingComments:A}}}function g(q,z){return(b)=>{if(z&&!z(b))return;if(!b.typeAnnotation)return;let A=`${q}<${b.typeAnnotation}>`,G=b.text.replace(b.typeAnnotation,A);return{...b,typeAnnotation:A,text:G}}}var y=U({name:"readonly",version:"1.0.0",description:"Makes all interface properties readonly",transform:M}),T=U({name:"strip-private",version:"1.0.0",description:"Strips @internal, @private, and @hidden declarations",transform:h((q)=>{if(!q.leadingComments)return!1;return q.leadingComments.some((z)=>/@(internal|private|hidden)\b/.test(z))})}),u=U({name:"declare",version:"1.0.0",description:"Adds declare keyword to all declarations",transform:J("declare",(q)=>q.kind!=="import")});function w(q){return U({name:`namespace-prefix-${q}`,version:"1.0.0",description:`Prefixes exported declarations with ${q}`,transform:$(`${q}_`,(z)=>z.isExported&&z.kind!=="import")})}function Q(q){return q.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}
export{Y as jb,Z as kb,X as lb,C as mb,_ as nb,j as ob,O as pb,S as qb,U as rb,B as sb,$ as tb,E as ub,h as vb,K as wb,N as xb,D as yb,R as zb,J as Ab,M as Bb,v as Cb,P as Db,k as Eb,g as Fb,y as Gb,T as Hb,u as Ib,w as Jb};