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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@carlwr/textmate-validate

v0.4.1

Published

Validation of TextMate grammars

Readme

textmate-validate

docs

Validation of VSCode TextMate grammars.

Links:

Overview

The following examples use an example grammar grammar.json that can be created with this shell command:

cat >grammar.json <<EOF

{ "patterns": [
    { "match": ".*" },       `# <- a valid regex`
    { "match": ".)" }        `# <- INVALID REGEX!`
  ]
}

EOF

CLI

Prerequisites: none; npx will fetch the package from npm on-the-fly

Example:

npx @carlwr/textmate-validate grammar.json || echo "failed!"
  # failed!

npx @carlwr/textmate-validate --verbose --verbose grammar.json
  # [INFO] number of regexes:
  #   total:    2
  #   valid:    1
  #   invalid:  1
  #
  # [INFO] valid regex:
  #   regex:    .*
  #   path:     patterns[0].match
  #
  # [ERROR] invalid regex:
  #   error:    unmatched close parenthesis
  #   regex:    .)
  #   path:     patterns[1].match
  #
  # [ERROR] validation failed

npx @carlwr/textmate-validate --help  # show help

Library

Prerequisites: npm install @carlwr/textmate-validate

Example:

import { validateGrammar, passed, failed, printResult } from '@carlwr/textmate-validate'

const result = await validateGrammar('grammar.json')

passed(result)  // false
failed(result)  // true

const verbosity = 2
printResult(result, verbosity)  // same as the CLI output above

JSON.stringify(result, null, 2)
  // [
  //   {
  //     "rgx": ".*",
  //     "loc": "patterns[0].match",
  //     "valid": true
  //   },
  //   {
  //     "rgx": ".)",
  //     "loc": "patterns[1].match",
  //     "valid": false,
  //     "err": "unmatched close parenthesis"
  //   }
  // ]

What?

For a given TextMate grammar, the regexes in it are extracted and then validated.

Validation is done by exercising each regex with the Oniguruma regex engine, which is what VSCode uses.

How?

Extracting the regexes:

  • a simple but robust custom parser is used
  • parsing errors are not reported (since it is beyond the scope of this package to reliably detect them)
  • ...the parsing strategy can be summarized as parse what we can, ignore anything else

Validating the extracted regexes:

  • each regex is exercised with the Oniguruma engine
  • if Oniguruma doesn't complain, the regex is considered valid
  • if Oniguruma complains, the regex is considered invalid
  • the error strings this package reports are those produced by Oniguruma

Oniguruma engine:

  • the package uses the Oniguruma WASM binary of vscode-oniguruma
  • this is a design decision and comes with benefits and drawbacks:
    • benefit: validation accuracy:
      • validation is done with the same library (e.g. compiled with the same compilation flags) that will be used by VSCode when the grammar is used at later points
    • drawback: fragility:
      • this package must use heuristics to dynamically locate the path of the onig.wasm file that vscode-oniguruma includes
      • the heuristics used are well tested and e.g. symlinked paths will be followed - but could likely fail for less common setups

Intended use

The package is intended to be used as a validation check for generated or hand-written TextMate grammars for VSCode, typically as part of a build script, CI pipeline, prepublish package.json lifecycle hook, etc. It complements the following that are also useful in a similar context:

  • schema validation
    • using e.g. the tmlanguage.json schema - which VSCode does not officially conform to, but is likely yet useful
    • note that schema validation serves the purpose of checking the structure of the grammar, something this package does not do
  • testing