@remotex-labs/xmap
Version:
A library with a sourcemap parser and TypeScript code formatter for the CLI
5 lines (4 loc) • 7 kB
JavaScript
var p={},g="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");g.forEach((u,e)=>{p[u]=e});function S(u){let e=u<0,n="",r=e?(-u<<1)+1:u<<1;do{let t=r&31;r>>>=5,n+=g[t|(r>0?32:0)]}while(r>0);return n}function f(u){return u.map(S).join("")}function h(u){let e=[],n=0,r=0;for(let t=0;t<u.length;t++){let o=p[u[t]];if(o===void 0)throw new Error(`Invalid Base64 character: ${u[t]}`);let i=o&32;if(r+=(o&31)<<n,i)n+=5;else{let s=(r&1)===1,a=r>>1;e.push(s?-a:a),r=n=0}}return e}var d=class u{mapping=[];constructor(e,n=0,r=0){e=e instanceof u?e.mapping:e,Array.isArray(e)?this.decodeMappingArray(e,n,r):this.decodeMappingString(e,n,r)}encode(){return this.encodeMappings(this.mapping)}decode(e,n=0,r=0){e=e instanceof u?e.mapping:e,Array.isArray(e)?this.decodeMappingArray(e,n,r):this.decodeMappingString(e,n,r)}getSegment(e,n,r=0){let t=this.mapping[e-1];if(!t||t.length===0)return null;let o=0,i=t.length-1,s=null;for(;o<=i;){let a=Math.floor((o+i)/2),c=t[a];if(c.generatedColumn<n)o=a+1,s=r===1?c:s;else if(c.generatedColumn>n)i=a-1,s=r===2?c:s;else return c}return s}getOriginalSegment(e,n,r,t=0){let o=null;for(let i of this.mapping){if(!i)continue;let s=0,a=i.length-1;for(;s<=a;){let c=Math.floor((s+a)/2),l=i[c];if(l.sourceIndex<r||l.line<e)s=c+1;else if(l.sourceIndex>r||l.line>e)a=c-1;else if(l.column<n)s=c+1,o=t===1?l:o;else if(l.column>n)a=c-1,o=t===2?l:o;else return l}}return o}initPositionOffsets(e=0,n=0){return{line:0,column:0,nameIndex:e,sourceIndex:n,generatedLine:0,generatedColumn:0}}validateMappingString(e){return/^[a-zA-Z0-9+/,;]+$/.test(e)}validateSegment(e){if(!Number.isFinite(e.line))throw new Error(`Invalid segment: line must be a finite number, received ${e.line}`);if(!Number.isFinite(e.column))throw new Error(`Invalid segment: column must be a finite number, received ${e.column}`);if(e.nameIndex!==null&&!Number.isFinite(e.nameIndex))throw new Error(`Invalid segment: nameIndex must be a number or null, received ${e.nameIndex}`);if(!Number.isFinite(e.sourceIndex))throw new Error(`Invalid segment: sourceIndex must be a finite number, received ${e.sourceIndex}`);if(!Number.isFinite(e.generatedLine))throw new Error(`Invalid segment: generatedLine must be a finite number, received ${e.generatedLine}`);if(!Number.isFinite(e.generatedColumn))throw new Error(`Invalid segment: generatedColumn must be a finite number, received ${e.generatedColumn}`)}encodeSegment(e,n){let{line:r,column:t,generatedColumn:o,nameIndex:i,sourceIndex:s}=n,a=r-1,c=t-1,l=o-1,m=[l-e.generatedColumn,s!==e.sourceIndex?s-e.sourceIndex:0,a-e.line,c-e.column];return i!=null&&(m[4]=i-e.nameIndex,e.nameIndex=i),e.line=a,e.column=c,e.generatedColumn=l,e.sourceIndex=s,f(m)}encodeMappings(e){let n=this.initPositionOffsets();return e.map(r=>r?(n.generatedColumn=0,r.map(o=>this.encodeSegment(n,o)).join(",")):"").join(";")}decodedSegment(e,n){let[r,t,o,i,s]=n;return e.line+=o,e.column+=i,e.nameIndex+=s??0,e.sourceIndex+=t,e.generatedColumn+=r,{line:e.line+1,column:e.column+1,nameIndex:s!==void 0?e.nameIndex:null,sourceIndex:e.sourceIndex,generatedLine:e.generatedLine+1,generatedColumn:e.generatedColumn+1}}decodeMappingString(e,n,r){if(!this.validateMappingString(e))throw new Error("Invalid Mappings string format: the provided string does not conform to expected VLQ format.");let t=e.split(";"),o=this.mapping.length,i=this.initPositionOffsets(n,r);try{t.forEach((s,a)=>{if(!s){this.mapping.push(null);return}i.generatedColumn=0,i.generatedLine=o+a;let c=s.split(",").map(l=>this.decodedSegment(i,h(l)));this.mapping.push(c)})}catch(s){throw new Error(`Error decoding mappings at frame index ${t.length}: ${s.message}`)}}decodeMappingArray(e,n,r){let t=this.mapping.length;if(!Array.isArray(e))throw new Error("Invalid encoded map: expected an array of frames.");try{e.forEach((o,i)=>{if(!o){this.mapping.push(o);return}if(!Array.isArray(o))throw new Error(`Invalid Mappings array format at frame index ${i}: expected an array, received ${typeof o}.`);let s=o.map(a=>(this.validateSegment(a),{...a,nameIndex:typeof a.nameIndex=="number"?a.nameIndex+n:null,sourceIndex:a.sourceIndex+r,generatedLine:a.generatedLine+t}));this.mapping.push(s)})}catch(o){let i=o instanceof Error?o.message:"Unknown error";throw new Error(`Error decoding mappings: ${i}`)}}};var I=class u{file;mappings;sourceRoot;names;sources;sourcesContent;constructor(e,n=null){typeof e=="string"&&(e=JSON.parse(e)),e=e,this.validateSourceMap(e),this.file=e.file??n,this.names=[...e.names??[]],this.sources=[...e.sources??[]],this.sourceRoot=e.sourceRoot??null,this.sourcesContent=e.sourcesContent?[...e.sourcesContent]:[],this.mappings=new d(e.mappings)}getMapObject(){let e={version:3,names:this.names,sources:this.sources,mappings:this.mappings.encode(),sourcesContent:this.sourcesContent};return this.file&&(e.file=this.file),this.sourceRoot&&(e.sourceRoot=this.sourceRoot),e}concat(...e){if(e.length<1)throw new Error("At least one map must be provided for concatenation.");for(let n of e)this.mappings.decode(n.mappings,this.names.length,this.sources.length),this.names.push(...n.names),this.sources.push(...n.sources),this.sourcesContent.push(...n.sourcesContent??[])}concatNewMap(...e){if(e.length<1)throw new Error("At least one map must be provided for concatenation.");let n=new u(this);for(let r of e)n.mappings.decode(r.mappings,n.names.length,n.sources.length),n.names.push(...r.names),n.sources.push(...r.sources),n.sourcesContent.push(...r.sourcesContent??[]);return n}getPositionByOriginal(e,n,r,t=0){let o=r;if(typeof r=="string"&&(o=this.sources.findIndex(s=>s.includes(r))),o<0)return null;let i=this.mappings.getOriginalSegment(e,n,o,t);return i?{name:this.names[i.nameIndex??-1]??null,line:i.line,column:i.column,source:this.sources[i.sourceIndex],sourceRoot:this.sourceRoot,sourceIndex:i.sourceIndex,generatedLine:i.generatedLine,generatedColumn:i.generatedColumn}:null}getPosition(e,n,r=0){let t=this.mappings.getSegment(e,n,r);return t?{name:this.names[t.nameIndex??-1]??null,line:t.line,column:t.column,source:this.sources[t.sourceIndex],sourceRoot:this.sourceRoot,sourceIndex:t.sourceIndex,generatedLine:t.generatedLine,generatedColumn:t.generatedColumn}:null}getPositionWithContent(e,n,r=0){let t=this.getPosition(e,n,r);return t?{...t,sourcesContent:this.sourcesContent[t.sourceIndex]}:null}getPositionWithCode(e,n,r=0,t){let o=this.getPosition(e,n,r);if(!o||!this.sourcesContent[o.sourceIndex])return null;let i=Object.assign({linesAfter:4,linesBefore:3},t),s=this.sourcesContent[o.sourceIndex].split(`
`),a=Math.min((o.line??1)+i.linesAfter,s.length),c=Math.max((o.line??1)-i.linesBefore,0),l=s.slice(c,Math.min(a+1,s.length)).join(`
`);return{...o,code:l,endLine:a,startLine:c}}toString(){return JSON.stringify(this.getMapObject())}validateSourceMap(e){if(!["sources","mappings","names"].every(r=>r in e))throw new Error("Missing required keys in SourceMap.")}};export{I as SourceService,h as decodeVLQ,f as encodeArrayVLQ,S as encodeVLQ};
//# sourceMappingURL=index.js.map