55 * Provides faster in-memory analysis and transformation without spawning CLI
66 */
77
8- import { NAPI_LANGUAGES } from './constants'
98import type { AnalyzeResult , MetaVariable , NapiLanguage , Range } from './types'
9+ import { NAPI_LANGUAGES } from './constants'
1010
11- // eslint-disable-next-line ts/no-explicit-any -- Runtime-loaded optional dependency
1211type AstGrepNapiModule = any
13- // eslint-disable-next-line ts/no-explicit-any -- AST node from NAPI module
12+
1413type SgNode = any
1514
1615// Dynamic import for @ast -grep/napi (optional dependency)
@@ -21,8 +20,10 @@ let napiLoadError: Error | null = null
2120 * Check if NAPI is available
2221 */
2322export function isNapiAvailable ( ) : boolean {
24- if ( napiModule !== null ) return true
25- if ( napiLoadError !== null ) return false
23+ if ( napiModule !== null )
24+ return true
25+ if ( napiLoadError !== null )
26+ return false
2627
2728 try {
2829 // eslint-disable-next-line ts/no-require-imports
@@ -67,20 +68,21 @@ function getLangEnum(lang: NapiLanguage): unknown {
6768/**
6869 * Parse code using NAPI
6970 */
71+ // eslint-disable-next-line ts/explicit-function-return-type
7072export function parseCode ( code : string , lang : NapiLanguage ) {
7173 if ( ! isNapiAvailable ( ) ) {
7274 throw new Error (
7375 `@ast-grep/napi not available: ${ napiLoadError ?. message ?? 'unknown error' } \n`
74- + 'Install with: bun add -D @ast-grep/napi' ,
76+ + 'Install with: bun add -D @ast-grep/napi' ,
7577 )
7678 }
7779
7880 if ( ! NAPI_LANGUAGES . includes ( lang ) ) {
7981 const supportedLangs = NAPI_LANGUAGES . join ( ', ' )
8082 throw new Error (
8183 `Unsupported language for NAPI: "${ lang } "\n`
82- + `Supported languages: ${ supportedLangs } \n\n`
83- + `Use ast_grep_search for other languages (25 supported via CLI).` ,
84+ + `Supported languages: ${ supportedLangs } \n\n`
85+ + `Use ast_grep_search for other languages (25 supported via CLI).` ,
8486 )
8587 }
8688
@@ -91,6 +93,7 @@ export function parseCode(code: string, lang: NapiLanguage) {
9193/**
9294 * Find pattern matches in parsed tree
9395 */
96+ // eslint-disable-next-line ts/explicit-function-return-type
9497export function findPattern ( root : ReturnType < typeof parseCode > , pattern : string ) {
9598 return root . root ( ) . findAll ( pattern )
9699}
@@ -166,7 +169,7 @@ export function transformCode(
166169 lang : NapiLanguage ,
167170 pattern : string ,
168171 rewrite : string ,
169- ) : { transformed : string ; editCount : number } {
172+ ) : { transformed : string , editCount : number } {
170173 const root = parseCode ( code , lang )
171174 const matches = findPattern ( root , pattern )
172175
@@ -192,7 +195,7 @@ export function transformCode(
192195/**
193196 * Get root node info for debugging
194197 */
195- export function getRootInfo ( code : string , lang : NapiLanguage ) : { kind : string ; childCount : number } {
198+ export function getRootInfo ( code : string , lang : NapiLanguage ) : { kind : string , childCount : number } {
196199 const root = parseCode ( code , lang )
197200 const rootNode = root . root ( )
198201 return {
0 commit comments