UNPKG

@remotex-labs/xmap

Version:

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

3 lines (2 loc) 6.54 kB
"use strict";var u=Object.create;var h=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var C=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty;var f=(a,e)=>{for(var t in e)h(a,t,{get:e[t],enumerable:!0})},m=(a,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of x(e))!K.call(a,i)&&i!==t&&h(a,i,{get:()=>e[i],enumerable:!(s=y(e,i))||s.enumerable});return a};var b=(a,e,t)=>(t=a!=null?u(C(a)):{},m(e||!a||!a.__esModule?h(t,"default",{value:a,enumerable:!0}):t,a)),v=a=>m(h({},"__esModule",{value:!0}),a);var T={};f(T,{CodeHighlighter:()=>d,Colors:()=>S,highlightCode:()=>w});module.exports=v(T);var r=b(require("typescript"),1),l=require("typescript"),S=(o=>(o.reset="\x1B[0m",o.gray="\x1B[38;5;243m",o.darkGray="\x1B[38;5;238m",o.lightCoral="\x1B[38;5;203m",o.lightOrange="\x1B[38;5;215m",o.oliveGreen="\x1B[38;5;149m",o.burntOrange="\x1B[38;5;208m",o.lightGoldenrodYellow="\x1B[38;5;221m",o.lightYellow="\x1B[38;5;230m",o.canaryYellow="\x1B[38;5;227m",o.deepOrange="\x1B[38;5;166m",o.lightGray="\x1B[38;5;252m",o.brightPink="\x1B[38;5;197m",o))(S||{}),E={enumColor:"\x1B[38;5;208m",typeColor:"\x1B[38;5;221m",classColor:"\x1B[38;5;215m",stringColor:"\x1B[38;5;149m",keywordColor:"\x1B[38;5;203m",commentColor:"\x1B[38;5;238m",functionColor:"\x1B[38;5;215m",variableColor:"\x1B[38;5;208m",interfaceColor:"\x1B[38;5;221m",parameterColor:"\x1B[38;5;166m",getAccessorColor:"\x1B[38;5;230m",numericLiteralColor:"\x1B[38;5;252m",methodSignatureColor:"\x1B[38;5;208m",regularExpressionColor:"\x1B[38;5;149m",propertyAssignmentColor:"\x1B[38;5;227m",propertyAccessExpressionColor:"\x1B[38;5;230m",expressionWithTypeArgumentsColor:"\x1B[38;5;215m"},d=class{constructor(e,t,s){this.sourceFile=e;this.code=t;this.schema=s}segments=new Map;parseNode(e){this.processComments(e),this.processKeywords(e),this.processNode(e)}highlight(){let e=0,t,s=[];return Array.from(this.segments.values()).sort((n,c)=>n.start-c.start||n.end-c.end).forEach(n=>{if(t&&n.start<t.end){let c=s.pop();if(!c)return;let g=this.getSegmentSource(n.start,n.end),p=`${n.color}${g}${t.color}`;s.push(c.replace(g,p));return}s.push(this.getSegmentSource(e,n.start)),s.push(`${n.color}${this.getSegmentSource(n.start,n.end)}${n.reset}`),e=n.end,t=n}),s.join("")+this.getSegmentSource(e)}getSegmentSource(e,t){return this.code.slice(e,t)}addSegment(e,t,s,i="\x1B[0m"){let n=`${e}-${t}`;this.segments.set(n,{start:e,end:t,color:s,reset:i})}processComments(e){[...r.getTrailingCommentRanges(this.sourceFile.getFullText(),e.getFullStart())||[],...r.getLeadingCommentRanges(this.sourceFile.getFullText(),e.getFullStart())||[]].forEach(s=>this.addSegment(s.pos,s.end,this.schema.commentColor))}processKeywords(e){if([l.SyntaxKind.NullKeyword,l.SyntaxKind.VoidKeyword,l.SyntaxKind.StringKeyword,l.SyntaxKind.NumberKeyword,l.SyntaxKind.BooleanKeyword,l.SyntaxKind.UndefinedKeyword].includes(e.kind))return this.addSegment(e.getStart(),e.getEnd(),this.schema.typeColor);e&&e.kind>=r.SyntaxKind.FirstKeyword&&e.kind<=r.SyntaxKind.LastKeyword&&this.addSegment(e.getStart(),e.getEnd(),this.schema.keywordColor)}processIdentifier(e){let t=e.getEnd(),s=e.getStart();switch(e.parent.kind){case r.SyntaxKind.EnumMember:return this.addSegment(s,t,this.schema.enumColor);case r.SyntaxKind.CallExpression:case r.SyntaxKind.EnumDeclaration:case r.SyntaxKind.PropertySignature:case r.SyntaxKind.ModuleDeclaration:return this.addSegment(s,t,this.schema.variableColor);case r.SyntaxKind.InterfaceDeclaration:return this.addSegment(s,t,this.schema.interfaceColor);case r.SyntaxKind.GetAccessor:return this.addSegment(s,t,this.schema.getAccessorColor);case r.SyntaxKind.PropertyAssignment:return this.addSegment(s,t,this.schema.propertyAssignmentColor);case r.SyntaxKind.MethodSignature:return this.addSegment(s,t,this.schema.methodSignatureColor);case r.SyntaxKind.MethodDeclaration:case r.SyntaxKind.FunctionDeclaration:return this.addSegment(s,t,this.schema.functionColor);case r.SyntaxKind.ClassDeclaration:return this.addSegment(s,t,this.schema.classColor);case r.SyntaxKind.Parameter:return this.addSegment(s,t,this.schema.parameterColor);case r.SyntaxKind.VariableDeclaration:return this.addSegment(s,t,this.schema.variableColor);case r.SyntaxKind.PropertyDeclaration:return this.addSegment(s,t,this.schema.variableColor);case r.SyntaxKind.PropertyAccessExpression:return e.parent.getChildAt(0).getText()===e.getText()?this.addSegment(s,t,this.schema.variableColor):this.addSegment(s,t,this.schema.propertyAccessExpressionColor);case r.SyntaxKind.ExpressionWithTypeArguments:return this.addSegment(s,t,this.schema.expressionWithTypeArgumentsColor);case r.SyntaxKind.BreakStatement:case r.SyntaxKind.ShorthandPropertyAssignment:case r.SyntaxKind.BindingElement:return this.addSegment(s,t,this.schema.variableColor);case r.SyntaxKind.BinaryExpression:case r.SyntaxKind.SwitchStatement:case r.SyntaxKind.TemplateSpan:return this.addSegment(s,t,this.schema.variableColor);case r.SyntaxKind.TypeReference:case r.SyntaxKind.TypeAliasDeclaration:return this.addSegment(s,t,this.schema.typeColor);case r.SyntaxKind.NewExpression:return this.addSegment(s,t,this.schema.variableColor)}}processTemplateExpression(e){let t=e.head.getStart(),s=e.head.getEnd();this.addSegment(t,s,this.schema.stringColor),e.templateSpans.forEach(i=>{let n=i.literal.getStart(),c=i.literal.getEnd();this.addSegment(n,c,this.schema.stringColor)})}processNode(e){let t=e.getStart(),s=e.getEnd();switch(e.kind){case r.SyntaxKind.TypeParameter:return this.addSegment(t,t+e.name.text.length,this.schema.typeColor);case r.SyntaxKind.TypeReference:return this.addSegment(t,s,this.schema.typeColor);case r.SyntaxKind.StringLiteral:case r.SyntaxKind.NoSubstitutionTemplateLiteral:return this.addSegment(t,s,this.schema.stringColor);case r.SyntaxKind.RegularExpressionLiteral:return this.addSegment(t,s,this.schema.regularExpressionColor);case r.SyntaxKind.TemplateExpression:return this.processTemplateExpression(e);case r.SyntaxKind.Identifier:return this.processIdentifier(e);case r.SyntaxKind.BigIntLiteral:case r.SyntaxKind.NumericLiteral:return this.addSegment(t,s,this.schema.numericLiteralColor)}}};function w(a,e={}){let t=r.createSourceFile("temp.ts",a,r.ScriptTarget.Latest,!0,r.ScriptKind.TS),s=new d(t,a,Object.assign(E,e));function i(n){s.parseNode(n);for(let c=0;c<n.getChildCount();c++)i(n.getChildAt(c))}return r.forEachChild(t,i),s.highlight()}0&&(module.exports={CodeHighlighter,Colors,highlightCode}); //# sourceMappingURL=highlighter.component.js.map