Skip to content

Commit

Permalink
fix: correct order of arguments in JS API
Browse files Browse the repository at this point in the history
fix #529
  • Loading branch information
HerringtonDarkholme committed Sep 10, 2024
1 parent 6b648d9 commit af72d28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions website/guide/api-usage/js-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ A common workflow to use ast-grep's JavaScript API is:
import { parse, Lang } from '@ast-grep/napi';
let source = `console.log("hello world")`
const ast = parse(source, Lang.JavaScript) // 1. parse the source
const ast = parse(Lang.JavaScript, source) // 1. parse the source
const root = ast.root() // 2. get the root
const node = root.find('console.log($A)') // 3. find the node
node.getMatch('A').text() // 4. collect the info
Expand All @@ -64,7 +64,7 @@ We can import the `Lang` enum from the `@ast-grep/napi` package and call the `p
import { Lang, parse } from '@ast-grep/napi';
const source = `console.log("hello world")`
const ast = parse(source, Lang.JavaScript)
const ast = parse(Lang.JavaScript, source)
```

The `SgRoot` object has a `root` method that returns the root `SgNode` of the AST.
Expand Down Expand Up @@ -123,7 +123,7 @@ A `Matcher` can be one of the three types: `string`, `number` or `object`.
// basic find example
root.find('console.log($A)') // returns SgNode of call_expression
let l = Lang.JavaScript // calling kind function requires Lang
const kind = kind('string', l) // convert kind name to kind id number
const kind = kind(l, 'string') // convert kind name to kind id number
root.find(kind) // returns SgNode of string
root.find('notExist') // returns null if not found

Expand Down Expand Up @@ -160,7 +160,7 @@ const src = `
console.log('hello')
logger('hello', 'world', '!')
`
const root = parse(src, Lang.JavaScript).root()
const root = parse(Lang.JavaScript, src).root()
const node = root.find('console.log($A)')
const arg = node.getMatch("A") // returns SgNode('hello')
arg !== null // true, node is found
Expand Down Expand Up @@ -191,7 +191,7 @@ export class SgNode {
**Example:**

```ts{3}
const ast = parse("console.log('hello world')", Lang.JavaScript)
const ast = parse(Lang.JavaScript, "console.log('hello world')")
root = ast.root()
root.text() // will return "console.log('hello world')"
```
Expand Down Expand Up @@ -275,7 +275,7 @@ class SgNode {
**Example**

```ts{3,4}
const root = parse("console.log('hello world')", Lang.JavaScript).root()
const root = parse(Lang.JavaScript, "console.log('hello world')").root()
const node = root.find('console.log($A)')
const edit = node.replace("console.error('bye world')")
const newSource = node.commitEdits([edit])
Expand All @@ -300,7 +300,7 @@ To access other languages, you can use the `parse`/`parseAsync` function and the
```js
import { parse, Lang } from '@ast-grep/napi'

const sg = parse('def test(): pass', Lang.Python)
const sg = parse(Lang.Python, 'def test(): pass')

console.log(sg.root().has('function_definition'))
```
14 changes: 7 additions & 7 deletions website/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,26 @@ ast-grep also provides other utility for parse kind string and construct pattern

```ts
/** Parse a string to an ast-grep instance */
export function parse(src: string, lang: Lang): SgRoot
export function parse(lang: Lang, src: string): SgRoot
/** Get the `kind` number from its string name. */
export function kind(kindName: string, lang: Lang): number
export function kind(lang: Lang, kindName: string): number
/** Compile a string to ast-grep Pattern. */
export function pattern(pattern: string, lang: Lang): NapiConfig
export function pattern(lang: Lang, pattern: string): NapiConfig
```

#### Example

```ts
import { parse, Lang } from '@ast-grep/napi'
const ast = parse(source, Lang.JavaScript)
const ast = parse(Lang.JavaScript, source)
const root = ast.root()
root.find("console.log")
```

### SgRoot

You will get an `SgRoot` instance when you `lang.parse(string)`.
You will get an `SgRoot` instance when you `parse(lang, string)`.

`SgRoot` can also be accessed in `lang.findInFiles`'s callback by calling `node.getRoot()`.

Expand All @@ -87,7 +87,7 @@ class SgRoot {
root(): SgNode
/**
* Returns the path of the file if it is discovered by ast-grep's `findInFiles`.
* Returns `"anonymous"` if the instance is created by `lang.parse(source)`.
* Returns `"anonymous"` if the instance is created by `parse(lang, source)`.
*/
filename(): string
}
Expand All @@ -98,7 +98,7 @@ class SgRoot {
```ts
import { parse, Lang } from '@ast-grep/napi'
const ast = parse(source, Lang.JavaScript)
const ast = parse(Lang.JavaScript, source)
const root = ast.root()
root.find("console.log")
```
Expand Down

0 comments on commit af72d28

Please sign in to comment.