Skip to content

Commit a4f720d

Browse files
fix: relax parse lang to accept custom lang
1 parent 6b2f1c1 commit a4f720d

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
lines changed

crates/napi/__test__/custom.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ test('test load custom lang', t => {
2323
t.pass('This test is not available on this platform')
2424
return
2525
}
26-
// @ts-expect-error TODO: change type
2726
const sg = parse('myjson', '{"test": 123}')
2827
const root = sg.root()
2928
const node = root.find("123")!

crates/napi/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export type { DynamicLangRegistrations } from './types/registerDynamicLang'
66
export type { Rule } from './types/rule'
77

88
//-----Runtime Value Export!-----//
9-
export { registerDynamicLanguage } from './types/registerDynamicLang'
109
export { SgRoot, SgNode } from './types/sgnode'
1110
export { Lang } from './types/lang'
1211
export {
@@ -17,5 +16,6 @@ export {
1716
pattern,
1817
findInFiles,
1918
} from './types/api'
19+
export { registerDynamicLanguage } from './types/registerDynamicLang'
2020
// deprecated
2121
export * from './types/deprecated'

crates/napi/types/api.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SgNode, SgRoot } from './sgnode'
22
import type { NapiConfig, FindConfig, FileOption } from './config'
3-
import type { Lang } from './lang'
3+
import type { NapiLang } from './lang'
44
import type { NamedKinds, TypesMap } from './staticTypes'
55

66
export declare function parseFiles<M extends TypesMap>(
@@ -9,7 +9,7 @@ export declare function parseFiles<M extends TypesMap>(
99
): Promise<number>
1010
/** Parse a string to an ast-grep instance */
1111
export declare function parse<M extends TypesMap>(
12-
lang: Lang,
12+
lang: NapiLang,
1313
src: string,
1414
): SgRoot<M>
1515
/**
@@ -20,17 +20,17 @@ export declare function parse<M extends TypesMap>(
2020
* for its default behavior and performance tuning tricks.
2121
*/
2222
export declare function parseAsync<M extends TypesMap>(
23-
lang: Lang,
23+
lang: NapiLang,
2424
src: string,
2525
): Promise<SgRoot<M>>
2626
/** Get the `kind` number from its string name. */
2727
export declare function kind<M extends TypesMap>(
28-
lang: Lang,
28+
lang: NapiLang,
2929
kindName: NamedKinds<M>,
3030
): number
3131
/** Compile a string to ast-grep Pattern. */
3232
export declare function pattern<M extends TypesMap>(
33-
lang: Lang,
33+
lang: NapiLang,
3434
pattern: string,
3535
): NapiConfig<M>
3636
/**
@@ -40,7 +40,7 @@ export declare function pattern<M extends TypesMap>(
4040
* `callback` will receive matching nodes found in a file.
4141
*/
4242
export declare function findInFiles<M extends TypesMap>(
43-
lang: Lang,
43+
lang: NapiLang,
4444
config: FindConfig<M>,
4545
callback: (err: null | Error, result: SgNode<M>[]) => void,
46-
): Promise<number>
46+
): Promise<number>

crates/napi/types/config.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Rule } from './rule'
2-
import type { Lang } from './lang'
2+
import type { NapiLang } from './lang'
33
import type { TypesMap } from './staticTypes'
44

55
/**
@@ -11,8 +11,8 @@ export interface NapiConfig<M extends TypesMap = TypesMap> {
1111
rule: Rule<M>
1212
/** See https://ast-grep.github.io/guide/rule-config.html#constraints */
1313
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
1616
/**
1717
* transform is NOT useful in JavaScript. You can use JS code to directly transform the result.
1818
* https://ast-grep.github.io/reference/yaml.html#transform
@@ -36,4 +36,4 @@ export interface FindConfig<M extends TypesMap = TypesMap> {
3636
* It is slightly different from https://ast-grep.github.io/reference/sgconfig.html#languageglobs
3737
*/
3838
languageGlobs?: Array<string>
39-
}
39+
}

crates/napi/types/lang.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ export enum Lang {
2424
Swift = 'Swift',
2525
Yaml = 'Yaml',
2626
}
27+
28+
type CustomLang = string & {}
29+
30+
export type NapiLang = Lang | CustomLang

crates/napi/types/staticTypes.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,4 @@ export type Kinds<M extends TypesMap = TypesMap> =
103103
export type RootKind<M extends TypesMap> = NoNever<
104104
Extract<M[keyof M], { root: true }>['type'],
105105
Kinds<M>
106-
>
106+
>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy