Skip to content

Commit d0817ef

Browse files
committed
fix tests & add default type for lang generic to support custom
1 parent 3823b66 commit d0817ef

File tree

4 files changed

+116
-8
lines changed

4 files changed

+116
-8
lines changed

crates/napi/__test__/type.spec.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type TypeScriptTypes from '../lang/TypeScript'
44
import { Lang, parse, parseAsync, type SgNode, type SgRoot } from '../index'
55

66
test('test no type annotation', t => {
7-
const sg = parse(Lang.TypeScript, 'a + b')
7+
const sg = parse('TypeScript', 'a + b')
88
// test root kind and field
99
const root = sg.root()
1010
t.is(root.kind(), 'program')
@@ -158,6 +158,53 @@ test('test type argument style', t => {
158158
})
159159
})
160160

161+
test('test infer lang node types', t => {
162+
const sg = parse(Lang.TypeScript, 'a + b')
163+
// test root kind and field
164+
const root = sg.root()
165+
t.is(root.kind(), 'program')
166+
// @ts-expect-error
167+
t.is(root.field('body'), null)
168+
// test child
169+
const child = root.child(0)
170+
t.assert(child !== null)
171+
const childKind = child!.kind()
172+
t.assert(childKind === 'expression_statement')
173+
t.assert(childKind !== ',')
174+
// test parent method
175+
const parent = child!.parent()
176+
t.assert(parent!.kind() === root.kind())
177+
// test find
178+
const sum = root.find({
179+
rule: {
180+
kind: 'binary_expression',
181+
},
182+
})!
183+
t.is(sum.kind(), 'binary_expression')
184+
t.is(sum.field('operator')!.kind(), '+')
185+
186+
// test type refinement
187+
const a = root.find('a')!
188+
t.assert(a.is('identifier'))
189+
if (a.is('identifier')) {
190+
t.assert(a.kind() === 'identifier')
191+
// @ts-expect-error: should refine kind
192+
t.assert(a.kind() !== 'invalid')
193+
// @ts-expect-error: should reject field
194+
t.is(a.field('type_annotation'), null)
195+
}
196+
197+
// test rule kind
198+
t.throws(() => {
199+
root.find({
200+
rule: {
201+
// @ts-expect-error: reject kind
202+
kind: 'notFound',
203+
},
204+
})
205+
})
206+
})
207+
161208
test('subtype alias', async t => {
162209
const sg = await parseAsync<TypeScriptTypes>(
163210
Lang.TypeScript,
@@ -214,4 +261,4 @@ test('subtype alias', async t => {
214261
},
215262
})
216263
t.falsy(wrong)
217-
})
264+
})

crates/napi/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"build:debug": "napi build --no-const-enum --dts ignore.d.ts --platform",
4747
"prepublishOnly": "napi prepublish -t npm --skip-gh-release",
4848
"pretest": "ts-node scripts/generateTypes.ts --test-only",
49+
"posttest": "ts-node scripts/cleanup.ts",
4950
"test": "tsc --noEmit && ava",
5051
"version": "napi version",
5152
"lint": "biome lint --fix && biome format --write",

crates/napi/scripts/cleanup.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { readdir, readFile, unlink, writeFile } from 'node:fs/promises'
2+
import path from 'node:path'
3+
4+
const root = path.resolve(__dirname, '..')
5+
const dirs = {
6+
root,
7+
types: path.join(root, 'types'),
8+
lang: path.join(root, 'lang'),
9+
}
10+
11+
async function cleanup() {
12+
try {
13+
const files = await readdir(dirs.lang)
14+
15+
await Promise.all(
16+
files
17+
.filter(file => file.endsWith('.d.ts'))
18+
.map(async file => {
19+
const filePath = path.join(dirs.lang, file)
20+
await unlink(filePath)
21+
console.log(`Deleted: ${filePath}`)
22+
}),
23+
)
24+
25+
const existingTypesSource = await readFile(
26+
path.join(dirs.types, 'lang.d.ts'),
27+
'utf8',
28+
)
29+
30+
const newSource = existingTypesSource.replace(
31+
/export type LanguageNodeTypes = \{[^{}]*\}/,
32+
'export type LanguageNodeTypes = Record<never, never>',
33+
)
34+
35+
await writeFile(path.join(dirs.types, 'lang.d.ts'), newSource)
36+
} catch (e) {
37+
console.error('Error during cleanup:', e)
38+
throw e
39+
}
40+
}
41+
42+
cleanup().catch(error => {
43+
console.error('Error:', error)
44+
process.exit(1)
45+
})

crates/napi/types/api.d.ts

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

66
export declare function parseFiles<M extends TypesMap>(
77
paths: Array<string> | FileOption,
88
callback: (err: null | Error, result: SgRoot<M>) => void,
99
): Promise<number>
1010
/** Parse a string to an ast-grep instance */
11-
export declare function parse<M extends TypesMap, L extends NapiLang>(
11+
export declare function parse<
12+
M extends TypesMap,
13+
L extends NapiLang = CustomLang,
14+
>(
1215
lang: L,
1316
src: string,
1417
): SgRoot<L extends keyof LanguageNodeTypes ? LanguageNodeTypes[L] : M>
@@ -19,19 +22,28 @@ export declare function parse<M extends TypesMap, L extends NapiLang>(
1922
* Please refer to libuv doc, nodejs' underlying runtime
2023
* for its default behavior and performance tuning tricks.
2124
*/
22-
export declare function parseAsync<M extends TypesMap, L extends NapiLang>(
25+
export declare function parseAsync<
26+
M extends TypesMap,
27+
L extends NapiLang = CustomLang,
28+
>(
2329
lang: L,
2430
src: string,
2531
): Promise<SgRoot<L extends keyof LanguageNodeTypes ? LanguageNodeTypes[L] : M>>
2632
/** Get the `kind` number from its string name. */
27-
export declare function kind<M extends TypesMap, L extends NapiLang>(
33+
export declare function kind<
34+
M extends TypesMap,
35+
L extends NapiLang = CustomLang,
36+
>(
2837
lang: L,
2938
kindName: NamedKinds<
3039
L extends keyof LanguageNodeTypes ? LanguageNodeTypes[L] : M
3140
>,
3241
): number
3342
/** Compile a string to ast-grep Pattern. */
34-
export declare function pattern<M extends TypesMap, L extends NapiLang>(
43+
export declare function pattern<
44+
M extends TypesMap,
45+
L extends NapiLang = CustomLang,
46+
>(
3547
lang: L,
3648
pattern: string,
3749
): Promise<
@@ -43,7 +55,10 @@ export declare function pattern<M extends TypesMap, L extends NapiLang>(
4355
* `config` specifies the file path and matcher.
4456
* `callback` will receive matching nodes found in a file.
4557
*/
46-
export declare function findInFiles<M extends TypesMap, L extends NapiLang>(
58+
export declare function findInFiles<
59+
M extends TypesMap,
60+
L extends NapiLang = CustomLang,
61+
>(
4762
lang: L,
4863
config: FindConfig<
4964
L extends keyof LanguageNodeTypes ? LanguageNodeTypes[L] : M

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