Socket
Socket
Sign inDemoInstall

esquery

Package Overview
Dependencies
0
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    esquery

A query library for ECMAScript AST using a CSS selector like query language.


Version published
Maintainers
1
Install size
112 kB
Created

Package description

What is esquery?

The esquery npm package is a tool for querying the abstract syntax tree (AST) of ECMAScript (JavaScript) code. It allows developers to find specific nodes within the AST using a CSS-like query syntax, making it easier to analyze and manipulate code structure programmatically.

What are esquery's main functionalities?

Selecting nodes by type

This feature allows you to select all nodes of a specific type, such as all function declarations in the AST. The code sample demonstrates how to select all function declaration nodes.

esquery(ast, 'FunctionDeclaration');

Selecting nodes by attribute

You can select nodes based on their attributes, such as selecting all nodes where the name attribute is 'myFunction'. The code sample shows how to select nodes with a specific name.

esquery(ast, '[name="myFunction"]');

Pseudo-classes for node selection

Esquery supports pseudo-classes for more complex queries, such as selecting the second child node of every matched set. The code sample demonstrates selecting the second child node.

esquery(ast, ':nth-child(2)');

Other packages similar to esquery

Readme

Source

ESQuery is a library for querying the AST output by Esprima for patterns of syntax using a CSS style selector system. Check out the demo:

demo

The following selectors are supported:

  • AST node type: ForStatement
  • Wildcard: *
  • Node attribute existence: [attr]
  • Node attribute value: [attr="foo"] or [attr=123]
  • Node attribute regex: [attr=/foo.*/]
  • Node attribute conditons: [attr!="foo"], [attr>2], [attr<3], [attr>=2], or [attr<=3]
  • Nested node attribute: [attr.level2="foo"]
  • First or last child: :first-child or :last-child
  • nth-child from beginning (1 based index, no ax+b support): :nth-child(2)
  • nth-last-child from end (1 based index, no ax+b support): :nth-last-child(1)
  • descendant: ancestor descendant
  • child: parent > child
  • sibling: node ~ sibling
  • adjacent: node + adjacent
  • Negation: :not(ForStatement)
  • Matches any pseudo selector: :matches([attr] > :first-child, :last-child)
  • Selector subject (like css4, postfix only): IfStatement! > [name="foo"]

Build Status

Keywords

FAQs

Last updated on 03 Aug 2013

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