ExAST.Patcher (ExAST v0.3.0)

Copy Markdown View Source

Finds and replaces AST patterns in source code.

Uses Sourceror for parsing, traversal, and source-level patching to preserve formatting of unchanged code.

Summary

Functions

Finds all occurrences of pattern in source.

Replaces all occurrences of pattern with replacement in source.

Types

match()

@type match() :: %{
  node: Macro.t(),
  range: Sourceror.Range.t(),
  captures: ExAST.Pattern.captures()
}

Functions

find_all(source, pattern, opts \\ [])

@spec find_all(String.t(), String.t(), keyword()) :: [match()]

Finds all occurrences of pattern in source.

Options

  • :inside — only match nodes nested within an ancestor matching this pattern
  • :not_inside — reject nodes nested within an ancestor matching this pattern

Returns a list of matches with the matched node, its source range, and any captured values.

replace_all(source, pattern, replacement, opts \\ [])

@spec replace_all(String.t(), String.t(), String.t(), keyword()) :: String.t()

Replaces all occurrences of pattern with replacement in source.

Captures from the pattern are substituted into the replacement template. Accepts the same :inside / :not_inside options as find_all/3. Returns the modified source string.