esquery
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
estraverse | 5.3.0 | 7.86 kB | BSD-2-Clause | prod |
ESQuery is a powerful JavaScript library designed for querying the Abstract Syntax Tree (AST) generated by Esprima. Its functionality revolves around the use of a CSS-style selector system to recognize and identify patterns of syntax within the AST. It provides a highly flexible and efficient method for processing and analyzing JavaScript code, making it easier for developers to manipulate the structure and elements of scripts.
Using ESQuery is straightforward and intuitive, especially for those familiar with CSS selectors. To implement it, you need to first establish the AST, then apply ESQuery functions. You can make use of a variety of selectors like AST node type, wildcard, attribute existence, attribute value, attribute regex, attribute conditions, nested attributes, and others for highly specific queries. Here's a basic example of utilizing ESQuery:
const esquery = require('esquery');
const esprima = require('esprima');
// Parse your JavaScript source code to AST using Esprima:
const ast = esprima.parseScript('your JavaScript code here');
// Use esquery to query specific patterns in the AST:
const matches = esquery.query(ast, 'your CSS style selector here');
In this example, 'matches' will hold an array of all the nodes in the AST that match the provided CSS style selector.
You can find the ESQuery documentation and get detailed explanations on the different selectors and the syntax within the GitHub repository of the project. There's also a useful demo to see how the selectors work, providing a great way to experiment with the functionality of ESQuery and understanding how to use it effectively in your projects. Remember to check the selectors supported section, it exhibits comprehensive information on leveraging different selector types, including attribute value, descendant, child, and more.