UNPKG

@remotex-labs/xmap

Version:

A library with a sourcemap parser and TypeScript code formatter for the CLI

5 lines (4 loc) 7.53 kB
"use strict";var m=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var M=(c,e)=>{for(var n in e)m(c,n,{get:e[n],enumerable:!0})},C=(c,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of b(e))!v.call(c,t)&&t!==n&&m(c,t,{get:()=>e[t],enumerable:!(r=y(e,t))||r.enumerable});return c};var N=c=>C(m({},"__esModule",{value:!0}),c);var w={};M(w,{SourceService:()=>f,decodeVLQ:()=>g,encodeArrayVLQ:()=>p,encodeVLQ:()=>x});module.exports=N(w);var I={},S="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");S.forEach((c,e)=>{I[c]=e});function x(c){let e=c<0,n="",r=e?(-c<<1)+1:c<<1;do{let t=r&31;r>>>=5,n+=S[t|(r>0?32:0)]}while(r>0);return n}function p(c){return c.map(x).join("")}function g(c){let e=[],n=0,r=0;for(let t=0;t<c.length;t++){let o=I[c[t]];if(o===void 0)throw new Error(`Invalid Base64 character: ${c[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 c{mapping=[];constructor(e,n=0,r=0){e=e instanceof c?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 c?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),u=t[a];if(u.generatedColumn<n)o=a+1,s=r===1?u:s;else if(u.generatedColumn>n)i=a-1,s=r===2?u:s;else return u}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 u=Math.floor((s+a)/2),l=i[u];if(l.sourceIndex<r||l.line<e)s=u+1;else if(l.sourceIndex>r||l.line>e)a=u-1;else if(l.column<n)s=u+1,o=t===1?l:o;else if(l.column>n)a=u-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,u=t-1,l=o-1,h=[l-e.generatedColumn,s!==e.sourceIndex?s-e.sourceIndex:0,a-e.line,u-e.column];return i!=null&&(h[4]=i-e.nameIndex,e.nameIndex=i),e.line=a,e.column=u,e.generatedColumn=l,e.sourceIndex=s,p(h)}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 u=s.split(",").map(l=>this.decodedSegment(i,g(l)));this.mapping.push(u)})}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 f=class c{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 c(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),u=Math.max((o.line??1)-i.linesBefore,0),l=s.slice(u,Math.min(a+1,s.length)).join(` `);return{...o,code:l,endLine:a,startLine:u}}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.")}};0&&(module.exports={SourceService,decodeVLQ,encodeArrayVLQ,encodeVLQ}); //# sourceMappingURL=index.js.map