Skip to content

Commit af72d28

Browse files
fix: correct order of arguments in JS API
fix #529
1 parent 6b648d9 commit af72d28

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

website/guide/api-usage/js-api.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ A common workflow to use ast-grep's JavaScript API is:
4747
import { parse, Lang } from '@ast-grep/napi';
4848
4949
let source = `console.log("hello world")`
50-
const ast = parse(source, Lang.JavaScript) // 1. parse the source
50+
const ast = parse(Lang.JavaScript, source) // 1. parse the source
5151
const root = ast.root() // 2. get the root
5252
const node = root.find('console.log($A)') // 3. find the node
5353
node.getMatch('A').text() // 4. collect the info
@@ -64,7 +64,7 @@ We can import the `Lang` enum from the `@ast-grep/napi` package and call the `p
6464
import { Lang, parse } from '@ast-grep/napi';
6565
6666
const source = `console.log("hello world")`
67-
const ast = parse(source, Lang.JavaScript)
67+
const ast = parse(Lang.JavaScript, source)
6868
```
6969

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

@@ -160,7 +160,7 @@ const src = `
160160
console.log('hello')
161161
logger('hello', 'world', '!')
162162
`
163-
const root = parse(src, Lang.JavaScript).root()
163+
const root = parse(Lang.JavaScript, src).root()
164164
const node = root.find('console.log($A)')
165165
const arg = node.getMatch("A") // returns SgNode('hello')
166166
arg !== null // true, node is found
@@ -191,7 +191,7 @@ export class SgNode {
191191
**Example:**
192192

193193
```ts{3}
194-
const ast = parse("console.log('hello world')", Lang.JavaScript)
194+
const ast = parse(Lang.JavaScript, "console.log('hello world')")
195195
root = ast.root()
196196
root.text() // will return "console.log('hello world')"
197197
```
@@ -275,7 +275,7 @@ class SgNode {
275275
**Example**
276276

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

303-
const sg = parse('def test(): pass', Lang.Python)
303+
const sg = parse(Lang.Python, 'def test(): pass')
304304

305305
console.log(sg.root().has('function_definition'))
306306
```

website/reference/api.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,26 @@ ast-grep also provides other utility for parse kind string and construct pattern
5353

5454
```ts
5555
/** Parse a string to an ast-grep instance */
56-
export function parse(src: string, lang: Lang): SgRoot
56+
export function parse(lang: Lang, src: string): SgRoot
5757
/** Get the `kind` number from its string name. */
58-
export function kind(kindName: string, lang: Lang): number
58+
export function kind(lang: Lang, kindName: string): number
5959
/** Compile a string to ast-grep Pattern. */
60-
export function pattern(pattern: string, lang: Lang): NapiConfig
60+
export function pattern(lang: Lang, pattern: string): NapiConfig
6161
```
6262

6363
#### Example
6464

6565
```ts
6666
import { parse, Lang } from '@ast-grep/napi'
6767
68-
const ast = parse(source, Lang.JavaScript)
68+
const ast = parse(Lang.JavaScript, source)
6969
const root = ast.root()
7070
root.find("console.log")
7171
```
7272

7373
### SgRoot
7474

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

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

@@ -87,7 +87,7 @@ class SgRoot {
8787
root(): SgNode
8888
/**
8989
* Returns the path of the file if it is discovered by ast-grep's `findInFiles`.
90-
* Returns `"anonymous"` if the instance is created by `lang.parse(source)`.
90+
* Returns `"anonymous"` if the instance is created by `parse(lang, source)`.
9191
*/
9292
filename(): string
9393
}
@@ -98,7 +98,7 @@ class SgRoot {
9898
```ts
9999
import { parse, Lang } from '@ast-grep/napi'
100100
101-
const ast = parse(source, Lang.JavaScript)
101+
const ast = parse(Lang.JavaScript, source)
102102
const root = ast.root()
103103
root.find("console.log")
104104
```

0 commit comments

Comments
 (0)