@ast-grep/napi 0.22.3 → 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 +71 -22
  2. package/index.js +8 -3
  3. package/package.json +9 -9
package/index.d.ts CHANGED
@@ -13,33 +13,12 @@ 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
28
- }
29
- export interface Pos {
30
- /** line number starting from 0 */
31
- line: number
32
- /** column number starting from 0 */
33
- column: number
34
- /** byte offset of the position */
35
- index: number
36
- }
37
- export interface Range {
38
- /** starting position of the range */
39
- start: Pos
40
- /** ending position of the range */
41
- end: Pos
42
- }
43
22
  export interface FileOption {
44
23
  paths: Array<string>
45
24
  languageGlobs: Record<string, Array<string>>
@@ -57,6 +36,74 @@ export interface FindConfig {
57
36
  */
58
37
  languageGlobs?: Array<string>
59
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'
63
+ }
64
+ export interface Edit {
65
+ /** The position of the edit */
66
+ position: number
67
+ /** The length of the text to be deleted */
68
+ deletedLength: number
69
+ /** The text to be inserted */
70
+ insertedText: string
71
+ }
72
+ export interface Pos {
73
+ /** line number starting from 0 */
74
+ line: number
75
+ /** column number starting from 0 */
76
+ column: number
77
+ /** byte offset of the position */
78
+ index: number
79
+ }
80
+ export interface Range {
81
+ /** starting position of the range */
82
+ start: Pos
83
+ /** ending position of the range */
84
+ end: Pos
85
+ }
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>
60
107
  export class SgNode {
61
108
  range(): Range
62
109
  isLeaf(): boolean
@@ -87,6 +134,8 @@ export class SgNode {
87
134
  nextAll(): Array<SgNode>
88
135
  prev(): SgNode | null
89
136
  prevAll(): Array<SgNode>
137
+ replace(text: string): Edit
138
+ commitEdits(edits: Array<Edit>): string
90
139
  }
91
140
  /** Represents the parsed tree of code. */
92
141
  export class SgRoot {
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.3",
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.3",
70
- "@ast-grep/napi-darwin-x64": "0.22.3",
71
- "@ast-grep/napi-linux-x64-gnu": "0.22.3",
72
- "@ast-grep/napi-win32-ia32-msvc": "0.22.3",
73
- "@ast-grep/napi-darwin-arm64": "0.22.3",
74
- "@ast-grep/napi-win32-arm64-msvc": "0.22.3",
75
- "@ast-grep/napi-linux-arm64-gnu": "0.22.3",
76
- "@ast-grep/napi-linux-x64-musl": "0.22.3"
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
  }