From af72d2821044402bad1421e4c4d4bc10730cc74a Mon Sep 17 00:00:00 2001 From: HerringtonDarkholme <2883231+HerringtonDarkholme@users.noreply.github.com> Date: Mon, 9 Sep 2024 21:04:42 -0400 Subject: [PATCH] fix: correct order of arguments in JS API fix #529 --- website/guide/api-usage/js-api.md | 14 +++++++------- website/reference/api.md | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/website/guide/api-usage/js-api.md b/website/guide/api-usage/js-api.md index 6b02602a..b9a8d6cb 100644 --- a/website/guide/api-usage/js-api.md +++ b/website/guide/api-usage/js-api.md @@ -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 @@ -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. @@ -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 @@ -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 @@ -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')" ``` @@ -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]) @@ -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')) ``` \ No newline at end of file diff --git a/website/reference/api.md b/website/reference/api.md index 1ba2c0e1..df421357 100644 --- a/website/reference/api.md +++ b/website/reference/api.md @@ -53,11 +53,11 @@ 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 @@ -65,14 +65,14 @@ export function pattern(pattern: string, lang: Lang): NapiConfig ```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()`. @@ -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 } @@ -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") ```