UNPKG

astx

Version:

super powerful structural search and replace for JavaScript and TypeScript to automate your refactoring

88 lines (87 loc) 3.54 kB
import { Expression, Statement, Node, NodePath } from './types'; import { Backend } from './backend/Backend'; import { Match } from './find'; import * as AstTypes from 'ast-types'; export declare type TransformOptions = { /** The absolute path to the current file. */ file: string; /** The source code of the current file. */ source: string; astx: Astx; expression(strings: TemplateStringsArray, ...quasis: any[]): Expression; statement(strings: TemplateStringsArray, ...quasis: any[]): Statement; statements(strings: TemplateStringsArray, ...quasis: any[]): Statement[]; t: typeof AstTypes; report: (msg: unknown) => void; }; export declare type TransformFunction = (options: TransformOptions) => string | null | undefined | void; export declare type Transform = { astx?: TransformFunction; find?: string | Node | Node[]; replace?: string | Node | Node[] | GetReplacement; where?: FindOptions['where']; onReport?: (options: { file: string; report: unknown; }) => void; finish?: () => any; }; export declare type TransformResult = { file: string; source?: string; transformed?: string; reports?: unknown[]; error?: Error; matches?: readonly Match[]; backend: Backend; }; export declare type ParsePattern = (strings: string | string[] | TemplateStringsArray, ...quasis: any[]) => Node | Node[]; export declare type GetReplacement = (astx: Astx, parse: ParsePattern) => string | Node | Node[]; export declare type FindOptions = { where?: { [captureName: string]: (path: NodePath) => boolean; }; }; declare class ExtendableProxy { constructor(handler: ProxyHandler<any>); } export default class Astx extends ExtendableProxy implements Iterable<Astx> { [name: `$${string}` | `$$${string}` | `$$$${string}`]: Astx; readonly backend: Backend; private readonly _matches; private readonly _withCaptures; private readonly _placeholder; private _lazyPaths; private _lazyNodes; private _lazyInitialMatch; constructor(backend: Backend, paths: NodePath<any>[] | Match[], { withCaptures, placeholder, }?: { withCaptures?: Match[]; placeholder?: string; }); get placeholder(): string | undefined; get size(): number; get matches(): readonly Match[]; [Symbol.iterator](): Iterator<Astx>; get match(): Match; get node(): Node; get path(): NodePath; get code(): string; get stringValue(): string; get paths(): readonly NodePath[]; get nodes(): readonly Node[]; filter(iteratee: (astx: Astx, index: number, parent: Astx) => boolean): Astx; map<T>(iteratee: (astx: Astx, index: number, parent: Astx) => T): T[]; at(index: number): Astx; withCaptures(...captures: (Match | Astx | { [name: `$${string}` | `$$${string}` | `$$$${string}`]: Astx; })[]): Astx; private get initialMatch(); private _execPattern; closest(strings: TemplateStringsArray, ...quasis: any[]): (options?: FindOptions) => Astx; closest(pattern: string | Node | Node[] | NodePath<any> | NodePath<any>[], options?: FindOptions): Astx; find(strings: string[] | TemplateStringsArray, ...quasis: any[]): (options?: FindOptions) => Astx; find(pattern: string | Node | Node[] | NodePath<any> | NodePath<any>[], options?: FindOptions): Astx; replace(strings: string[] | TemplateStringsArray, ...quasis: any[]): () => void; replace(replacement: string | Node | Node[] | GetReplacement): void; } export {};