UNPKG

@stacksjs/dtsx

Version:

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

11 lines (10 loc) 5.28 kB
// @bun var S={PARSE_ERROR:"PARSE_ERROR",SYNTAX_ERROR:"SYNTAX_ERROR",FILE_NOT_FOUND:"FILE_NOT_FOUND",FILE_READ_ERROR:"FILE_READ_ERROR",FILE_WRITE_ERROR:"FILE_WRITE_ERROR",TYPE_INFERENCE_ERROR:"TYPE_INFERENCE_ERROR",UNRESOLVED_TYPE:"UNRESOLVED_TYPE",EXTRACTION_ERROR:"EXTRACTION_ERROR",PROCESSING_ERROR:"PROCESSING_ERROR",VALIDATION_ERROR:"VALIDATION_ERROR",INVALID_DECLARATION:"INVALID_DECLARATION",CONFIG_ERROR:"CONFIG_ERROR",INVALID_ENTRYPOINT:"INVALID_ENTRYPOINT",CIRCULAR_DEPENDENCY:"CIRCULAR_DEPENDENCY",TIMEOUT_ERROR:"TIMEOUT_ERROR",NOT_SUPPORTED:"NOT_SUPPORTED",UNKNOWN_ERROR:"UNKNOWN_ERROR"};class H extends Error{code;context;constructor(j,q="UNKNOWN_ERROR",v){super(j);if(this.name="DtsxError",this.code=q,this.context=v,Error.captureStackTrace)Error.captureStackTrace(this,this.constructor)}toString(){let j=`${this.name} [${this.code}]: ${this.message}`;if(this.context)j+=` Context: ${JSON.stringify(this.context,null,2)}`;return j}toJSON(){return{name:this.name,code:this.code,message:this.message,context:this.context,stack:this.stack}}}class W extends H{filePath;line;column;constructor(j,q,v){super(j,"PARSE_ERROR",{filePath:q,line:v?.line,column:v?.column});if(this.name="ParseError",this.filePath=q,this.line=v?.line,this.column=v?.column,v?.cause)this.cause=v.cause}get locationString(){if(this.line!==void 0&&this.column!==void 0)return`${this.filePath}:${this.line}:${this.column}`;return this.line!==void 0?`${this.filePath}:${this.line}`:this.filePath}}class _ extends H{filePath;declarationKind;constructor(j,q,v,z){super(j,"EXTRACTION_ERROR",{filePath:q,declarationKind:v});if(this.name="ExtractionError",this.filePath=q,this.declarationKind=v,z)this.cause=z}}class $ extends H{declarationName;constructor(j,q,v){super(j,"PROCESSING_ERROR",{declarationName:q});if(this.name="ProcessingError",this.declarationName=q,v)this.cause=v}}class X extends H{filePath;operation;constructor(j,q,v,z){super(j,v==="read"?"FILE_READ_ERROR":"FILE_WRITE_ERROR",{filePath:q,operation:v});if(this.name="FileError",this.filePath=q,this.operation=v,z)this.cause=z}}class Y extends H{configPath;invalidKey;constructor(j,q){super(j,"CONFIG_ERROR",{configPath:q?.configPath,invalidKey:q?.invalidKey});if(this.name="ConfigError",this.configPath=q?.configPath,this.invalidKey=q?.invalidKey,q?.cause)this.cause=q.cause}}class A extends H{cycle;constructor(j){super(`Circular dependency detected: ${j.join(" -> ")}`,"CIRCULAR_DEPENDENCY",{cycle:j});this.name="CircularDependencyError",this.cycle=j}}function k(j){return j instanceof H}function w(j){return j instanceof W}function F(j){return j instanceof X}function O(j){return j instanceof Y}function K(j,q="UNKNOWN_ERROR",v){if(j instanceof H)return j;let z=v||(j instanceof Error?j.message:String(j)),B=new H(z,q);if(j instanceof Error)B.cause=j;return B}function L(j,q){let v=1,z=1,B=0;for(let G of j){if(B>=q)break;if(G===` `)v++,z=1;else z++;B++}return{line:v,column:z,offset:q}}function M(j,q,v,z){let B=j.split(` `),G=q.line-1,J=[];if(z)J.push(`${z}:${q.line}:${q.column}`);else J.push(`Line ${q.line}, Column ${q.column}`);J.push(`Error: ${v}`),J.push("");let T=Math.max(0,G-1),Q=Math.min(B.length-1,G+1);for(let R=T;R<=Q;R++){let U=(R+1).toString().padStart(4," "),V=R===G?">":" ";if(J.push(`${V} ${U} | ${B[R]}`),R===G){let Z=" ".repeat(8+q.column-1);J.push(`${Z}^`)}}return J.join(` `)}function N(j,q,v){let z={file:q,message:"Unknown error",code:S.UNKNOWN_ERROR};if(j instanceof Error){z.message=j.message,z.stack=j.stack;let B=j.message.match(/\((\d+),(\d+)\)/);if(B)z.location={line:Number.parseInt(B[1],10),column:Number.parseInt(B[2],10)};if(j.message.includes("Cannot find")||j.message.includes("not found"))z.code=S.FILE_NOT_FOUND,z.suggestion="Check that the file path is correct and the file exists.";else if(j.message.includes("syntax")||j.message.includes("Unexpected token"))z.code=S.SYNTAX_ERROR,z.suggestion="Check for syntax errors in your TypeScript code.";else if(j.message.includes("type")&&j.message.includes("cannot"))z.code=S.TYPE_INFERENCE_ERROR,z.suggestion="Add explicit type annotations to help with type inference.";else if(j.message.includes("parse")||j.message.includes("Parse"))z.code=S.PARSE_ERROR,z.suggestion="The file contains invalid TypeScript syntax."}else if(typeof j==="string")z.message=j;return z}function y(j,q){let v=[],z=j.file;if(j.location)z+=`:${j.location.line}:${j.location.column}`;if(j.code)z+=` [${j.code}]`;if(v.push(z),v.push(` Error: ${j.message}`),q&&j.location){v.push("");let B=q.split(` `),G=j.location.line-1;if(G>=0&&G<B.length){let J=Math.max(0,G-1),T=Math.min(B.length-1,G+1);for(let Q=J;Q<=T;Q++){let R=(Q+1).toString().padStart(4," "),U=Q===G?">":" ";if(v.push(` ${U} ${R} | ${B[Q]}`),Q===G&&j.location.column>0){let V=" ".repeat(10+j.location.column-1);v.push(` ${V}^`)}}}}if(j.suggestion)v.push(""),v.push(` Suggestion: ${j.suggestion}`);return v.join(` `)}function I(j){if(j.length===0)return"No errors";let q=new Map;for(let z of j){let B=z.code||"UNKNOWN";q.set(B,(q.get(B)||0)+1)}let v=[`${j.length} error(s) found:`];for(let[z,B]of q.entries())v.push(` - ${z}: ${B}`);return v.join(` `)} export{S as ld,H as md,W as nd,_ as od,$ as pd,X as qd,Y as rd,A as sd,k as td,w as ud,F as vd,O as wd,K as xd,L as yd,M as zd,N as Ad,y as Bd,I as Cd};