Skip to content

Commit 5960eb8

Browse files
committed
style(ast-grep): fix lint errors
- Add process import from node:process in constants.ts and downloader.ts - Convert capturing groups to non-capturing in utils.ts regex - Add YAML document separators (---) in ast-grep.md examples - Apply auto-fixes for import ordering and code style
1 parent 46ee26f commit 5960eb8

8 files changed

Lines changed: 46 additions & 39 deletions

File tree

agents/ast-grep.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ For complex queries, use YAML rules with operators.
110110
id: rule-name
111111
language: javascript
112112
rule:
113-
# Rule definition here
114-
message: "Explanation of what was found"
113+
# Rule definition here
114+
message: Explanation of what was found
115115
```
116116
117117
### Atomic Rules
@@ -161,7 +161,7 @@ rule:
161161
has:
162162
kind: try_statement
163163
stopBy: end
164-
164+
---
165165
# Find React useState without dependency
166166
id: usestate-in-component
167167
language: tsx
@@ -170,15 +170,15 @@ rule:
170170
inside:
171171
kind: function_declaration
172172
stopBy: end
173-
173+
---
174174
# Find console.log in production code
175175
id: no-console-log
176176
language: javascript
177177
rule:
178178
pattern: console.log($$$)
179179
not:
180180
inside:
181-
regex: "test|spec|__tests__"
181+
regex: test|spec|__tests__
182182
kind: string
183183
```
184184

@@ -197,7 +197,7 @@ rule:
197197
kind: string
198198
nthChild: 1
199199
stopBy: end
200-
200+
---
201201
# Find bare except clauses
202202
id: bare-except
203203
language: python

packages/dora/src/providers/ast-grep/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
* Executes sg CLI commands and parses results
55
*/
66

7+
import type { CliMatch, RunSgOptions, SgResult } from './types'
8+
import { existsSync } from 'node:fs'
79
import { spawn } from 'bun'
8-
import { existsSync } from 'fs'
910
import {
1011
CLI_LANGUAGES,
1112
DEFAULT_MAX_MATCHES,
1213
DEFAULT_MAX_OUTPUT_BYTES,
1314
DEFAULT_TIMEOUT_MS,
1415
} from './constants'
1516
import { ensureAstGrepBinary, getInstallInstructions } from './downloader'
16-
import type { CliMatch, RunSgOptions, SgResult } from './types'
1717

1818
// Cached binary path
1919
let resolvedCliPath: string | null = null

packages/dora/src/providers/ast-grep/constants.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
* Constants for ast-grep provider
33
*/
44

5-
import * as os from 'os'
6-
import * as path from 'path'
75
import type { PlatformConfig, PlatformId } from './types'
6+
import * as os from 'node:os'
7+
import * as path from 'node:path'
8+
import process from 'node:process'
89

