@ast-grep/napi 0.32.3 → 0.33.0

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.
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  //-----Type Only Export!-----//
2
2
  export type { Pos, Edit, Range } from './types/sgnode'
3
3
  export type { NapiConfig, FindConfig, FileOption } from './types/config'
4
+ export type { DynamicLangRegistrations } from './types/registerDynamicLang'
4
5
  // Only Rule here. User can use Rule['pattern'], e.g., to get the type of subfield.
5
6
  export type { Rule } from './types/rule'
6
7
 
@@ -15,5 +16,6 @@ export {
15
16
  pattern,
16
17
  findInFiles,
17
18
  } from './types/api'
19
+ export { registerDynamicLanguage } from './types/registerDynamicLang'
18
20
  // deprecated
19
- export * from './types/deprecated'
21
+ export * from './types/deprecated'
package/index.js CHANGED
@@ -310,7 +310,7 @@ if (!nativeBinding) {
310
310
  throw new Error(`Failed to load native binding`)
311
311
  }
312
312
 
313
- const { parseFiles, Lang, SgNode, SgRoot, parse, parseAsync, kind, pattern, findInFiles, html, js, jsx, ts, tsx, css } = nativeBinding
313
+ const { parseFiles, Lang, SgNode, SgRoot, parse, parseAsync, kind, pattern, findInFiles, registerDynamicLanguage, html, js, jsx, ts, tsx, css } = nativeBinding
314
314
 
315
315
  module.exports.parseFiles = parseFiles
316
316
  module.exports.Lang = Lang
@@ -321,6 +321,7 @@ module.exports.parseAsync = parseAsync
321
321
  module.exports.kind = kind
322
322
  module.exports.pattern = pattern
323
323
  module.exports.findInFiles = findInFiles
324
+ module.exports.registerDynamicLanguage = registerDynamicLanguage
324
325
  module.exports.html = html
325
326
  module.exports.js = js
326
327
  module.exports.jsx = jsx
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ast-grep/napi",
3
- "version": "0.32.3",
3
+ "version": "0.33.0",
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",
@@ -52,7 +52,7 @@
52
52
  "typegen": "ts-node scripts/generateTypes.ts"
53
53
  },
54
54
  "devDependencies": {
55
- "@ast-grep/napi": "0.31.1",
55
+ "@ast-grep/napi": "0.32.3",
56
56
  "@biomejs/biome": "1.9.4",
57
57
  "@napi-rs/cli": "2.18.4",
58
58
  "@types/node": "^22.10.2",
@@ -76,14 +76,14 @@
76
76
  }
77
77
  },
78
78
  "optionalDependencies": {
79
- "@ast-grep/napi-win32-x64-msvc": "0.32.3",
80
- "@ast-grep/napi-darwin-x64": "0.32.3",
81
- "@ast-grep/napi-linux-x64-gnu": "0.32.3",
82
- "@ast-grep/napi-win32-ia32-msvc": "0.32.3",
83
- "@ast-grep/napi-darwin-arm64": "0.32.3",
84
- "@ast-grep/napi-win32-arm64-msvc": "0.32.3",
85
- "@ast-grep/napi-linux-arm64-gnu": "0.32.3",
86
- "@ast-grep/napi-linux-arm64-musl": "0.32.3",
87
- "@ast-grep/napi-linux-x64-musl": "0.32.3"
79
+ "@ast-grep/napi-win32-x64-msvc": "0.33.0",
80
+ "@ast-grep/napi-darwin-x64": "0.33.0",
81
+ "@ast-grep/napi-linux-x64-gnu": "0.33.0",
82
+ "@ast-grep/napi-win32-ia32-msvc": "0.33.0",
83
+ "@ast-grep/napi-darwin-arm64": "0.33.0",
84
+ "@ast-grep/napi-win32-arm64-msvc": "0.33.0",
85
+ "@ast-grep/napi-linux-arm64-gnu": "0.33.0",
86
+ "@ast-grep/napi-linux-arm64-musl": "0.33.0",
87
+ "@ast-grep/napi-linux-x64-musl": "0.33.0"
88
88
  }
89
89
  }
package/types/api.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { SgNode, SgRoot } from './sgnode'
2
2
  import type { NapiConfig, FindConfig, FileOption } from './config'
3
- import type { Lang } from './lang'
3
+ import type { NapiLang } from './lang'
4
4
  import type { NamedKinds, TypesMap } from './staticTypes'
5
5
 
