npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@remotex-labs/xmap

v2.0.4

Published

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

Downloads

480

Readme

xMap Description

xMap is a library designed to help with sourcemap parsing and TypeScript code formatting for the CLI.
It includes components for parsing error stack traces, formatting code, and providing syntax highlighting, as well as a service for handling source maps.

Installation

To install the package, use npm or yarn:

npm install @remotex-labs/xmap

or

yarn add @remotex-labs/xmap

Usage SourceService

A TypeScript service for validating and processing source maps. The SourceService class provides functionality for parsing and manipulating source maps, including retrieving position mappings, concatenating source maps, and getting code snippets based on mappings.

Importing the SourceService

You can import the SourceService class in your TypeScript project as follows:

import { SourceService } from '@remotex-labs/xmap'

Creating an Instance of SourceService

You can create an instance of SourceService using either a SourceMapInterface object, a JSON string representing the source map, or an existing SourceService instance. Example:

const sourceMapJSON = `
{
  "version": 3,
  "sources": ["../src/core/core.component.ts", "../src/index.ts"],
  "sourceRoot": "https://github.com/remotex-lab/xmap/tree/test/",
  "sourcesContent": ["export class CoreModule {\\r\\n    private name: string;\\r\\n\\r\\n    constructor(name: string) {\\r\\n        this.name = name;\\r\\n    }\\r\\n\\r\\n    public greet(): string {\\r\\n        return \`Hello from \${ this.name }!\`;\\r\\n    }\\r\\n}", "import { CoreModule } from '@core/core.component';\\r\\n\\r\\nconst coreInstance = new CoreModule('Core Module');\\r\\n\\r\\nconsole.log(coreInstance.greet());"],
  "mappings": "aAAO,IAAMA,EAAN,KAAiB,CACZ,KAER,YAAYC,EAAc,CACtB,KAAK,KAAOA,CAChB,CAEO,OAAgB,CACnB,MAAO,cAAc,KAAK,IAAI,GAClC,CACJ,ECRA,IAAMC,EAAe,IAAIC,EAAW,aAAa,EAEjD,QAAQ,IAAID,EAAa,MAAM,CAAC",
  "names": ["CoreModule", "name", "coreInstance", "CoreModule"]
}
`;
const sourceService = new SourceService(sourceMapJSON, 'bundle.js');
console.log(sourceService);

Retrieving Position Information

You can retrieve position information based on the original source line and column using the getPositionByOriginal method. Example:

const position = sourceService.getPositionByOriginal(3, 7, 'index.ts');
console.log(position);

Getting Code Snippets

To retrieve the position and a code snippet from the original source based on the given generated code position, you can use the getPositionWithCode method. Example:

const positionWithCode = sourceService.getPositionWithCode(1, 104, Bias.UPPER_BOUND, { linesBefore: 2, linesAfter: 2 });
console.log(positionWithCode);

Converting to JSON String

You can convert the current source map object to a JSON string using the toString method. Example:

console.log(sourceService.toString());

Concatenating Source Maps

To concatenate one or more source maps to the current source map, you can use the concat method.

const anotherSourceMap = {
    version: 3,
    file: "another-bundle.js",
    sources: ["bar.ts"],
    names: [],
    mappings: "AAAA"
};

sourceService.concat(anotherSourceMap);
console.log(sourceService.sources); // Updated source paths

Parsing Error Stack Traces

The parseErrorStack function parses an error stack trace and returns an array of stack entries. Each entry contains information about the function call, file, line number, column number, and if applicable, details about the eval context. Example:

import { parseErrorStack } from '@remotex-labs/xmap';

// Example stack trace string
const stackTrace = `
Error: Example error
    at Object.<anonymous> (/path/to/file.js:10:15)
    at Module._compile (node:internal/modules/cjs/loader:1217:14)
    at node:internal/modules/cjs/loader:1308:14
    at node:internal/modules/cjs/loader:1425:5
    at node:internal/modules/cjs/loader:1425:5
    at node:internal/modules/cjs/loader:1483:3
    at node:internal/modules/cjs/loader:1700:8
    at node:internal/modules/cjs/loader:1760:3
    at /path/to/file.js:10:15
    at Object.<anonymous> (/path/to/file.js:10:15)
    at eval (eval at <anonymous> (/path/to/file.js:10:15), <anonymous>:1:1)
`;

