@ast-grep/napi 0.22.4 → 0.22.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +63 -24
  2. package/index.js +8 -3
  3. package/package.json +9 -9
package/index.d.ts CHANGED
@@ -13,18 +13,53 @@ export interface NapiConfig {
13
13
  /** See https://ast-grep.github.io/guide/rule-config.html#constraints */
14
14
  constraints?: any
15
15
  /** Available languages: html, css, js, jsx, ts, tsx */
16
- language?: FrontEndLanguage
16
+ language?: Lang
17
17
  /** https://ast-grep.github.io/reference/yaml.html#transform */
18
18
  transform?: any
19
19
  /** https://ast-grep.github.io/guide/rule-config/utility-rule.html */
20
20
  utils?: any
21
21
  }
22
- export const enum FrontEndLanguage {
23
- Html = 0,
24
- JavaScript = 1,
25
- Tsx = 2,
26
- Css = 3,
27
- TypeScript = 4
22
+ export interface FileOption {
23
+ paths: Array<string>
24
+ languageGlobs: Record<string, Array<string>>
25
+ }
26
+ export function parseFiles(paths: Array<string> | FileOption, callback: (err: null | Error, result: SgRoot) => void): Promise<number>
27
+ export interface FindConfig {
28
+ /** specify the file paths to recursively find files */
29
+ paths: Array<string>
30
+ /** a Rule object to find what nodes will match */
31
+ matcher: NapiConfig
32
+ /**
33
+ * An list of pattern globs to treat of certain files in the specified language.
34
+ * eg. ['*.vue', '*.svelte'] for html.findFiles, or ['*.ts'] for tsx.findFiles.
35
+ * It is slightly different from https://ast-grep.github.io/reference/sgconfig.html#languageglobs
36
+ */
37
+ languageGlobs?: Array<string>
38
+ }
39
+ export const enum Lang {
40
+ Html = 'Html',
41
+ JavaScript = 'JavaScript',
42
+ Tsx = 'Tsx',
43
+ Css = 'Css',
44
+ TypeScript = 'TypeScript',
45
+ Bash = 'Bash',
46
+ C = 'C',
47
+ Cpp = 'Cpp',
48
+ CSharp = 'CSharp',
49
+ Dart = 'Dart',
50
+ Go = 'Go',
51
+ Elixir = 'Elixir',
52
+ Haskell = 'Haskell',
53
+ Java = 'Java',
54
+ Json = 'Json',
55
+ Kotlin = 'Kotlin',
56
+ Lua = 'Lua',
57
+ Php = 'Php',
58
+ Python = 'Python',
59
+ Ruby = 'Ruby',
60
+ Rust = 'Rust',
61
+ Scala = 'Scala',
62
+ Swift = 'Swift'
28
63
  }
29
64
  export interface Edit {
30
65
  /** The position of the edit */
@@ -48,23 +83,27 @@ export interface Range {
48
83
  /** ending position of the range */
49
84
  end: Pos
50
85
  }
51
- export interface FileOption {
52
- paths: Array<string>
53
- languageGlobs: Record<string, Array<string>>
54
- }
55
- export function parseFiles(paths: Array<string> | FileOption, callback: (err: null | Error, result: SgRoot) => void): Promise<number>
56
- export interface FindConfig {
57
- /** specify the file paths to recursively find files */
58
- paths: Array<string>
59
- /** a Rule object to find what nodes will match */
60
- matcher: NapiConfig
61
- /**
62
- * An list of pattern globs to treat of certain files in the specified language.
63
- * eg. ['*.vue', '*.svelte'] for html.findFiles, or ['*.ts'] for tsx.findFiles.
64
- * It is slightly different from https://ast-grep.github.io/reference/sgconfig.html#languageglobs
65
- */
66
- languageGlobs?: Array<string>
67
- }
86
+ /** Parse a string to an ast-grep instance */
87
+ export function parse(lang: Lang, src: string): SgRoot
88
+ /**
89
+ * Parse a string to an ast-grep instance asynchronously in threads.
90
+ * It utilize multiple CPU cores when **concurrent processing sources**.
91
+ * However, spawning excessive many threads may backfire.
92
+ * Please refer to libuv doc, nodejs' underlying runtime
93
+ * for its default behavior and performance tuning tricks.
94
+ */
95
+ export function parseAsync(lang: Lang, src: string): Promise<SgRoot>
96
+ /** Get the `kind` number from its string name. */
97
+ export function kind(lang: Lang, kindName: string): number
98
+ /** Compile a string to ast-grep Pattern. */
99
+ export function pattern(lang: Lang, pattern: string): NapiConfig
100
+ /**
101
+ * Discover and parse multiple files in Rust.
102
+ * `lang` specifies the language.
103
+ * `config` specifies the file path and matcher.
104
+ * `callback` will receive matching nodes found in a file.
105
+ */
106
+ export function findInFiles(lang: Lang, config: FindConfig, callback: (err: null | Error, result: SgNode[]) => void): Promise<number>
68
107
  export class SgNode {
69
108
  range(): Range
70
109
  isLeaf(): boolean
package/index.js CHANGED
@@ -281,12 +281,17 @@ if (!nativeBinding) {
281
281
  throw new Error(`Failed to load native binding`)
282
282
  }
283
283
 
284
- const { FrontEndLanguage, SgNode, SgRoot, parseFiles, html, js, jsx, ts, tsx, css } = nativeBinding
284
+ const { parseFiles, Lang, SgNode, SgRoot, parse, parseAsync, kind, pattern, findInFiles, html, js, jsx, ts, tsx, css } = nativeBinding
285
285
 
286
- module.exports.FrontEndLanguage = FrontEndLanguage
286
+ module.exports.parseFiles = parseFiles
287
+ module.exports.Lang = Lang
287
288
  module.exports.SgNode = SgNode
288
289
  module.exports.SgRoot = SgRoot
289
- module.exports.parseFiles = parseFiles
290
+ module.exports.parse = parse
291
+ module.exports.parseAsync = parseAsync
292
+ module.exports.kind = kind
293
+ module.exports.pattern = pattern
294
+ module.exports.findInFiles = findInFiles
290
295
  module.exports.html = html
291
296
  module.exports.js = js
292
297
  module.exports.jsx = jsx
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ast-grep/napi",
3
- "version": "0.22.4",
3
+ "version": "0.22.5",
4
4
  "description": "Search and Rewrite code at large scale using precise AST pattern",
5
5
  "homepage": "https://ast-grep.github.io",
6
6
  "main": "index.js",
@@ -66,13 +66,13 @@
66
66
  }
67
67
  },
68
68
  "optionalDependencies": {
69
- "@ast-grep/napi-win32-x64-msvc": "0.22.4",
70
- "@ast-grep/napi-darwin-x64": "0.22.4",
71
- "@ast-grep/napi-linux-x64-gnu": "0.22.4",
72
- "@ast-grep/napi-win32-ia32-msvc": "0.22.4",
73
- "@ast-grep/napi-darwin-arm64": "0.22.4",
74
- "@ast-grep/napi-win32-arm64-msvc": "0.22.4",
75
- "@ast-grep/napi-linux-arm64-gnu": "0.22.4",
76
- "@ast-grep/napi-linux-x64-musl": "0.22.4"
69
+ "@ast-grep/napi-win32-x64-msvc": "0.22.5",
70
+ "@ast-grep/napi-darwin-x64": "0.22.5",
71
+ "@ast-grep/napi-linux-x64-gnu": "0.22.5",
72
+ "@ast-grep/napi-win32-ia32-msvc": "0.22.5",
73
+ "@ast-grep/napi-darwin-arm64": "0.22.5",
74
+ "@ast-grep/napi-win32-arm64-msvc": "0.22.5",
75
+ "@ast-grep/napi-linux-arm64-gnu": "0.22.5",
76
+ "@ast-grep/napi-linux-x64-musl": "0.22.5"
77
77
  }
78
78
  }