UNPKG

@remotex-labs/xmap

Version:

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

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