refactor(packages): extract ast-grep-core from ast-grep-mcp

This commit is contained in:
YeonGyu-Kim
2026-05-21 01:17:17 +09:00
parent a5c1d71001
commit 3b303e79f4
20 changed files with 608 additions and 421 deletions
+19
View File
@@ -0,0 +1,19 @@
export { CLI_LANGUAGES, DEFAULT_MAX_MATCHES, DEFAULT_MAX_OUTPUT_BYTES, DEFAULT_TIMEOUT_MS } from "./language-support"
export { getPatternHint, detectLanguageSpecificMistake, detectRegexMisuse } from "./pattern-hints"
export { formatReplaceResult, formatSearchResult } from "./result-formatter"
export { createSgResultFromStdout } from "./sg-compact-json-output"
export { buildSgArgs, runSg } from "./runner"
export type {
CliLanguage,
CliMatch,
Position,
Range,
SgResult,
} from "./types"
export type {
SgRunArgs,
SgRunnerDeps,
SpawnOptions,
SpawnProcess,
SpawnResult,
} from "./runner"
@@ -0,0 +1,31 @@
export const CLI_LANGUAGES = [
"bash",
"c",
"cpp",
"csharp",
"css",
"elixir",
"go",
"haskell",
"html",
"java",
"javascript",
"json",
"kotlin",
"lua",
"nix",
"php",
"python",
"ruby",
"rust",
"scala",
"solidity",
"swift",
"typescript",
"tsx",
"yaml",
] as const
export const DEFAULT_TIMEOUT_MS = 300_000
export const DEFAULT_MAX_OUTPUT_BYTES = 1 * 1024 * 1024
export const DEFAULT_MAX_MATCHES = 500
@@ -0,0 +1,63 @@
import type { CliLanguage } from "./types"
export function detectRegexMisuse(pattern: string): string | null {
const src = pattern.trim()
if (/\\[wWdDsSbB]/.test(src)) {
return 'Hint: "\\w", "\\d", "\\s", "\\b" are regex escapes. ast-grep matches AST nodes, not text - use $VAR for identifiers, $$$ for node lists, or switch to grep for text search.'
}
if (/\[[a-zA-Z0-9]-[a-zA-Z0-9]\]/.test(src)) {
return 'Hint: "[a-z]" and similar character classes are regex, not AST. Use $VAR to match any identifier, or switch to grep for text search.'
}
if (!src.includes("$") && /\w\.[*+]/.test(src)) {
return 'Hint: ".*" and ".+" are regex wildcards. In ast-grep use $$$ for multiple AST nodes and $VAR for a single node. For text patterns, switch to grep.'
}
if (/^[-\w.*]+\|[-\w.*|]+$/.test(src)) {
return 'Hint: "|" is regex alternation and does NOT work in ast-grep patterns. Options: (a) fire one ast_grep_search per alternative, or (b) switch to grep with a regex pattern like "foo|bar".'
}
return null
}
export function detectLanguageSpecificMistake(
pattern: string,
lang: CliLanguage,
): string | null {
const src = pattern.trim()
if (lang === "python") {
if (src.startsWith("class ") && src.endsWith(":")) {
return `Hint: Remove trailing colon. Try: "${src.slice(0, -1)}"`
}
if ((src.startsWith("def ") || src.startsWith("async def ")) && src.endsWith(":")) {
return `Hint: Remove trailing colon. Try: "${src.slice(0, -1)}"`
}
}
if (["javascript", "typescript", "tsx"].includes(lang)) {
if (/^(export\s+)?(async\s+)?function\s+\$[A-Z_]+\s*$/i.test(src)) {
return 'Hint: Function patterns need params and body. Try "function $NAME($$$) { $$$ }"'
}
}
if (lang === "go") {
if (/^func\s+\$[A-Z_]+\s*$/i.test(src)) {
return 'Hint: Go function patterns need params and body. Try "func $NAME($$$) { $$$ }"'
}
}
if (lang === "rust") {
if (/^fn\s+\$[A-Z_]+\s*$/i.test(src)) {
return 'Hint: Rust fn patterns need params and body. Try "fn $NAME($$$) { $$$ }"'
}
}
return null
}
export function getPatternHint(pattern: string, lang: CliLanguage): string | null {
return detectRegexMisuse(pattern) ?? detectLanguageSpecificMistake(pattern, lang)
}
@@ -0,0 +1,70 @@
import type { SgResult } from "./types"
export function formatSearchResult(result: SgResult): string {
if (result.error) {
return `Error: ${result.error}`
}
if (result.matches.length === 0) {
return "No matches found"
}
const lines: string[] = []
if (result.truncated) {
const reason = result.truncatedReason === "max_matches"
? `showing first ${result.matches.length} of ${result.totalMatches}`
: result.truncatedReason === "max_output_bytes"
? "output exceeded 1MB limit"
: "search timed out"
lines.push(`[TRUNCATED] Results truncated (${reason})\n`)
}
lines.push(`Found ${result.matches.length} match(es)${result.truncated ? ` (truncated from ${result.totalMatches})` : ""}:\n`)
for (const match of result.matches) {
const loc = `${match.file}:${match.range.start.line + 1}:${match.range.start.column + 1}`
lines.push(`${loc}`)
lines.push(` ${match.lines.trim()}`)
lines.push("")
}
return lines.join("\n")
}
export function formatReplaceResult(result: SgResult, isDryRun: boolean): string {
if (result.error) {
return `Error: ${result.error}`
}
if (result.matches.length === 0) {
return "No matches found to replace"
}
const prefix = isDryRun ? "[DRY RUN] " : ""
const lines: string[] = []
if (result.truncated) {
const reason = result.truncatedReason === "max_matches"
? `showing first ${result.matches.length} of ${result.totalMatches}`
: result.truncatedReason === "max_output_bytes"
? "output exceeded 1MB limit"
: "search timed out"
lines.push(`[TRUNCATED] Results truncated (${reason})\n`)
}
lines.push(`${prefix}${result.matches.length} replacement(s):\n`)
for (const match of result.matches) {
const loc = `${match.file}:${match.range.start.line + 1}:${match.range.start.column + 1}`
lines.push(`${loc}`)
lines.push(` ${match.text}`)
lines.push("")
}
if (isDryRun) {
lines.push("Use dryRun=false to apply changes")
}
return lines.join("\n")
}
+195
View File
@@ -0,0 +1,195 @@
import { DEFAULT_TIMEOUT_MS } from "./language-support"
import { createSgResultFromStdout } from "./sg-compact-json-output"
import type { CliLanguage, SgResult } from "./types"
const SG_BINARY_NOT_FOUND_MESSAGE =
`ast-grep (sg) binary not found.\n\n` +
`Install options:\n` +
` bun add -D @ast-grep/cli\n` +
` cargo install ast-grep --locked\n` +
` brew install ast-grep`
export interface SgRunArgs {
readonly pattern: string
readonly lang: CliLanguage
readonly cwd?: string
readonly paths?: readonly string[]
readonly globs?: readonly string[]
readonly rewrite?: string
readonly context?: number
readonly updateAll?: boolean
}
export interface SpawnOptions {
readonly cwd?: string
readonly stdout?: "pipe" | "inherit" | "ignore"
readonly stderr?: "pipe" | "inherit" | "ignore"
}
export interface SpawnResult {
readonly stdout: string
readonly stderr: string
readonly exitCode: number
}
export type SpawnProcess = (
binary: string,
args: readonly string[],
options?: SpawnOptions,
) => Promise<SpawnResult>
export interface SgRunnerDeps {
readonly resolveBinary: () => Promise<string>
readonly spawnProcess: SpawnProcess
}
export function buildSgArgs(
options: SgRunArgs,
flags: { readonly includeJson: boolean; readonly includeUpdateAll: boolean },
): string[] {
const args = ["run", "-p", options.pattern, "--lang", options.lang]
if (flags.includeJson) {
args.push("--json=compact")
}
if (options.rewrite) {
args.push("-r", options.rewrite)
if (flags.includeUpdateAll) {
args.push("--update-all")
}
}
if (typeof options.context === "number" && options.context > 0) {
args.push("-C", String(options.context))
}
if (options.globs) {
for (const glob of options.globs) {
args.push("--globs", glob)
}
}
const paths = options.paths && options.paths.length > 0 ? options.paths : ["."]
args.push("--", ...paths)
return args
}
export async function runSg(options: SgRunArgs, deps: SgRunnerDeps): Promise<SgResult> {
const shouldSeparateWritePass = Boolean(options.rewrite && options.updateAll)
const args = buildSgArgs(options, { includeJson: true, includeUpdateAll: false })
let binary: string
try {
binary = await deps.resolveBinary()
} catch (error) {
return {
matches: [],
totalMatches: 0,
truncated: false,
error: isNoEntryError(error) ? SG_BINARY_NOT_FOUND_MESSAGE : `Failed to resolve ast-grep binary: ${errorMessage(error)}`,
}
}
const searchResult = await trySpawn(binary, args, options.cwd, deps)
if (searchResult.error) {
return searchResult.error
}
const output = searchResult.value
if (output.exitCode !== 0 && output.stdout.trim() === "") {
if (output.stderr.includes("No files found")) {
return { matches: [], totalMatches: 0, truncated: false }
}
if (output.stderr.trim()) {
return { matches: [], totalMatches: 0, truncated: false, error: output.stderr.trim() }
}
return { matches: [], totalMatches: 0, truncated: false }
}
const jsonResult = createSgResultFromStdout(output.stdout)
if (!(shouldSeparateWritePass && jsonResult.matches.length > 0)) {
return jsonResult
}
const writeArgs = buildSgArgs(options, { includeJson: false, includeUpdateAll: true })
const writeResult = await trySpawn(binary, writeArgs, options.cwd, deps)
if (writeResult.error) {
return { ...jsonResult, error: `Replace failed: ${writeResult.error.error ?? "unknown error"}` }
}
if (writeResult.value.exitCode !== 0) {
const errorDetail =
writeResult.value.stderr.trim() || `ast-grep exited with code ${writeResult.value.exitCode}`
return { ...jsonResult, error: `Replace failed: ${errorDetail}` }
}
return jsonResult
}
async function trySpawn(
binary: string,
args: readonly string[],
cwd: string | undefined,
deps: SgRunnerDeps,
): Promise<{ readonly value: SpawnResult; readonly error?: never } | { readonly value?: never; readonly error: SgResult }> {
try {
const value = await deps.spawnProcess(binary, args, {
cwd,
stdout: "pipe",
stderr: "pipe",
})
return { value }
} catch (error) {
if (error instanceof Error && error.message.includes("timeout")) {
return {
error: {
matches: [],
totalMatches: 0,
truncated: true,
truncatedReason: "timeout",
error: error.message,
},
}
}
if (isNoEntryError(error)) {
return {
error: {
matches: [],
totalMatches: 0,
truncated: false,
error: SG_BINARY_NOT_FOUND_MESSAGE,
},
}
}
return {
error: {
matches: [],
totalMatches: 0,
truncated: false,
error: `Failed to spawn ast-grep: ${errorMessage(error)}`,
},
}
}
}
function isNoEntryError(error: unknown): boolean {
if (typeof error !== "object" || error === null) {
return false
}
const code = Reflect.get(error, "code")
const message = errorMessage(error)
return code === "ENOENT" || message.includes("ENOENT") || message.includes("not found")
}
function errorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message
}
return String(error)
}
export { DEFAULT_TIMEOUT_MS }
@@ -0,0 +1,54 @@
import { DEFAULT_MAX_MATCHES, DEFAULT_MAX_OUTPUT_BYTES } from "./language-support"
import type { CliMatch, SgResult } from "./types"
export function createSgResultFromStdout(stdout: string): SgResult {
if (!stdout.trim()) {
return { matches: [], totalMatches: 0, truncated: false }
}
const outputTruncated = stdout.length >= DEFAULT_MAX_OUTPUT_BYTES
const outputToProcess = outputTruncated ? stdout.substring(0, DEFAULT_MAX_OUTPUT_BYTES) : stdout
let matches: CliMatch[] = []
try {
matches = JSON.parse(outputToProcess) as CliMatch[]
} catch {
if (outputTruncated) {
try {
const lastValidIndex = outputToProcess.lastIndexOf("}")
if (lastValidIndex > 0) {
const bracketIndex = outputToProcess.lastIndexOf("},", lastValidIndex)
if (bracketIndex > 0) {
const truncatedJson = outputToProcess.substring(0, bracketIndex + 1) + "]"
matches = JSON.parse(truncatedJson) as CliMatch[]
}
}
} catch {
return {
matches: [],
totalMatches: 0,
truncated: true,
truncatedReason: "max_output_bytes",
error: "Output too large and could not be parsed",
}
}
} else {
return { matches: [], totalMatches: 0, truncated: false }
}
}
const totalMatches = matches.length
const matchesTruncated = totalMatches > DEFAULT_MAX_MATCHES
const finalMatches = matchesTruncated ? matches.slice(0, DEFAULT_MAX_MATCHES) : matches
return {
matches: finalMatches,
totalMatches,
truncated: outputTruncated || matchesTruncated,
truncatedReason: outputTruncated
? "max_output_bytes"
: matchesTruncated
? "max_matches"
: undefined,
}
}
+35
View File
@@ -0,0 +1,35 @@
import type { CLI_LANGUAGES } from "./language-support"
export type CliLanguage = (typeof CLI_LANGUAGES)[number]
export interface Position {
line: number
column: number
}
export interface Range {
start: Position
end: Position
}
export interface CliMatch {
text: string
range: {
byteOffset: { start: number; end: number }
start: Position
end: Position
}
file: string
lines: string
charCount: { leading: number; trailing: number }
language: string
}
export interface SgResult {
matches: CliMatch[]
totalMatches: number
truncated: boolean
truncatedReason?: "max_matches" | "max_output_bytes" | "timeout"
error?: string
}