910
// CLI supported languages (25 total)
1011
export const CLI_LANGUAGES = [

packages/dora/src/providers/ast-grep/downloader.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
* Caches to ~/.cache/dora/ast-grep/
66
*/
77

8-
import { existsSync, mkdirSync, chmodSync, unlinkSync, writeFileSync, readFileSync } from 'fs'
8+
import type { PlatformId } from './types'
9+
import { chmodSync, existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
10+
import process from 'node:process'
911
import { spawn } from 'bun'
1012
import {
1113
AST_GREP_VERSION,
12-
PLATFORM_CONFIGS,
1314
getAstGrepCacheDir,
1415
getCachedBinaryPath,
1516
getPlatformId,
1617
getVersionMarkerPath,
18+
PLATFORM_CONFIGS,
1719
} from './constants'
18-
import type { PlatformId } from './types'
1920

2021
/**
2122
* Verify a binary is actually ast-grep by checking --version output
@@ -95,8 +96,8 @@ async function extractZip(archivePath: string, destDir: string): Promise<void> {
9596
let proc
9697
if (process.platform === 'win32') {
9798
// Escape single quotes for PowerShell by doubling them
98-
const escapedArchive = archivePath.replace(/'/g, "''")
99-
const escapedDest = destDir.replace(/'/g, "''")
99+
const escapedArchive = archivePath.replace(/'/g, '\'\'')
100+
const escapedDest = destDir.replace(/'/g, '\'\'')
100101
proc = spawn(
101102
[
102103
'powershell',

packages/dora/src/providers/ast-grep/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
* Provides AST-aware code search and transformation tools
55
*/
66

7-
import { z, ZodError } from 'zod'
87
import type { Provider, ToolDefinition, ToolResult } from '../provider'
98
import type { RegistryConfig } from '../registry'
10-
import { CLI_LANGUAGES, NAPI_LANGUAGES } from './constants'
11-
import { runSg, isCliAvailable } from './cli'
12-
import { isNapiAvailable, getNapiError, analyzeCode, transformCode } from './napi'
13-
import { formatSearchResult, formatReplaceResult, formatAnalyzeResult, formatTransformResult, getEmptyResultHint } from './utils'
149
import type { NapiLanguage } from './types'
10+
import { z, ZodError } from 'zod'
11+
import { isCliAvailable, runSg } from './cli'
12+
import { CLI_LANGUAGES, NAPI_LANGUAGES } from './constants'
13+
import { analyzeCode, getNapiError, isNapiAvailable, transformCode } from './napi'
14+
import { formatAnalyzeResult, formatReplaceResult, formatSearchResult, formatTransformResult, getEmptyResultHint } from './utils'
1515

1616
/**
1717
* Format error for tool result, with special handling for Zod validation errors
@@ -157,7 +157,9 @@ export class AstGrepProvider implements Provider {
157157
const napiAvailable = isNapiAvailable()
158158
if (!napiAvailable) {
159159
const error = getNapiError()
160+
160161
console.log(`[ast-grep] NAPI not available: ${error ?? 'unknown'}`)
162+
161163
console.log('[ast-grep] In-memory tools (analyze/transform) disabled')
162164
}
163165

packages/dora/src/providers/ast-grep/napi.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
* Provides faster in-memory analysis and transformation without spawning CLI
66
*/
77

8-
import { NAPI_LANGUAGES } from './constants'
98
import 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
1211
type AstGrepNapiModule = any
13-
// eslint-disable-next-line ts/no-explicit-any -- AST node from NAPI module
12+
1413
type 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
*/
2322
export 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
7072
export 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
9497
export 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 {

packages/dora/src/providers/ast-grep/types.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ export interface Range {
2626
export interface CliMatch {
2727
text: string
2828
range: {
29-
byteOffset: { start: number; end: number }
29+
byteOffset: { start: number, end: number }
3030
start: Position
3131
end: Position
3232
}
3333
file: string
3434
lines: string
35-
charCount: { leading: number; trailing: number }
35+
charCount: { leading: number, trailing: number }
3636
language: string
3737
}
3838

@@ -96,12 +96,12 @@ export interface RunSgOptions {
9696
}
9797

9898
/** Platform identifier */
99-
export type PlatformId =
100-
| 'win-x64'
101-
| 'linux-x64'
102-
| 'linux-arm64'
103-
| 'osx-x64'
104-
| 'osx-arm64'
99+
export type PlatformId
100+
= | 'win-x64'
101+
| 'linux-x64'
102+
| 'linux-arm64'
103+
| 'osx-x64'
104+
| 'osx-arm64'
105105

106106
/** Platform-specific binary configuration */
107107
export interface PlatformConfig {

packages/dora/src/providers/ast-grep/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function getEmptyResultHint(pattern: string, lang: CliLanguage): string |
139139
}
140140

141141
if (['javascript', 'typescript', 'tsx'].includes(lang)) {
142-
if (/^(export\s+)?(async\s+)?function\s+\$[A-Z_]+\s*$/i.test(src)) {
142+
if (/^(?:export\s+)?(?:async\s+)?function\s+\$[A-Z_]+\s*$/i.test(src)) {
143143
return `Hint: Function patterns need params and body. Try "function $NAME($$$) { $$$ }"`
144144
}
145145
}

0 commit comments

Comments
 (0)