Query TypeScript ASTs with the esquery API!
npm install @aztack/tsqueryTSQuery is a port of the ESQuery API for TypeScript! TSQuery allows you to query a TypeScript AST for patterns of syntax using a CSS style selector system.
``sh`
npm install @aztack/tsquery --save-devor install tsquery cli to global
npm install -g @aztack/tsquery
Say we want to select all instances of an identifier with name "Animal", e.g. the identifier in the class declaration, and the identifier in the extends declaration.
We would do something like the following:
`ts
import { tsquery } from '@aztack/tsquery';
const typescript =
class Animal {
constructor(public name: string) { }
move(distanceInMeters: number = 0) {
console.log(\\${this.name} moved \${distanceInMeters}m.\);
}
}
class Snake extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = 5) {
console.log("Slithering...");
super.move(distanceInMeters);
}
}
const ast = tsquery.ast(typescript);
const nodes = tsquery(ast, 'Identifier[name="Animal"]');
console.log(nodes.length); // 2
`
Usage:
$ tsquery [inline-script | js-file-path]
``ts
// ./src/a.ts
class ClassA {}// ./src/b.ts
class ClassB {}
// ./process.tsq.js
module.exprots = function ($, $filepath, $filename, $files, $tsq) {
// matched: files matched with glob-pattern
// index: current file index
// $files = {matched, index}
// ast: ast of current ts code
// tsquery: the tsquery instance
// code: code string read file current file
// ts: the typescript library
// $tsq = {ast, tsquery, code, ts}
return $.name.escapedText;
}
``bash
$ tsquery "./src/*.ts" "ClassDeclaration" "return $.name.escapedText"
or
$ tsquery "./src/*.ts" "ClassDeclaration" "./process.tsq.js"
`
will output`js
{
query: "ClassDeclaration",
result: [{
file: './a.ts',
basename: 'a',
count: 1,
result: [ 'ClassA' ]
},{
file: './b.ts',
basename: 'b',
count: 1,
result: [ 'ClassB' ]
}],
count: 2
script: "inline",
}
`$3
The following selectors are supported:
* AST node type:
ForStatement (see common node types)
wildcard:
* attribute existence: [attr]
* attribute value: [attr="foo"] or [attr=123]
attribute regex: [attr=/foo./]
* attribute conditons: [attr!="foo"], [attr>2], [attr<3], [attr>=2], or [attr<=3]
* nested attribute: [attr.level2="foo"]
* field: FunctionDeclaration > Identifier.id
* First or last child: :first-child or :last-child
* nth-child (no ax+b support): :nth-child(2)
* nth-last-child (no ax+b support): :nth-last-child(1)
* descendant: ancestor descendant
* child: parent > child
* following sibling: node ~ sibling
* adjacent sibling: node + adjacent
* negation: :not(ForStatement)
* matches-any: :matches([attr] > :first-child, :last-child)
* has: IfStatement:has([name="foo"])
* class of AST node: :statement, :expression, :declaration, :function, or :pattern$3
*
Identifier - any identifier (name of a function, class, variable, etc)
* IfStatement, ForStatement, WhileStatement, DoStatement - control flow
* FunctionDeclaration, ClassDeclaration, ArrowFunction - declarations
* VariableStatement - var, const, let.
* ImportDeclaration - any import statement
* StringLiteral - any string
* TrueKeyword, FalseKeyword, NullKeyword, AnyKeyword - various keywords
* CallExpression - function call
* NumericLiteral - any numeric constant
* NoSubstitutionTemplateLiteral, TemplateExpression` - template strings and expressions