@@ -47,7 +47,7 @@ A common workflow to use ast-grep's JavaScript API is:
47
47
import { parse, Lang } from '@ast-grep/napi';
48
48
49
49
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
51
51
const root = ast.root() // 2. get the root
52
52
const node = root.find('console.log($A)') // 3. find the node
53
53
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
64
64
import { Lang, parse } from '@ast-grep/napi';
65
65
66
66
const source = `console.log("hello world")`
67
- const ast = parse(source, Lang.JavaScript)
67
+ const ast = parse(Lang.JavaScript, source )
68
68
```
69
69
70
70
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`.
123
123
// basic find example
124
124
root .find (' console.log($A)' ) // returns SgNode of call_expression
125
125
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
127
127
root .find (kind ) // returns SgNode of string
128
128
root .find (' notExist' ) // returns null if not found
129
129
@@ -160,7 +160,7 @@ const src = `
160
160
console.log('hello')
161
161
logger('hello', 'world', '!')
162
162
`
163
- const root = parse(src, Lang.JavaScript).root()
163
+ const root = parse(Lang.JavaScript, src ).root()
164
164
const node = root.find('console.log($A)')
165
165
const arg = node.getMatch("A") // returns SgNode('hello')
166
166
arg !== null // true, node is found
@@ -191,7 +191,7 @@ export class SgNode {
191
191
** Example:**
192
192
193
193
``` ts{3}
194
- const ast = parse("console.log('hello world')", Lang.JavaScript )
194
+ const ast = parse(Lang.JavaScript, "console.log('hello world')")
195
195
root = ast.root()
196
196
root.text() // will return "console.log('hello world')"
197
197
```
@@ -275,7 +275,7 @@ class SgNode {
275
275
** Example**
276
276
277
277
``` 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()
279
279
const node = root.find('console.log($A)')
280
280
const edit = node.replace("console.error('bye world')")
281
281
const newSource = node.commitEdits([edit])
@@ -300,7 +300,7 @@ To access other languages, you can use the `parse`/`parseAsync` function and the
300
300
``` js
301
301
import { parse , Lang } from ' @ast-grep/napi'
302
302
303
- const sg = parse (' def test(): pass' , Lang . Python )
303
+ const sg = parse (Lang . Python , ' def test(): pass' )
304
304
305
305
console .log (sg .root ().has (' function_definition' ))
306
306
```
0 commit comments