🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

lezer-template-json

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lezer-template-json

A Lezer grammar for parsing template JSON

latest
Source
npmnpm
Version
0.1.4
Version published
Maintainers
1
Created
Source

lezer-template-json

npm npm bundle size jsDelivr hits

A Lezer grammar for parsing template JSON (JSON with {{variable}} holes) with incremental parsing support and TypeScript definitions.

Install

npm i lezer-template-json

Features

  • Supports parsing template JSON with {{variable}} syntax

Usage

Basic

import { parser } from "lezer-template-json";

const tree = parser.parse(`{
  "name": {{name}},
  "age": {{age}},
  "nested": {
    "value": {{value}}
  }
}`);
console.log(tree.toString());

With CodeMirror

import { parser, templateJsonHighlighting } from "lezer-template-json";
import { LRLanguage } from "@codemirror/language";
import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";

const templateJsonLanguage = LRLanguage.define({
  parser,
  languageData: { name: "template-json" },
});

const highlightStyle = HighlightStyle.define([templateJsonHighlighting]);
const extensions = [templateJsonLanguage, syntaxHighlighting(highlightStyle)];

Tree Navigation

import { parser } from "lezer-template-json";
import * as terms from "lezer-template-json";

const tree = parser.parse(`{
  "name": {{name}},
  "age": {{age}}
}`);
const cursor = tree.cursor();
while (cursor.next()) {
  if (cursor.type.id === terms.Variable) {
    console.log(`Found variable: ${cursor.node.name}`);
  }
}

Error Handling

import { parser } from "lezer-template-json";

function parseWithErrors(pattern: string) {
  const tree = parser.parse(pattern);
  const errors: any[] = [];

  tree.cursor().iterate((node) => {
    if (node.type.isError) {
      errors.push({
        from: node.from,
        to: node.to,
        message: `Syntax error at ${node.from}-${node.to}`,
      });
    }
  });

  return { tree, errors };
}

API

Exports

  • parser - Lezer parser instance
  • templateJsonHighlighting - CodeMirror syntax highlighting
  • Grammar terms - Node type constants for tree navigation

Types

parser.parse(input: string, fragments?: TreeFragment[], ranges?: {from: number, to: number}[]): Tree

Development

git clone https://github.com/Sec-ant/lezer-template-json
cd lezer-template-json
pnpm install
pnpm build
pnpm test

Commands:

  • pnpm test:run - Run all tests
  • pnpm test:ui - Interactive test UI

Contributing

  • Fork the repository
  • Create a feature branch
  • Add tests in tests/fixtures/
  • Ensure tests pass
  • Submit a pull request

License

MIT

Keywords

lezer

FAQs

Package last updated on 08 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts