
Product
Unify Your Security Stack with Socket Basics
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.
`babel-q` offers a way to query an babel AST to find specific patterns using a syntax somewhat similar to XPath. `babel-q` was inspired by [astq](https://github.com/rse/astq), but offers some features that did not seem possible in `astq` and also has som
babel-q
offers a way to query an babel AST to find specific patterns using a syntax somewhat similar to XPath.
babel-q
was inspired by astq, but offers some features that did not seem possible in astq
and also has some limits compared to astq
.
Goals:
The following pattern:
//AssignmentExpression[
/MemberExpression[
/:property/:name == "migrateVersion" &&
/$:object == ../../../../:params
]
]/:right/:value
tries to find the following code snippet and returns the string value assigned to the migrateVersion
property:
...
function( jQuery, window ) {
...
jQuery.migrateVersion = "3.4.1";
...
}
...
Find an AssignmentExpression at arbitrary depth, which directly contains a MemberExpression where the property name is "migrateVersion" and the object is bound to a parameter of the surrounding function, an return the assigned value.
/<node type>
- find a child node of the given type//<node type>
- find a descendant of the given type/:<name>
- find an attribute of the current node with the given name//:<name>
- find an attribute of the current node with the given name regardless of whether it's on the current node or on a descendant/$:<name>
- find the binding of an Identifier/$$:<name>
- return the binding or the attribute if bining cannot be resolved (helpful if a variable is sometimes directly assigned and sometimes not)[]
- apply a filter to the node&&
, ||
- logical conditions of a filter==
- comparison in filter../
- go to parent in filter (use with care, as this causes extra traversal)/*
- wildcard type child//*
- wildcard type descendant'<some value>'
,"<some value>"
- a string literalquery(code: ParseResult<Babel.File> | string, query: string) : Result[]
- Runs the given query on the given code in the form of an already parsed AST or a string (which is parsed as sourceType: "script"
), and returns the result.multiQuery<T extends Record<string, string>>(code: ParseResult<Babel.File> | string, namedQueries: T) : Record<keyof T, Result[]>
- Runs the given set of named queries on the given code in the form of an already parsed AST or a string (which is parsed as sourceType: "script"
), and returns a map of named results (one result array per named query).where Result
is Babel.Node | string | number | boolean;
import { query } from "babel-q";
import * as fs from "fs";
const contents = fs.readFileSync("some-file.js");
const result = query(
contents,
`//FunctionDeclaration/:id/:name`
);
console.log("Function names", result);
[1.0.0-beta.3] - 2024-02-06
$$
which will resolve to the right value for x in both a.x = 2
and let y = 1; a.x = y;
FAQs
offers a way to query an babel AST to find specific patterns using a syntax somewhat similar to XPath.
The npm package babel-q receives a total of 80 weekly downloads. As such, babel-q popularity was classified as not popular.
We found that babel-q demonstrated a not healthy version release cadence and project activity because the last version was released 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
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.
Product
Socket is launching experimental protection for the Hugging Face ecosystem, scanning for malware and malicious payload injections inside model files to prevent silent AI supply chain attacks.
Research
/Security News
The Socket Threat Research Team uncovered a coordinated campaign that floods the Chrome Web Store with 131 rebranded clones of a WhatsApp Web automation extension to spam Brazilian users.