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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@anche/semantic-tokens-utilities

v0.0.2

Published

Semantic Tokens Utilities

Readme

semantic-tokens-utilities

This package exposes several utilities that help with Semantic Tokens.

interface Position {
    line: number;
    start: number;
    length: number;
}

interface Token {
    type: string;
    modifiers: string[];
    language?: string;
}

interface SemanticToken extends Position, Token {}

Usage

First you should/could initialize the match helper with the scope fallbacks before you use the match utility from this package. Those fallbacks are TextMate scopes. (https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide).

(You could call the initialize module before starting your app)

import { initialize } from '@anche/semantic-tokens-utilities';

export type FallbackRegister = (tokenString: string, fallbackScopes: string[]) => void;

(() => {
    initialize((registerTokenFallback: FallbackRegister) => {
        // Define your fallbacks:

        registerTokenFallback('namespace', 'entity.name.namespace');
    });
    App.start();
})();
// If you want to have the same fallbacks as VSCode
import { initialize, Presets } from '@anche/semantic-tokens-utilities';

initialize(Presets.vscode);

Parser

The parser transforms a string into a CodeDocument and returns an array of Semantic Tokens

(This can only be executed on a node server)

interface CodeDocument {
    getTextAtPosition(position: Position): string;
    getLines(): string[];
    getRaw(): string;
}

type SemanticTokensParserResult = <T extends Position>{
    code: CodeDocument;
    tokens: T[];
}

import { parser } from '@anche/semantic-tokens-utilities';

const rawCode = `const semanticTokens = () => {}`;

const parserResult = parser({ code: rawCode, language: 'tsx' })
;

const firstNode = parserResult.tokens[0];
// `semanticTokens` is a Semantic Token:
// {
//     line: 0,
//     start: 6,
//     length: 13,
//     type: 'function',
//     modifiers: ['declaration', 'readonly'],
//     language: undefined
// }

Matcher

interface Token {
    type: string;
    modifiers: string[];
    language?: string;
}

interface TokenFallback {
    token: Token;
    fallbackScopes: string[];
}

interface TokenWinner<T extends Token> {
    token: T;
    score: number;
}

interface Matcher {
    // returns a Semantic Token or TextMate fallback scopes
    matchToken: <T extends Token>(
        token: string | Token,
        semanticTokens: T[],
    ) =>
        | {
              token?: T;
              fallbackScopes: string[];
          }
        | undefined;

    //  will return TextMate fallback scopes defined during initialization
    getFallback: (token: Token) => TokenFallback | undefined;

    // will return a matching Semantic Token or undefined
    getMatch: <T extends Token>(token: Token, semanticTokens: T[]) => TokenWinner<T> | undefined;
}

// Defined Semantic Tokens
const semanticTokens = [
    { scope: 'member.defaultLibrary', color: '#000000' }
    { scope: 'variable.declaration.readonly', color: '#ff00ff'
    { scope: 'function', color: '#00ff00' }
];

const code = `const semanticTokens = () => {};`;

// after parsing you have the node for `semanticTokens`:
const firstNode = parserResult.tokens[0];
// {
//     line: 0,
//     start: 6,
//     length: 13,
//     type: 'function',
//     modifiers: ['declaration', 'readonly'],
//     language: undefined
// }

import { Matcher } from '@anche/semantic-tokens-utilities';

// semanticTokens[2] will return
const rule = Matcher.matchToken(firstNode, semanticTokens);