Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion website/guide/api-usage/js-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@ To try out the JavaScript API, you can use the [code sandbox](https://codesandbo
First, install ast-grep's napi package.

::: code-group

```bash[npm]
npm install --save @ast-grep/napi
```

```bash[pnpm]
pnpm add @ast-grep/napi
```

:::

Now let's explore ast-grep's API!

## Core Concepts

The core concepts in ast-grep's JavaScript API are:

* `SgRoot`: a class representing the whole syntax tree
* `SgNode`: a node in the syntax tree

Expand Down Expand Up @@ -133,6 +136,16 @@ Array.isArray(nodes) // true, findAll returns SgNode
nodes.map(n => n.text()) // string array of function source
const empty = root.findAll('not exist') // returns []
empty.length === 0 // true

// find i.e. `console.log("hello world")` using a NapiConfig
const node = root.find({
rule: {
pattern: "console.log($A)"
},
constraints: {
A: { regex: "hello" }
}
})
```

Note, `find` returns `null` if no node is found. `findAll` returns an empty array if nothing matches.
Expand Down Expand Up @@ -250,6 +263,7 @@ export class SgNode {
prevAll(): Array<SgNode>
}
```

## Fix code

`SgNode` is immutable so it is impossible to change the code directly.
Expand Down Expand Up @@ -294,7 +308,6 @@ See also [ast-grep#1172](https://github.com/ast-grep/ast-grep/issues/1172)

To access other languages, you can use the `parse`/`parseAsync` function and the `Lang` enum.


**Example**

```js
Expand Down