UNPKG

@stacksjs/dtsx

Version:

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

4 lines (3 loc) 4.25 kB
// @bun var Z=[{pattern:/^Promise<void>$/,replacement:"Promise<void>",priority:-1},{pattern:/^Record<string,\s*any>$/,replacement:"Record<string, unknown>",priority:0},{pattern:/^Array<([^<>]+)>$/,replacement:"$1[]",priority:0}],$=[{pattern:/\bany\b/,replacement:"unknown",global:!0,priority:10},{pattern:/^object$/,replacement:"Record<string, unknown>",priority:10},{pattern:/^Function$/,replacement:"(...args: unknown[]) => unknown",priority:10}],k=[{pattern:/^(\w+)\[\]$/,replacement:"readonly $1[]",priority:5},{pattern:/^Array<(.+)>$/,replacement:"ReadonlyArray<$1>",priority:5},{pattern:/^Map<(.+),\s*(.+)>$/,replacement:"ReadonlyMap<$1, $2>",priority:5},{pattern:/^Set<(.+)>$/,replacement:"ReadonlySet<$1>",priority:5}],v=[{pattern:/^Partial<Partial<(.+)>>$/,replacement:"Partial<$1>",priority:5},{pattern:/^Required<Required<(.+)>>$/,replacement:"Required<$1>",priority:5},{pattern:/^Pick<Omit<(.+),\s*(.+)>,\s*(.+)>$/,replacement:"Pick<$1, $3>",priority:5}];function w(j){switch(j){case"strict":return $;case"readonly":return k;case"simplified":return v;case"nullable":return[{pattern:/^(.+)\s*\|\s*undefined$/,replacement:"$1 | undefined",priority:5}];case"branded":return[{pattern:/^string$/,replacement:"string",condition:(q)=>q.declarationKind==="variable"&&(q.declarationName?.includes("path")??!1),priority:5}];default:return[]}}var G=/[.*+?^${}()|[\]\\]/g,Q=2048;class V{rules;compiled=[];cache=new Map;constructor(j={rules:[]}){if(this.rules=[],j.includeDefaults!==!1)this.rules.push(...Z);if(j.presets)for(let q of j.presets)this.rules.push(...w(q));this.rules.push(...j.rules),this.rules.sort((q,z)=>(z.priority||0)-(q.priority||0)),this.recompile()}recompile(){let j=Array(this.rules.length);for(let q=0;q<this.rules.length;q++){let z=this.rules[q],B=typeof z.pattern==="string"?new RegExp(z.pattern.replace(G,"\\$&"),z.global?"g":""):z.pattern;j[q]={rule:z,regex:B}}this.compiled=j}map(j,q={}){let B=!q.declarationName&&!q.declarationKind&&!q.filePath&&!q.isReturnType&&!q.isParameterType&&!q.isPropertyType?j:`${j}\x00${q.declarationName??""}\x00${q.declarationKind??""}\x00${q.filePath??""}\x00${q.isReturnType?"r":""}${q.isParameterType?"p":""}${q.isPropertyType?"P":""}`,N=this.cache.get(B);if(N!==void 0)return N;let F=j,O=null;for(let J=0;J<this.compiled.length;J++){let{rule:I,regex:D}=this.compiled[J];if(I.condition){if(!O)O={type:j,...q};if(!I.condition(O))continue}if(D.lastIndex=0,D.test(F)){if(D.lastIndex=0,F=F.replace(D,I.replacement),D.lastIndex=0,!I.global)break}}if(this.cache.size>=Q){let J=Math.ceil(Q*0.1),I=0;for(let D of this.cache.keys()){if(I++>=J)break;this.cache.delete(D)}}return this.cache.set(B,F),F}mapAll(j,q={}){return j.map((z)=>this.map(z,{...q,type:z}))}addRule(j){this.rules.push(j),this.rules.sort((q,z)=>(z.priority||0)-(q.priority||0)),this.recompile(),this.cache.clear()}removeRules(j){let q=this.rules.length;return this.rules=this.rules.filter((z)=>{let B=typeof z.pattern==="string"?z.pattern:z.pattern.source;if(j instanceof RegExp)j.lastIndex=0;return typeof j==="string"?B!==j:!j.test(B)}),this.recompile(),this.cache.clear(),q-this.rules.length}clearCache(){this.cache.clear()}getRules(){return this.rules}}function W(j={rules:[]}){return new V(j)}var L=W({rules:[]}),S=W({rules:[],presets:["strict"]});function U(j,q,z={}){return j.replace(/:\s*([^;,)\]}]+)/g,(B,N)=>{return`: ${q.map(N.trim(),z)}`})}var H={makeReadonly:(j)=>{if(j.endsWith("[]"))return`readonly ${j}`;if(j.startsWith("Array<"))return j.replace("Array<","ReadonlyArray<");return j},makeNullable:(j)=>{if(j.includes("| null")||j.includes("| undefined"))return j;return`${j} | null`},makeOptional:(j)=>{if(j.includes("| undefined"))return j;return`${j} | undefined`},makeRequired:(j)=>{return j.replace(/\s*\|\s*null/g,"").replace(/\s*\|\s*undefined/g,"").trim()},promisify:(j)=>{if(j.startsWith("Promise<"))return j;return`Promise<${j}>`},unpromisify:(j)=>{let q=j.match(/^Promise<(.+)>$/);return q?q[1]:j},arrayify:(j)=>{if(j.endsWith("[]")||j.startsWith("Array<"))return j;return`${j}[]`},unarrayify:(j)=>{if(j.endsWith("[]"))return j.slice(0,-2);let q=j.match(/^(?:Readonly)?Array<(.+)>$/);return q?q[1]:j}}; export{Z as Dd,$ as Ed,k as Fd,v as Gd,w as Hd,V as Id,W as Jd,L as Kd,S as Ld,U as Md,H as Nd};