// Parsing the stack trace
const parsedStack = parseErrorStack(stackTrace);
console.log(parsedStack);

highlightCode

Highlights the provided TypeScript code based on a specified highlighting scheme. This function creates a source file from the provided code string, walks through its nodes, and applies syntax highlighting according to the given schema.

Example:

import { highlightCode } from '@remotex-labs/xmap';

// TypeScript code to be highlighted
const codeToHighlight = `
function greet(name: string): string {
    return 'Hello, ' + name;
}
`;

// Optional custom highlight scheme
const customScheme = {
    keywordColor: '\x1b[36m', // Blue
    stringColor: '\x1b[32m',  // Green
    numberColor: '\x1b[31m'  // Red
};

// Highlight the code
const highlightedCode = highlightCode(codeToHighlight);
const highlightedCode2 = highlightCode(codeToHighlight, customScheme);
const highlightedCode3 = highlightCode(codeToHighlight);

// Output the highlighted code
console.log(highlightedCode, highlightedCode2, highlightedCode3);

If you provide a customScheme, it will override the default highlighting scheme. This means you won't need to pass it each time you call highlightCode.

image

formatCode

The formatCode function formats a given code snippet, adding line numbers with customizable padding and enabling specific actions for particular lines. This utility is useful for displaying code snippets in a user-friendly manner, particularly in documentation or debugging scenarios.

Example:

import { formatCode, highlightCode } from '@remotex-labs/xmap';

const code = `
function greet(name: string) {
    console.log('Hello, ' + name);
}

greet('World');
`;

const formattedCode = formatCode(highlightCode(code), {
    padding: 8,
    startLine: 5,
    action: {
        triggerLine: 6,
        callback: (lineString, padding, lineNumber) => {
            return `*** Custom formatting for line ${lineNumber} ***\n${lineString}`;
        }
    }
});

console.log(formattedCode);

image

formatErrorCode

The formatErrorCode function formats a code snippet around a specified error location, applying special highlighting to indicate where the error occurred. This function is particularly useful for debugging and error reporting, as it helps to visually identify issues in the source code.

import { highlightCode, SourceService, formatErrorCode } from '@remotex-labs/xmap';

const sourceMapJSON = `
{
  "version": 3,
  "sources": ["../src/core/core.component.ts", "../src/index.ts"],
  "sourceRoot": "https://github.com/remotex-lab/xmap/tree/test/",
  "sourcesContent": ["export class CoreModule {\\r\\n    private name: string;\\r\\n\\r\\n    constructor(name: string) {\\r\\n        this.name = name;\\r\\n    }\\r\\n\\r\\n    public greet(): string {\\r\\n        return \`Hello from \${ this.name }!\`;\\r\\n    }\\r\\n}", "import { CoreModule } from '@core/core.component';\\r\\n\\r\\nconst coreInstance = new CoreModule('Core Module');\\r\\n\\r\\nconsole.log(coreInstance.greet());"],
  "mappings": "aAAO,IAAMA,EAAN,KAAiB,CACZ,KAER,YAAYC,EAAc,CACtB,KAAK,KAAOA,CAChB,CAEO,OAAgB,CACnB,MAAO,cAAc,KAAK,IAAI,GAClC,CACJ,ECRA,IAAMC,EAAe,IAAIC,EAAW,aAAa,EAEjD,QAAQ,IAAID,EAAa,MAAM,CAAC",
  "names": ["CoreModule", "name", "coreInstance", "CoreModule"]
}
`;
const sourceService = new SourceService(sourceMapJSON, 'bundle.js');
const error = sourceService.getPositionWithCode(1, 91);

const ansiOption = {
    color: '\x1b[38;5;160m', // Color for error marker
    reset: '\x1b[0m'         // Reset color
};

if (error) {
    error.code = highlightCode(error.code);
    console.log(formatErrorCode(error, ansiOption));
}

image