6
6
  export declare function parseFiles<M extends TypesMap>(
@@ -9,7 +9,7 @@ export declare function parseFiles<M extends TypesMap>(
9
9
  ): Promise<number>
10
10
  /** Parse a string to an ast-grep instance */
11
11
  export declare function parse<M extends TypesMap>(
12
- lang: Lang,
12
+ lang: NapiLang,
13
13
  src: string,
14
14
  ): SgRoot<M>
15
15
  /**
@@ -20,17 +20,17 @@ export declare function parse<M extends TypesMap>(
20
20
  * for its default behavior and performance tuning tricks.
21
21
  */
22
22
  export declare function parseAsync<M extends TypesMap>(
23
- lang: Lang,
23
+ lang: NapiLang,
24
24
  src: string,
25
25
  ): Promise<SgRoot<M>>
26
26
  /** Get the `kind` number from its string name. */
27
27
  export declare function kind<M extends TypesMap>(
28
- lang: Lang,
28
+ lang: NapiLang,
29
29
  kindName: NamedKinds<M>,
30
30
  ): number
31
31
  /** Compile a string to ast-grep Pattern. */
32
32
  export declare function pattern<M extends TypesMap>(
33
- lang: Lang,
33
+ lang: NapiLang,
34
34
  pattern: string,
35
35
  ): NapiConfig<M>
36
36
  /**
@@ -40,7 +40,7 @@ export declare function pattern<M extends TypesMap>(
40
40
  * `callback` will receive matching nodes found in a file.
41
41
  */
42
42
  export declare function findInFiles<M extends TypesMap>(
43
- lang: Lang,
43
+ lang: NapiLang,
44
44
  config: FindConfig<M>,
45
45
  callback: (err: null | Error, result: SgNode<M>[]) => void,
46
- ): Promise<number>
46
+ ): Promise<number>
package/types/config.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Rule } from './rule'
2
- import type { Lang } from './lang'
2
+ import type { NapiLang } from './lang'
3
3
  import type { TypesMap } from './staticTypes'
4
4
 
5
5
  /**
@@ -11,8 +11,8 @@ export interface NapiConfig<M extends TypesMap = TypesMap> {
11
11
  rule: Rule<M>
12
12
  /** See https://ast-grep.github.io/guide/rule-config.html#constraints */
13
13
  constraints?: Record<string, Rule<M>>
14
- /** Available languages: html, css, js, jsx, ts, tsx */
15
- language?: Lang
14
+ /** Builtin Language or custom language */
15
+ language?: NapiLang
16
16
  /**
17
17
  * transform is NOT useful in JavaScript. You can use JS code to directly transform the result.
18
18
  * https://ast-grep.github.io/reference/yaml.html#transform
@@ -36,4 +36,4 @@ export interface FindConfig<M extends TypesMap = TypesMap> {
36
36
  * It is slightly different from https://ast-grep.github.io/reference/sgconfig.html#languageglobs
37
37
  */
38
38
  languageGlobs?: Array<string>
39
- }
39
+ }
package/types/lang.d.ts CHANGED
@@ -24,3 +24,7 @@ export enum Lang {
24
24
  Swift = 'Swift',
25
25
  Yaml = 'Yaml',
26
26
  }
27
+
28
+ type CustomLang = string & {}
29
+
30
+ export type NapiLang = Lang | CustomLang
@@ -0,0 +1,27 @@
1
+ /** The JSON object to register dynamic languages */
2
+ interface LangRegistration {
3
+ /** the path to the dynamic library */
4
+ libraryPath: string
5
+ /** the file extensions of the language. e.g. mojo */
6
+ extensions: string[]
7
+ /** the dylib symbol to load ts-language, default is `tree_sitter_{name}` */
8
+ languageSymbol?: string
9
+ /** the meta variable leading character, default is $ */
10
+ metaVarChar?: string
11
+ /**
12
+ * An optional char to replace $ in your pattern.
13
+ * See https://ast-grep.github.io/advanced/custom-language.html#register-language-in-sgconfig-yml
14
+ */
15
+ expandoChar?: string
16
+ }
17
+
18
+ /** A map of language names to their registration information */
19
+ export interface DynamicLangRegistrations {
20
+ [langName: string]: LangRegistration
21
+ }
22
+
23
+ /**
24
+ * @experimental
25
+ * Register dynamic languages. This function should be called exactly once in the program.
26
+ */
27
+ export declare function registerDynamicLanguage(langs: DynamicLangRegistrations): void
@@ -103,4 +103,4 @@ export type Kinds<M extends TypesMap = TypesMap> =
103
103
  export type RootKind<M extends TypesMap> = NoNever<
104
104
  Extract<M[keyof M], { root: true }>['type'],
105
105
  Kinds<M>
106
- >
106
+ >