@ohos-npm-ports/ast-grep-napi 0.43.0-1

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/package.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "@ohos-npm-ports/ast-grep-napi",
3
+ "version": "0.43.0-1",
4
+ "description": "Search and Rewrite code at large scale using precise AST pattern",
5
+ "homepage": "https://ast-grep.github.io",
6
+ "main": "index.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/ohos-npm-ports/ohos-npm-ports.git",
10
+ "directory": "ports/ast-grep-napi/0.43.0"
11
+ },
12
+ "license": "MIT",
13
+ "keywords": [
14
+ "ast",
15
+ "pattern",
16
+ "codemod",
17
+ "search",
18
+ "rewrite"
19
+ ],
20
+ "files": [
21
+ "index.d.ts",
22
+ "index.js",
23
+ "types/*.ts",
24
+ "lang/*.ts",
25
+ "ast-grep-napi.openharmony-arm64.node"
26
+ ],
27
+ "napi": {
28
+ "binaryName": "ast-grep-napi",
29
+ "targets": [
30
+ "x86_64-unknown-linux-gnu",
31
+ "x86_64-pc-windows-msvc",
32
+ "x86_64-apple-darwin",
33
+ "i686-pc-windows-msvc",
34
+ "aarch64-apple-darwin",
35
+ "aarch64-pc-windows-msvc",
36
+ "aarch64-unknown-linux-gnu",
37
+ "aarch64-unknown-linux-musl",
38
+ "x64-unknown-linux-musl"
39
+ ]
40
+ },
41
+ "engines": {
42
+ "node": ">= 10"
43
+ },
44
+ "publishConfig": {
45
+ "registry": "https://registry.npmjs.org/",
46
+ "access": "public"
47
+ },
48
+ "devDependencies": {
49
+ "@ast-grep/napi": "0.42.3",
50
+ "@napi-rs/cli": "3.6.2",
51
+ "@types/node": "^24.0.0",
52
+ "oxlint": "1.66.0",
53
+ "ava": "7.0.0",
54
+ "chalk": "5.6.2",
55
+ "dprint": "0.54.0",
56
+ "smol-toml": "^1.3.1",
57
+ "tsimp": "2.0.12",
58
+ "typescript": "6.0.3"
59
+ },
60
+ "ava": {
61
+ "nodeArguments": [
62
+ "--import=tsimp"
63
+ ],
64
+ "extensions": {
65
+ "ts": "module"
66
+ },
67
+ "timeout": "2m",
68
+ "workerThreads": false,
69
+ "environmentVariables": {
70
+ "TS_NODE_PROJECT": "./tsconfig.json"
71
+ }
72
+ },
73
+ "optionalDependencies": {
74
+ "@ast-grep/napi-linux-x64-gnu": "0.43.0",
75
+ "@ast-grep/napi-win32-x64-msvc": "0.43.0",
76
+ "@ast-grep/napi-darwin-x64": "0.43.0",
77
+ "@ast-grep/napi-win32-ia32-msvc": "0.43.0",
78
+ "@ast-grep/napi-darwin-arm64": "0.43.0",
79
+ "@ast-grep/napi-win32-arm64-msvc": "0.43.0",
80
+ "@ast-grep/napi-linux-arm64-gnu": "0.43.0",
81
+ "@ast-grep/napi-linux-arm64-musl": "0.43.0",
82
+ "@ast-grep/napi-linux-x64-musl": "0.43.0"
83
+ }
84
+ }
package/types/api.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ import type { FileOption, FindConfig, NapiConfig } from './config'
2
+ import type { NapiLang } from './lang'
3
+ import type { SgNode, SgRoot } from './sgnode'
4
+ import type { NamedKinds, TypesMap } from './staticTypes'
5
+
6
+ export declare function parseFiles<M extends TypesMap>(
7
+ paths: Array<string> | FileOption,
8
+ callback: (err: null | Error, result: SgRoot<M>) => void,
9
+ ): Promise<number>
10
+ /** Parse a string to an ast-grep instance */
11
+ export declare function parse<M extends TypesMap>(
12
+ lang: NapiLang,
13
+ src: string,
14
+ ): SgRoot<M>
15
+ /**
16
+ * Parse a string to an ast-grep instance asynchronously in threads.
17
+ * It utilize multiple CPU cores when **concurrent processing sources**.
18
+ * However, spawning excessive many threads may backfire.
19
+ * Please refer to libuv doc, nodejs' underlying runtime
20
+ * for its default behavior and performance tuning tricks.
21
+ */
22
+ export declare function parseAsync<M extends TypesMap>(
23
+ lang: NapiLang,
24
+ src: string,
25
+ ): Promise<SgRoot<M>>
26
+ /** Get the `kind` number from its string name. */
27
+ export declare function kind<M extends TypesMap>(
28
+ lang: NapiLang,
29
+ kindName: NamedKinds<M>,
30
+ ): number
31
+ /** Compile a string to ast-grep Pattern. */
32
+ export declare function pattern<M extends TypesMap>(
33
+ lang: NapiLang,
34
+ pattern: string,
35
+ ): NapiConfig<M>
36
+ /**
37
+ * Discover and parse multiple files in Rust.
38
+ * `lang` specifies the language.
39
+ * `config` specifies the file path and matcher.
40
+ * `callback` will receive matching nodes found in a file.
41
+ */
42
+ export declare function findInFiles<M extends TypesMap>(
43
+ lang: NapiLang,
44
+ config: FindConfig<M>,
45
+ callback: (err: null | Error, result: SgNode<M>[]) => void,
46
+ ): Promise<number>
@@ -0,0 +1,39 @@
1
+ import type { NapiLang } from './lang'
2
+ import type { Rule } from './rule'
3
+ import type { TypesMap } from './staticTypes'
4
+
5
+ /**
6
+ * Rule configuration similar to YAML
7
+ * See https://ast-grep.github.io/reference/yaml.html
8
+ */
9
+ export interface NapiConfig<M extends TypesMap = TypesMap> {
10
+ /** The rule object, see https://ast-grep.github.io/reference/rule.html */
11
+ rule: Rule<M>
12
+ /** See https://ast-grep.github.io/guide/rule-config.html#constraints */
13
+ constraints?: Record<string, Rule<M>>
14
+ /** Builtin Language or custom language */
15
+ language?: NapiLang
16
+ /**
17
+ * transform is NOT useful in JavaScript. You can use JS code to directly transform the result.
18
+ * https://ast-grep.github.io/reference/yaml.html#transform
19
+ */
20
+ transform?: unknown
21
+ /** https://ast-grep.github.io/guide/rule-config/utility-rule.html */
22
+ utils?: Record<string, Rule<M>>
23
+ }
24
+ export interface FileOption {
25
+ paths: Array<string>
26
+ languageGlobs: Record<string, Array<string>>
27
+ }
28
+ export interface FindConfig<M extends TypesMap = TypesMap> {
29
+ /** specify the file paths to recursively find files */
30
+ paths: Array<string>
31
+ /** a Rule object to find what nodes will match */
32
+ matcher: NapiConfig<M>
33
+ /**
34
+ * An list of pattern globs to treat of certain files in the specified language.
35
+ * eg. ['*.vue', '*.svelte'] for html.findFiles, or ['*.ts'] for tsx.findFiles.
36
+ * It is slightly different from https://ast-grep.github.io/reference/sgconfig.html#languageglobs
37
+ */
38
+ languageGlobs?: Array<string>
39
+ }
@@ -0,0 +1,117 @@
1
+ import type { FindConfig, NapiConfig } from './config'
2
+ import type { SgNode, SgRoot } from './sgnode'
3
+
4
+ /**
5
+ * @deprecated language specific objects are deprecated
6
+ * use the equivalent functions like `parse` in @ast-grep/napi
7
+ */
8
+ export declare namespace html {
9
+ /** @deprecated use `parse(Lang.Html, src)` instead */
10
+ export function parse(src: string): SgRoot
11
+ /** @deprecated use `parseAsync(Lang.Html, src)` instead */
12
+ export function parseAsync(src: string): Promise<SgRoot>
13
+ /** @deprecated use `kind(Lang.Html, kindName)` instead */
14
+ export function kind(kindName: string): number
15
+ /** @deprecated use `pattern(Lang.Html, p)` instead */
16
+ export function pattern(pattern: string): NapiConfig
17
+ /** @deprecated use `findInFiles(Lang.Html, config, callback)` instead */
18
+ export function findInFiles(
19
+ config: FindConfig,
20
+ callback: (err: null | Error, result: SgNode[]) => void,
21
+ ): Promise<number>
22
+ }
23
+ /**
24
+ * @deprecated language specific objects are deprecated
25
+ * use the equivalent functions like `parse` in @ast-grep/napi
26
+ */
27
+ export declare namespace js {
28
+ /** @deprecated use `parse(Lang.JavaScript, src)` instead */
29
+ export function parse(src: string): SgRoot
30
+ /** @deprecated use `parseAsync(Lang.JavaScript, src)` instead */
31
+ export function parseAsync(src: string): Promise<SgRoot>
32
+ /** @deprecated use `kind(Lang.JavaScript, kindName)` instead */
33
+ export function kind(kindName: string): number
34
+ /** @deprecated use `pattern(Lang.JavaScript, p)` instead */
35
+ export function pattern(pattern: string): NapiConfig
36
+ /** @deprecated use `findInFiles(Lang.JavaScript, config, callback)` instead */
37
+ export function findInFiles(
38
+ config: FindConfig,
39
+ callback: (err: null | Error, result: SgNode[]) => void,
40
+ ): Promise<number>
41
+ }
42
+ /**
43
+ * @deprecated language specific objects are deprecated
44
+ * use the equivalent functions like `parse` in @ast-grep/napi
45
+ */
46
+ export declare namespace jsx {
47
+ /** @deprecated use `parse(Lang.JavaScript, src)` instead */
48
+ export function parse(src: string): SgRoot
49
+ /** @deprecated use `parseAsync(Lang.JavaScript, src)` instead */
50
+ export function parseAsync(src: string): Promise<SgRoot>
51
+ /** @deprecated use `kind(Lang.JavaScript, kindName)` instead */
52
+ export function kind(kindName: string): number
53
+ /** @deprecated use `pattern(Lang.JavaScript, p)` instead */
54
+ export function pattern(pattern: string): NapiConfig
55
+ /** @deprecated use `findInFiles(Lang.JavaScript, config, callback)` instead */
56
+ export function findInFiles(
57
+ config: FindConfig,
58
+ callback: (err: null | Error, result: SgNode[]) => void,
59
+ ): Promise<number>
60
+ }
61
+ /**
62
+ * @deprecated language specific objects are deprecated
63
+ * use the equivalent functions like `parse` in @ast-grep/napi
64
+ */
65
+ export declare namespace ts {
66
+ /** @deprecated use `parse(Lang.TypeScript, src)` instead */
67
+ export function parse(src: string): SgRoot
68
+ /** @deprecated use `parseAsync(Lang.TypeScript, src)` instead */
69
+ export function parseAsync(src: string): Promise<SgRoot>
70
+ /** @deprecated use `kind(Lang.TypeScript, kindName)` instead */
71
+ export function kind(kindName: string): number
72
+ /** @deprecated use `pattern(Lang.TypeScript, p)` instead */
73
+ export function pattern(pattern: string): NapiConfig
74
+ /** @deprecated use `findInFiles(Lang.TypeScript, config, callback)` instead */
75
+ export function findInFiles(
76
+ config: FindConfig,
77
+ callback: (err: null | Error, result: SgNode[]) => void,
78
+ ): Promise<number>
79
+ }
80
+ /**
81
+ * @deprecated language specific objects are deprecated
82
+ * use the equivalent functions like `parse` in @ast-grep/napi
83
+ */
84
+ export declare namespace tsx {
85
+ /** @deprecated use `parse(Lang.Tsx, src)` instead */
86
+ export function parse(src: string): SgRoot
87
+ /** @deprecated use `parseAsync(Lang.Tsx, src)` instead */
88
+ export function parseAsync(src: string): Promise<SgRoot>
89
+ /** @deprecated use `kind(Lang.Tsx, kindName)` instead */
90
+ export function kind(kindName: string): number
91
+ /** @deprecated use `pattern(Lang.Tsx, p)` instead */
92
+ export function pattern(pattern: string): NapiConfig
93
+ /** @deprecated use `findInFiles(Lang.Tsx, config, callback)` instead */
94
+ export function findInFiles(
95
+ config: FindConfig,
96
+ callback: (err: null | Error, result: SgNode[]) => void,
97
+ ): Promise<number>
98
+ }
99
+ /**
100
+ * @deprecated language specific objects are deprecated
101
+ * use the equivalent functions like `parse` in @ast-grep/napi
102
+ */
103
+ export declare namespace css {
104
+ /** @deprecated use `parse(Lang.Css, src)` instead */
105
+ export function parse(src: string): SgRoot
106
+ /** @deprecated use `parseAsync(Lang.Css, src)` instead */
107
+ export function parseAsync(src: string): Promise<SgRoot>
108
+ /** @deprecated use `kind(Lang.Css, kindName)` instead */
109
+ export function kind(kindName: string): number
110
+ /** @deprecated use `pattern(Lang.Css, p)` instead */
111
+ export function pattern(pattern: string): NapiConfig
112
+ /** @deprecated use `findInFiles(Lang.Css, config, callback)` instead */
113
+ export function findInFiles(
114
+ config: FindConfig,
115
+ callback: (err: null | Error, result: SgNode[]) => void,
116
+ ): Promise<number>
117
+ }
@@ -0,0 +1,11 @@
1
+ export enum Lang {
2
+ Html = 'Html',
3
+ JavaScript = 'JavaScript',
4
+ Tsx = 'Tsx',
5
+ Css = 'Css',
6
+ TypeScript = 'TypeScript',
7
+ }
8
+
9
+ type CustomLang = string & {}
10
+
11
+ 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
@@ -0,0 +1,105 @@
1
+ import type { NamedKinds, TypesMap } from './staticTypes'
2
+
3
+ export type Strictness = 'cst' | 'smart' | 'ast' | 'relaxed' | 'signature'
4
+
5
+ export interface PatternObject<M extends TypesMap = TypesMap> {
6
+ context: string
7
+ selector?: NamedKinds<M> // only named node types
8
+ strictness?: Strictness
9
+ }
10
+
11
+ export type PatternStyle<M extends TypesMap = TypesMap> =
12
+ | string
13
+ | PatternObject<M>
14
+
15
+ export interface Relation<M extends TypesMap = TypesMap> extends Rule<M> {
16
+ /**
17
+ * Specify how relational rule will stop relative to the target node.
18
+ */
19
+ stopBy?: 'neighbor' | 'end' | Rule<M>
20
+ /** Specify the tree-sitter field in parent node. Only available in has/inside rule. */
21
+ field?: string
22
+ }
23
+
24
+ export interface NthChildObject<M extends TypesMap = TypesMap> {
25
+ /** The position in nodes' sibling list. It can be a number of An+B string */
26
+ position: string | number
27
+ ofRule?: Rule<M>
28
+ reverse?: boolean
29
+ }
30
+
31
+ /**
32
+ * NthChild can have these types:
33
+ * * number: the position of the node in the sibling list.
34
+ * * string: An + B style string like CSS nth-child selector.
35
+ * * object: An object with `position` and `ofRule` fields.
36
+ */
37
+ export type NthChild<M extends TypesMap = TypesMap> =
38
+ | number
39
+ | string
40
+ | NthChildObject<M>
41
+
42
+ export interface Position {
43
+ /** 0-indexed line number. */
44
+ line: number
45
+ /** 0-indexed column number. */
46
+ column: number
47
+ }
48
+
49
+ export interface Range {
50
+ start: Position
51
+ end: Position
52
+ }
53
+
54
+ export type UtilityCall<M extends TypesMap = TypesMap> = Record<
55
+ string,
56
+ Record<string, Rule<M>>
57
+ >
58
+
59
+ export type Matches<M extends TypesMap = TypesMap> = string | UtilityCall<M>
60
+
61
+ export interface Rule<M extends TypesMap = TypesMap> {
62
+ /** A pattern string or a pattern object. */
63
+ pattern?: PatternStyle<M>
64
+ /** The kind name of the node to match. You can look up code's kind names in playground. */
65
+ kind?: NamedKinds<M>
66
+ /** The exact range of the node in the source code. */
67
+ range?: Range
68
+ /** A Rust regular expression to match the node's text. https://docs.rs/regex/latest/regex/#syntax */
69
+ regex?: string
70
+ /**
71
+ * `nthChild` accepts number, string or object.
72
+ * It specifies the position in nodes' sibling list. */
73
+ nthChild?: NthChild<M>
74
+
75
+ // relational
76
+ /**
77
+ * `inside` accepts a relational rule object.
78
+ * the target node must appear inside of another node matching the `inside` sub-rule. */
79
+ inside?: Relation<M>
80
+ /**
81
+ * `has` accepts a relational rule object.
82
+ * the target node must has a descendant node matching the `has` sub-rule. */
83
+ has?: Relation<M>
84
+ /**
85
+ * `precedes` accepts a relational rule object.
86
+ * the target node must appear before another node matching the `precedes` sub-rule. */
87
+ precedes?: Relation<M>
88
+ /**
89
+ * `follows` accepts a relational rule object.
90
+ * the target node must appear after another node matching the `follows` sub-rule. */
91
+ follows?: Relation<M>
92
+ // composite
93
+ /**
94
+ * A list of sub rules and matches a node if all of sub rules match.
95
+ * The meta variables of the matched node contain all variables from the sub-rules. */
96
+ all?: Array<Rule<M>>
97
+ /**
98
+ * A list of sub rules and matches a node if any of sub rules match.
99
+ * The meta variables of the matched node only contain those of the matched sub-rule. */
100
+ any?: Array<Rule<M>>
101
+ /** A single sub-rule and matches a node if the sub rule does not match. */
102
+ not?: Rule<M>
103
+ /** A utility rule id or parameterized utility call and matches a node if that utility matches. */
104
+ matches?: Matches<M>
105
+ }
@@ -0,0 +1,126 @@
1
+ import type { NapiConfig } from './config'
2
+ import type {
3
+ ChildKinds,
4
+ ExtractField,
5
+ FieldNames,
6
+ Kinds,
7
+ NamedChildKinds,
8
+ NamedKinds,
9
+ NodeFieldInfo,
10
+ RootKind,
11
+ TypesInField,
12
+ TypesMap,
13
+ } from './staticTypes'
14
+
15
+ export interface Edit {
16
+ /** The start position of the edit */
17
+ startPos: number
18
+ /** The end position of the edit */
19
+ endPos: number
20
+ /** The text to be inserted */
21
+ insertedText: string
22
+ }
23
+ export interface Pos {
24
+ /** line number starting from 0 */
25
+ line: number
26
+ /** column number starting from 0 */
27
+ column: number
28
+ /** byte offset of the position */
29
+ index: number
30
+ }
31
+ export interface Range {
32
+ /** starting position of the range */
33
+ start: Pos
34
+ /** ending position of the range */
35
+ end: Pos
36
+ }
37
+
38
+ export declare class SgNode<
39
+ M extends TypesMap = TypesMap,
40
+ out T extends Kinds<M> = Kinds<M>,
41
+ > {
42
+ /** Returns the node's id */
43
+ id(): number
44
+ range(): Range
45
+ isLeaf(): boolean
46
+ isNamed(): boolean
47
+ isNamedLeaf(): boolean
48
+ text(): string
49
+ matches(m: string | number | NapiConfig<M>): boolean
50
+ inside(m: string | number | NapiConfig<M>): boolean
51
+ has(m: string | number | NapiConfig<M>): boolean
52
+ precedes(m: string | number | NapiConfig<M>): boolean
53
+ follows(m: string | number | NapiConfig<M>): boolean
54
+ /** Returns the string name of the node kind */
55
+ kind(): T
56
+ readonly kindToRefine: T
57
+ /** Check if the node is the same kind as the given `kind` string */
58
+ is<K extends T>(kind: K): this is SgNode<M, K>
59
+ // we need this override to allow string literal union
60
+ is(kind: string): boolean
61
+
62
+ getMatch: NodeMethod<M, [mv: string]>
63
+ getMultipleMatches(m: string): Array<SgNode<M>>
64
+ getTransformed(m: string): string | null
65
+ /** Returns the node's SgRoot */
66
+ getRoot(): SgRoot<M>
67
+ children(): Array<SgNode<M>>
68
+ find: NodeMethod<M, [matcher: string | number | NapiConfig<M>]>
69
+ findAll<K extends Kinds<M>>(
70
+ matcher: string | number | NapiConfig<M>,
71
+ ): Array<RefineNode<M, K>>
72
+ /** Finds the first child node in the `field` */
73
+ field<F extends FieldNames<M[T]>>(name: F): FieldNode<M, T, F>
74
+ /** Finds all the children nodes in the `field` */
75
+ fieldChildren<F extends FieldNames<M[T]>>(
76
+ name: F,
77
+ ): Exclude<FieldNode<M, T, F>, null>[]
78
+ parent: NodeMethod<M>
79
+ child(nth: number): SgNode<M, ChildKinds<M, T>> | null
80
+ child<K extends NamedChildKinds<M, T>>(nth: number): RefineNode<M, K> | null
81
+ ancestors(): Array<SgNode<M>>
82
+ next: NodeMethod<M>
83
+ nextAll(): Array<SgNode<M>>
84
+ prev: NodeMethod<M>
85
+ prevAll(): Array<SgNode<M>>
86
+ replace(text: string): Edit
87
+ commitEdits(edits: Array<Edit>): string
88
+ }
89
+ /** Represents the parsed tree of code. */
90
+ export declare class SgRoot<M extends TypesMap = TypesMap> {
91
+ /** Returns the root SgNode of the ast-grep instance. */
92
+ root(): SgNode<M, RootKind<M>>
93
+ /**
94
+ * Returns the path of the file if it is discovered by ast-grep's `findInFiles`.
95
+ * Returns `"anonymous"` if the instance is created by `lang.parse(source)`.
96
+ */
97
+ filename(): string
98
+ }
99
+
100
+ interface NodeMethod<M extends TypesMap, Args extends unknown[] = []> {
101
+ (...args: Args): SgNode<M> | null
102
+ <K extends NamedKinds<M>>(...args: Args): RefineNode<M, K> | null
103
+ }
104
+
105
+ /**
106
+ * if K contains string, return general SgNode. Otherwise,
107
+ * if K is a literal union, return a union of SgNode of each kind.
108
+ */
109
+ type RefineNode<M extends TypesMap, K> = string extends K ? SgNode<M>
110
+ : K extends Kinds<M> ? SgNode<M, K>
111
+ : never
112
+
113
+ /**
114
+ * return the SgNode of the field in the node.
115
+ */
116
+ // F extends string is used to prevent noisy TS hover info
117
+ type FieldNode<
118
+ M extends TypesMap,
119
+ K extends Kinds<M>,
120
+ F extends FieldNames<M[K]>,
121
+ > = F extends string ? FieldNodeImpl<M, ExtractField<M[K], F>> : never
122
+
123
+ type FieldNodeImpl<M extends TypesMap, I extends NodeFieldInfo> = I extends {
124
+ required: true
125
+ } ? RefineNode<M, TypesInField<M, I>>
126
+ : RefineNode<M, TypesInField<M, I>> | null
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Reference
3
+ * https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
4
+ * Rust CLI Impl
5
+ * https://github.com/tree-sitter/tree-sitter/blob/f279d10aa2aca37c0004d84b2261685739f3cab8/cli/generate/src/node_types.rs#L35-L47
6
+ */
7
+
8
+ export interface NodeBasicInfo {
9
+ type: string
10
+ named: boolean
11
+ }
12
+
13
+ export interface NodeFieldInfo {
14
+ multiple: boolean
15
+ required: boolean
16
+ types: NodeBasicInfo[]
17
+ }
18
+
19
+ export interface NodeType extends NodeBasicInfo {
20
+ root?: boolean
21
+ fields?: {
22
+ [fieldName: string]: NodeFieldInfo
23
+ }
24
+ children?: NodeFieldInfo
25
+ subtypes?: NodeBasicInfo[]
26
+ }
27
+
28
+ /**
29
+ * A map of key to NodeType.
30
+ * Note, the key is not necessary node's kind.
31
+ * it can be a rule representing a category of syntax nodes
32
+ * (e.g. “expression”, “type”, “declaration”).
33
+ * See reference above for more details.
34
+ */
35
+ export interface TypesMap {
36
+ [key: string]: NodeType
37
+ }
38
+
39
+ export type FieldNames<N extends NodeType> = N extends { fields: infer F } ? keyof F
40
+ : string
41
+
42
+ export type ExtractField<
43
+ N extends NodeType,
44
+ F extends FieldNames<N>,
45
+ > = N['fields'] extends Record<F, NodeFieldInfo> ? N['fields'][F]
46
+ : NodeFieldInfo
47
+
48
+ // in case of empty types array, return string as fallback
49
+ type NoNever<T, Fallback> = [T] extends [never] ? Fallback : T
50
+
51
+ export type TypesInField<M extends TypesMap, I extends NodeFieldInfo> = NoNever<
52
+ ResolveType<M, I['types'][number]['type']>,
53
+ Kinds<M>
54
+ >
55
+
56
+ export type NamedChildKinds<
57
+ M extends TypesMap,
58
+ T extends Kinds<M>,
59
+ > = M[T] extends { children: infer C extends NodeFieldInfo } ? TypesInField<M, C>
60
+ : NamedKinds<M>
61
+ export type ChildKinds<M extends TypesMap, T extends Kinds<M>> =
62
+ | NamedChildKinds<M, T>
63
+ | LowPriorityKey
64
+
65
+ /**
66
+ * resolve subtypes alias. see tree-sitter's reference
67
+ * e.g. like `expression` => `binary_expression` | `unary_expression` | ...
68
+ */
69
+ type ResolveType<M extends TypesMap, K> = K extends keyof M
70
+ ? M[K] extends { subtypes: infer S extends NodeBasicInfo[] } ? ResolveType<M, S[number]['type']>
71
+ : K
72
+ : K
73
+
74
+ /**
75
+ * All named nodes' kinds that are usable in ast-grep rule
76
+ * NOTE: SgNode can return kind not in this list
77
+ */
78
+ export type NamedKinds<M extends TypesMap> = ResolveType<M, keyof M>
79
+
80
+ /**
81
+ * See open-ended unions / string literal completion in TypeScript
82
+ * https://github.com/microsoft/TypeScript/issues/26277
83
+ * https://github.com/microsoft/TypeScript/issues/33471
84
+ */
85
+ type LowPriorityKey = string & {}
86
+
87
+ /**
88
+ * A union of all named node kinds and a low priority key
89
+ * tree-sitter Kinds also include unnamed nodes which is not usable in rule
90
+ * NOTE: SgNode can return a string type if it is not a named node
91
+ */
92
+ export type Kinds<M extends TypesMap = TypesMap> =
93
+ | NamedKinds<M>
94
+ | LowPriorityKey
95
+
96
+ /**
97
+ * The root node kind of the tree.
98
+ */
99
+ export type RootKind<M extends TypesMap> = NoNever<
100
+ Extract<M[keyof M], { root: true }>['type'],
101
+ Kinds<M>
102
+ >