@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
97 lines • 3.33 kB
TypeScript
/**
* AST Edit Tool
*
* Provides AST-aware code transformations for workspace files.
* Uses @ast-grep/napi for syntax-aware pattern matching and transforms.
*
* Requires @ast-grep/napi as an optional peer dependency.
*/
interface TransformResult {
content: string;
count: number;
error?: string;
}
interface ImportSpec {
module: string;
names: string[];
isDefault?: boolean;
}
/**
* Minimal interface for an ast-grep SgNode.
* Avoids importing @ast-grep/napi types directly since it's an optional dep.
*/
interface SgNode {
text(): string;
range(): {
start: {
index: number;
};
end: {
index: number;
};
};
findAll(config: {
rule: Record<string, unknown>;
}): SgNode[];
getMatch(name: string): SgNode | null;
}
/** Minimal interface for the ast-grep Lang enum values. */
type LangValue = unknown;
/** The subset of @ast-grep/napi we use after dynamic import. */
interface AstGrepModule {
parse(lang: LangValue, content: string): {
root(): SgNode;
};
Lang: Record<string, LangValue>;
}
/**
* Try to load @ast-grep/napi. Returns null if not available.
* Uses dynamic import to avoid compile-time dependency.
* Concurrent callers share the same in-flight promise.
*/
export declare function loadAstGrep(): Promise<AstGrepModule | null>;
/**
* Check if @ast-grep/napi is available without importing it.
* Useful for deciding whether to create the tool at registration time.
*/
export declare function isAstGrepAvailable(): boolean;
/**
* Map file extension to ast-grep Lang enum.
*
* Only languages with built-in tree-sitter grammars in @ast-grep/napi are
* supported. Python, Go, Rust, etc. require separate @ast-grep/lang-* packages
* which are not currently integrated.
*/
export declare function getLanguageFromPath(filePath: string, Lang: Record<string, LangValue>): LangValue | null;
/**
* Add an import statement to the file.
* Inserts after the last existing import, or at the beginning if none exist.
* If the module is already imported, merges new names into it.
*/
export declare function addImport(content: string, root: SgNode, importSpec: ImportSpec): string;
/**
* Remove an import by module name.
* Matches against the import source string.
*/
export declare function removeImport(content: string, root: SgNode, targetName: string): string;
/**
* Pattern-based replacement using AST metavariables.
* Pattern uses $VARNAME placeholders that match any AST node.
* Replacement substitutes matched text back in.
*/
export declare function patternReplace(content: string, root: SgNode, pattern: string, replacement: string): TransformResult;
export declare const astEditTool: import("../../tools").Tool<{
path: string;
pattern?: string | undefined;
replacement?: string | undefined;
transform?: "add-import" | "remove-import" | "rename" | undefined;
targetName?: string | undefined;
newName?: string | undefined;
importSpec?: {
module: string;
names: string[];
isDefault?: boolean | undefined;
} | undefined;
}, unknown, unknown, unknown, import("../../tools").ToolExecutionContext<unknown, unknown, unknown>, "mastra_workspace_ast_edit", unknown>;
export {};
//# sourceMappingURL=ast-edit.d.ts.map