UNPKG

@stacksjs/dtsx

Version:

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

6 lines (5 loc) 3.89 kB
// @bun var D=["builtin","external","internal","parent","sibling","index","type","unknown"];function L(q){let W=q.match(/^(import\s+)(type\s+)?(.+?)\s+from\s+['"]([^'"]+)['"]/);if(!W){let X=q.match(/^import\s+['"]([^'"]+)['"]/);if(X)return{statement:q,source:X[1],isTypeOnly:!1,group:F(X[1]),specifiers:[]};return null}let[,,Z,$,w]=W,k=!!Z,N,A,z=[],J=$.trim(),Y=J.match(/^\*\s+as\s+(\w+)$/);if(Y)A=Y[1];else if(J.includes("{")){let X=J.match(/^(\w+)\s*,/);if(X)N=X[1];let v=J.match(/\{([^}]+)\}/);if(v){let K=v[1].split(",").map((C)=>C.trim()).filter(Boolean);z.push(...K)}}else if(/^\w+$/.test(J))N=J;else{let X=J.match(/\{([^}]+)\}/);if(X){let v=X[1].split(",").map((K)=>K.trim()).filter(Boolean);z.push(...v)}}return{statement:q,source:w,isTypeOnly:k,group:F(w,k),specifiers:z,defaultImport:N,namespaceImport:A}}function F(q,W=!1){if(W)return"type";if(q.startsWith("node:")||R(q))return"builtin";if(q.startsWith("./"))return"sibling";if(q.startsWith("../"))return"parent";if(q==="."||q==="./index")return"index";if(q.startsWith("@/")||q.startsWith("~/")||q.startsWith("#"))return"internal";if(q.startsWith("@")||/^[a-z]/.test(q))return"external";return"unknown"}var _=new Set(["assert","buffer","child_process","cluster","console","constants","crypto","dgram","dns","domain","events","fs","http","https","module","net","os","path","perf_hooks","process","punycode","querystring","readline","repl","stream","string_decoder","sys","timers","tls","tty","url","util","v8","vm","wasi","worker_threads","zlib"]);function R(q){return _.has(q)}function S(q,W={}){let{order:Z=[],groupByType:$=!0,groups:w=D,alphabetize:k=!0,caseInsensitive:N=!0,typeImportsLast:A=!1,separateTypeImports:z=!1}=W,J=[];for(let j of q){let H=L(j);if(H)J.push(H);else J.push({statement:j,source:"",isTypeOnly:!1,group:"unknown",specifiers:[]})}let Y=Z.length>0?Z.map((j)=>{if(j.startsWith("^"))try{return{regex:new RegExp(j),prefix:j.slice(1)}}catch{return{prefix:j.slice(1)}}return{prefix:j}}):[],X=(j)=>{for(let H=0;H<Y.length;H++){let Q=Y[H];if(Q.regex){if(Q.regex.test(j))return H}else if(j.startsWith(Q.prefix)||j.includes(`/${Q.prefix}`))return H}return Y.length},v=new Map,K=(j)=>{let H=v.get(j);if(H!==void 0)return H;let Q=w.indexOf(j),V=Q>=0?Q:w.length;return v.set(j,V),V},C=(j,H)=>{if(Y.length>0){let Q=X(j.source),V=X(H.source);if(Q!==V)return Q-V}if($){let Q=K(j.group),V=K(H.group);if(Q!==V)return Q-V}if(A&&j.isTypeOnly!==H.isTypeOnly)return j.isTypeOnly?1:-1;if(k){let Q=N?j.source.toLowerCase():j.source,V=N?H.source.toLowerCase():H.source;return Q.localeCompare(V)}return 0};if(J.sort(C),$){let j=[],H=null;for(let Q of J){if(H!==null&&Q.group!==H){let V=w.indexOf(H),B=w.indexOf(Q.group);if(V>=0&&B>=0&&V!==B)j.push("")}j.push(Q.statement),H=Q.group}return j}return J.map((j)=>j.statement)}function U(q,W={}){let Z=q.split(` `),$=[],w=[],k=-1;for(let J=0;J<Z.length;J++){let Y=Z[J].trim();if(Y.startsWith("import "))$.push(Z[J]),w.push(J),k=J;else if($.length>0&&Y==="")continue;else if($.length>0&&!Y.startsWith("import "))break}if($.length===0)return q;let N=S($,W),A=Z.slice(0,w[0]),z=Z.slice(k+1);while(z.length>0&&z[0].trim()==="")z.shift();return[...A,...N,"",...z].join(` `)}function P(q={}){return{sort:(W)=>S(W,q),sortContent:(W)=>U(W,q),parse:L,detectGroup:F}}var T={default:{groupByType:!0,groups:D,alphabetize:!0,caseInsensitive:!0},node:{order:["node:"],groupByType:!0,groups:["builtin","external","internal","parent","sibling","index"],alphabetize:!0},bun:{order:["bun","node:"],groupByType:!0,groups:["builtin","external","internal","parent","sibling","index"],alphabetize:!0},typeSeparated:{groupByType:!0,groups:["builtin","external","internal","parent","sibling","index","type"],separateTypeImports:!0,alphabetize:!0},alphabetical:{groupByType:!1,alphabetize:!0,caseInsensitive:!0},none:{groupByType:!1,alphabetize:!1}}; export{D as O,L as P,F as Q,S as R,U as S,P as T,T as U};