Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
@lezer/javascript
Advanced tools
Package description
@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.
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);
Acorn is a small, fast, JavaScript-based JavaScript parser. It is designed to be modular and can be extended with plugins. Compared to @lezer/javascript, Acorn is more widely used and has a larger community, but it may not integrate as seamlessly with the Lezer ecosystem.
Esprima is a high-performance, standard-compliant ECMAScript parser written in JavaScript. It is known for its speed and accuracy. While Esprima is similar to @lezer/javascript in terms of functionality, it is more mature and has been used in many production environments.
Babel Parser (formerly Babylon) is a JavaScript parser used in the Babel project. It supports the latest JavaScript features and experimental syntax. Compared to @lezer/javascript, Babel Parser is more feature-rich and is part of the larger Babel ecosystem, which includes tools for code transformation and transpilation.
Changelog
1.4.9 (2023-10-30)
Allow default
to be used in import
/export
binding sets.
Readme
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
lezer-based JavaScript grammar
The npm package @lezer/javascript receives a total of 605,468 weekly downloads. As such, @lezer/javascript popularity was classified as popular.
We found that @lezer/javascript demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.