Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
@lezer/javascript
Advanced tools
@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.
This is a JavaScript grammar for the lezer parser system.
It parses modern JavaScript, 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.
1.4.21 (2024-12-03)
Add support for const
modifiers on TypeScript type parameters.
Allow TypeScript syntax, where the condition is just a variable.
Fix a bug where some TypeScript <
tokens didn't appear in the syntax tree.
FAQs
lezer-based JavaScript grammar
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.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.