| 1 |
Querying By Selector |
| 2 |
========== |
| 3 |
**_From version 1.12_** |
| 4 |
|
| 5 |
Inspired by [esquery](https://github.com/estools/esquery) project, Peast allows you to query the generated AST using a syntax similar to CSS selectors. |
| 6 |
This can be done using the **Query** class: |
| 7 |
```php |
| 8 |
//Generate the AST |
| 9 |
$ast = Peast\Peast::latest($source, $options)->parse(); |
| 10 |
//Query the AST |
| 11 |
$query = new Peast\Query($ast, $options); |
| 12 |
$query->find("Literal[value='search me']"); |
| 13 |
``` |
| 14 |
|
| 15 |
Or you can use the shortcut method on the `Program` node (the one returned by Peast parser): |
| 16 |
```php |
| 17 |
//Generate the AST |
| 18 |
$ast = Peast\Peast::latest($source, $options)->parse(); |
| 19 |
//Query the AST |
| 20 |
$query = $ast->query("Literal[value='search me']"); |
| 21 |
``` |
| 22 |
|
| 23 |
The shortcut method returns a Query instance. |
| 24 |
|
| 25 |
Options |
| 26 |
------------- |
| 27 |
The Query class constructor takes an optional associative array of options. |
| 28 |
Available options are: |
| 29 |
* "encoding": to specify the encoding of the selector, if not specified the parser will assume UTF-8. |
| 30 |
|
| 31 |
Methods |
| 32 |
------------- |
| 33 |
### find |
| 34 |
The `find` method searches node that match the given selector starting from the current matching nodes. |
| 35 |
For example this code matches `FunctionDeclaration` nodes and then finds all the descendant `Literal` nodes: |
| 36 |
```php |
| 37 |
$ast->query("FunctionDeclaration")->find("Literal"); |
| 38 |
``` |
| 39 |
|
| 40 |
### filter |
| 41 |
The `filter` method filters the current matching nodes and keeps only those that respect the given selector. |
| 42 |
For example this code matches `Literal` nodes and then keeps only the nodes whose value is 2: |
| 43 |
```php |
| 44 |
$ast->query("FunctionDeclaration")->filter("[value=2]"); |
| 45 |
``` |
| 46 |
|
| 47 |
### count |
| 48 |
The `count` method returns the number of current matching nodes. |
| 49 |
You can also use the builtin `count` function: |
| 50 |
```php |
| 51 |
$ast->query("FunctionDeclaration")->count(); |
| 52 |
//Or |
| 53 |
count($ast->query("FunctionDeclaration")); |
| 54 |
``` |
| 55 |
|
| 56 |
### get |
| 57 |
The `get` method returns the node at the given index: |
| 58 |
```php |
| 59 |
//Returns the first matching node |
| 60 |
$ast->query("FunctionDeclaration")->get(0); |
| 61 |
``` |
| 62 |
|
| 63 |
Iteration |
| 64 |
------------- |
| 65 |
You can use the Query object in a foreach to loop all the matching nodes: |
| 66 |
```php |
| 67 |
foreach ($ast->query("FunctionDeclaration") as $node) { |
| 68 |
//... |
| 69 |
} |
| 70 |
``` |
| 71 |
|
| 72 |
Selectors syntax |
| 73 |
------------- |
| 74 |
Note that Peast tries to preserve the order of the nodes in the AST, but that is not always possible, so you shouldn't rely on that. |
| 75 |
|
| 76 |
### Filter by type |
| 77 |
You can filter nodes by their type simply writing it. |
| 78 |
For example `Literal` matches all the nodes whose type is Literal. |
| 79 |
|
| 80 |
### Filter by attribute |
| 81 |
You can filter nodes by their attributes writing the name and optionally the value inside square brackets. |
| 82 |
There are several types of attribute filters: |
| 83 |
* `[value]` matches all the nodes that have a `value` attribute, without checking its value |
| 84 |
* `[value="test"]` matches all the nodes whose `value` attribute equals to the string "test" |
| 85 |
* `[value^="test"]` matches all the nodes whose `value` attribute starts with the string "test" |
| 86 |
* `[value*="test"]` matches all the nodes whose `value` attribute contains the string "test" |
| 87 |
* `[value$="test"]` matches all the nodes whose `value` attribute ends with the string "test" |
| 88 |
* `[value>2]` matches all the nodes whose `value` attribute is greater than 2 |
| 89 |
* `[value>=2]` matches all the nodes whose `value` attribute is greater or equals to 2 |
| 90 |
* `[value<2]` matches all the nodes whose `value` attribute is lower than 2 |
| 91 |
* `[value<=2]` matches all the nodes whose `value` attribute is lower or equals to 2 |
| 92 |
|
| 93 |
In attributes filters the type is very important because a selector like `[value="2"]` will match a node whose `value` attribute is the string "2" but not 2 as number. |
| 94 |
Available types are: |
| 95 |
* Strings: `[value="a"]` or `[value='a']` |
| 96 |
* Integer numbers: `[value=123]` or `[value=0xFFF]` or `[value=0b11011]` or `[value=0o77]` |
| 97 |
* Decimal numbers: `[value=1.23]` |
| 98 |
* Booleans: `[value=true]` or `[value=false]` |
| 99 |
* Null: `[value=null]` |
| 100 |
|
| 101 |
Strings can be escaped using the backslash character, for example `[value='That\'s great']` will find a value that equals to the string "That's great". |
| 102 |
|
| 103 |
You can perform case-insensitive comparison using this syntax `[value='search' i]`. |
| 104 |
|
| 105 |
You can also search using a regexp in this way: `[value=/test\d+/i]`. |
| 106 |
|
| 107 |
Sometimes it's useful to check also inner attributes, you can do it by separating attributes name with a dot. |
| 108 |
For example `FunctionDeclaration[id.name='funcName']` matches all `FunctionDeclaration` whose `id` attribute has a `name` attribute with the value "funcName". |
| 109 |
|
| 110 |
### Filter by pseudo selector |
| 111 |
Pseudo selectors begin with `:` and can optionally accept arguments wrapped in parentheses. |
| 112 |
There are 3 groups of pseudo selectors: |
| 113 |
|
| 114 |
###### Simple pseudo selector |
| 115 |
These selectors don't accept any argument. |
| 116 |
* `:first-child` matches nodes that are the first child of their parent |
| 117 |
* `:last-child` matches nodes that are the first child of their parent |
| 118 |
* `:pattern` matches nodes that implement the `Pattern` interface |
| 119 |
* `:statement` matches nodes that implement the `Statement` interface |
| 120 |
* `:expression` matches nodes that implement the `Expression` interface |
| 121 |
* `:declaration` matches nodes that implement the `Declaration` interface |
| 122 |
|
| 123 |
###### Positional pseudo selector |
| 124 |
These selectors accept a number or a An+B syntax, where A represents the step and B is the starting offset. |
| 125 |
Remember that the index is 1-based, so the first node is 1. |
| 126 |
You can read more about the arguments accepted by these select on [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child). |
| 127 |
* `:nth-child` matches nodes that respects the given index in their parent children list. For example: `:nth-child(5n+3)` matches every 5th node starting from the 3rd one. |
| 128 |
* `:nth-last-child` matches nodes that respects the given index in their parent children list, starting from the end. For example: `:nth-last-child(1)` matches a node that is the last child of its parent. |
| 129 |
You can also use `even` and `odd` as arguments to match even and odds nodes. |
| 130 |
|
| 131 |
###### Inner selector pseudo selector |
| 132 |
These selectors accept a inner selector. |
| 133 |
* `:is` matches a node that respect the given selector. For example `Literal:is([value=2], [value=3])` matches `Literal` nodes whose `value` is 2 or 3 |
| 134 |
* `:not` matches a node that do not respect the given selector. For example `Literal:not([value=2], [value=3])` matches `Literal` nodes whose `value` is not 2 or 3 |
| 135 |
* `:has` matches a node whose descendant match the given selector. For example `AssignmentExpression:has(Literal[value="string""])` matches `AssignmentExpression` nodes that contain `Literal` nodes whose `value` is "string" |
| 136 |
|
| 137 |
### Combinators |
| 138 |
Combinators are used for match other nodes relative to the current. |
| 139 |
* Descendant: the space can be used to match descendant nodes. For example `AssignmentExpression Literal` matches `Literal` nodes inside `AssignmentExpression` nodes, even if they are not direct children |
| 140 |
* Children: the `>` character can be used to match child nodes. For example `ArrayExpression > Literal` matches a `Literal` nodes that are children of `ArrayExpression` nodes |
| 141 |
* Adjacent Sibling: the `+` character can be used to match nodes that follow other nodes. For example `FunctionDeclaration + VariableDeclaration` matches the first `VariableDeclaration` nodes that follow `FunctionDeclaration` nodes |
| 142 |
* General Sibling: the `~` character can be used to match all the nodes that follow other nodes. For example `FunctionDeclaration ~ VariableDeclaration` matches all the `VariableDeclaration` nodes that follow `FunctionDeclaration` nodes |
| 143 |
|
| 144 |
### Groups |
| 145 |
A selector can contain multiple selector groups separated by commas. |
| 146 |
For example: `Literal, ArrayExpression` match all the `Literal` and `ArrayExpression` nodes. |