Socket
Socket
Sign inDemoInstall

@lezer/javascript

Package Overview
Dependencies
3
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @lezer/javascript

lezer-based JavaScript grammar


Version published
Weekly downloads
593K
decreased by-6.02%
Maintainers
1
Install size
798 kB
Created
Weekly downloads
 

Package description

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

Changelog

Source

1.4.17 (2024-06-11)

Bug fixes

Add support for {-readonly [K in T]-?: U} TypeScript syntax.

Readme

Source

@lezer/javascript

This is a JavaScript grammar for the lezer parser system.

It parses ES2020, and supports a "ts" dialect to parse TypeScript, and a "jsx" dialect to parse JSX.

The top option can be set to "SingleExpression" or "SingleClassItem" to parse an expression or class item instead of a full program.

The code is licensed under an MIT license.

FAQs

Last updated on 11 Jun 2024

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc