🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@lezer/javascript

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lezer/javascript

lezer-based JavaScript grammar

1.5.1
latest
Source
npm
Version published
Weekly downloads
1.3M
-2.25%
Maintainers
1
Weekly downloads
 
Created

What is @lezer/javascript?

@lezer/javascript is a parser for JavaScript written using the Lezer parser system. It provides a way to parse JavaScript code into a syntax tree, which can be used for various purposes such as syntax highlighting, code analysis, and transformation.

What are @lezer/javascript's main functionalities?

Parsing JavaScript Code

This feature allows you to parse JavaScript code into a syntax tree. The code sample demonstrates how to parse a simple JavaScript statement and print the resulting syntax tree.

const {parser} = require('@lezer/javascript');
const input = 'const x = 10;';
const tree = parser.parse(input);
console.log(tree.toString());

Syntax Tree Traversal

This feature allows you to traverse the syntax tree. The code sample shows how to use a TreeCursor to iterate over the nodes in the syntax tree and print their types.

const {parser} = require('@lezer/javascript');
const {TreeCursor} = require('@lezer/common');
const input = 'const x = 10;';
const tree = parser.parse(input);
let cursor = tree.cursor();
do {
  console.log(cursor.node.type.name);
} while (cursor.next());

Custom Syntax Highlighting

This feature allows you to apply custom syntax highlighting to JavaScript code. The code sample demonstrates how to define a highlighting style and apply it to a parsed syntax tree.

const {parser} = require('@lezer/javascript');
const {highlightTree} = require('@lezer/highlight');
const {styleTags, tags} = require('@lezer/highlight');
const input = 'const x = 10;';
const tree = parser.parse(input);
const highlightStyle = styleTags({
  'VariableName': tags.variableName,
  'Keyword': tags.keyword,
  'Number': tags.number
});
let highlighted = '';
highlightTree(tree, highlightStyle, (from, to, classes) => {
  highlighted += input.slice(from, to) + ' [' + classes + '] ';
});
console.log(highlighted);

Other packages similar to @lezer/javascript

FAQs

Package last updated on 18 Apr 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