UNPKG

4.3 kBMarkdownView Raw
1# TSQuery
2
3[![npm version](https://img.shields.io/npm/v/@phenomnomnominal/tsquery.svg)](https://img.shields.io/npm/v/@phenomnomnominal/tsquery.svg)
4[![Code Climate](https://codeclimate.com/github/phenomnomnominal/tsquery/badges/gpa.svg)](https://codeclimate.com/github/phenomnomnominal/tsquery)
5[![Test Coverage](https://codeclimate.com/github/phenomnomnominal/tsquery/coverage.svg)](https://codeclimate.com/github/phenomnomnominal/tsquery/coverage)
6
7TSQuery 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.
8
9[Check out the ESQuery demo](https://estools.github.io/esquery/) - note that the demo requires JavaScript code, not TypeScript
10
11[You can also check out the TSQuery Playground](https://tsquery-playground.firebaseapp.com) - Lovingly crafted by [Uri Shaked]( https://github.com/urish)
12
13## Installation
14
15```sh
16npm install @phenomnomnominal/tsquery --save-dev
17```
18
19## Examples
20
21Say 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.
22
23We would do something like the following:
24
25```ts
26import { tsquery } from '@phenomnomnominal/tsquery';
27
28const typescript = `
29
30class Animal {
31 constructor(public name: string) { }
32 move(distanceInMeters: number = 0) {
33 console.log(\`\${this.name} moved \${distanceInMeters}m.\`);
34 }
35}
36
37class Snake extends Animal {
38 constructor(name: string) { super(name); }
39 move(distanceInMeters = 5) {
40 console.log("Slithering...");
41 super.move(distanceInMeters);
42 }
43}
44
45`;
46
47const ast = tsquery.ast(typescript);
48const nodes = tsquery(ast, 'Identifier[name="Animal"]');
49console.log(nodes.length); // 2
50```
51
52[Try running this code in StackBlitz](https://stackblitz.com/edit/tsquery-examples)!
53
54### Selectors
55
56The following selectors are supported:
57
58* AST node type: `ForStatement` (see [common node types](#common-ast-node-types))
59* [wildcard](http://dev.w3.org/csswg/selectors4/#universal-selector): `*`
60* [attribute existence](http://dev.w3.org/csswg/selectors4/#attribute-selectors): `[attr]`
61* [attribute value](http://dev.w3.org/csswg/selectors4/#attribute-selectors): `[attr="foo"]` or `[attr=123]`
62* attribute regex: `[attr=/foo.*/]`
63* attribute conditons: `[attr!="foo"]`, `[attr>2]`, `[attr<3]`, `[attr>=2]`, or `[attr<=3]`
64* nested attribute: `[attr.level2="foo"]`
65* field: `FunctionDeclaration > Identifier.id`
66* [First](http://dev.w3.org/csswg/selectors4/#the-first-child-pseudo) or [last](http://dev.w3.org/csswg/selectors4/#the-last-child-pseudo) child: `:first-child` or `:last-child`
67* [nth-child](http://dev.w3.org/csswg/selectors4/#the-nth-child-pseudo) (no ax+b support): `:nth-child(2)`
68* [nth-last-child](http://dev.w3.org/csswg/selectors4/#the-nth-last-child-pseudo) (no ax+b support): `:nth-last-child(1)`
69* [descendant](http://dev.w3.org/csswg/selectors4/#descendant-combinators): `ancestor descendant`
70* [child](http://dev.w3.org/csswg/selectors4/#child-combinators): `parent > child`
71* [following sibling](http://dev.w3.org/csswg/selectors4/#general-sibling-combinators): `node ~ sibling`
72* [adjacent sibling](http://dev.w3.org/csswg/selectors4/#adjacent-sibling-combinators): `node + adjacent`
73* [negation](http://dev.w3.org/csswg/selectors4/#negation-pseudo): `:not(ForStatement)`
74* [matches-any](http://dev.w3.org/csswg/selectors4/#matches): `:matches([attr] > :first-child, :last-child)`
75* [has](https://drafts.csswg.org/selectors-4/#has-pseudo): `IfStatement:has([name="foo"])`
76* class of AST node: `:statement`, `:expression`, `:declaration`, `:function`, or `:pattern`
77
78### Common AST node types
79
80* `Identifier` - any identifier (name of a function, class, variable, etc)
81* `IfStatement`, `ForStatement`, `WhileStatement`, `DoStatement` - control flow
82* `FunctionDeclaration`, `ClassDeclaration`, `ArrowFunction` - declarations
83* `VariableStatement` - var, const, let.
84* `ImportDeclaration` - any `import` statement
85* `StringLiteral` - any string
86* `TrueKeyword`, `FalseKeyword`, `NullKeyword`, `AnyKeyword` - various keywords
87* `CallExpression` - function call
88* `NumericLiteral` - any numeric constant
89* `NoSubstitutionTemplateLiteral`, `TemplateExpression` - template strings and expressions