mix ex_ast.replace (ExAST v0.11.0)

Copy Markdown View Source

Replaces AST pattern matches in Elixir source files.

Usage

mix ex_ast.replace 'pattern' 'replacement' [path ...]

Options

  • --dry-run — show changes without writing files
  • --inside 'pattern' — only replace inside ancestors matching this pattern
  • --not-inside 'pattern' — skip replacements inside ancestors matching this pattern
  • --parent 'pattern' / --not-parent 'pattern' — filter by direct semantic parent
  • --ancestor 'pattern' / --not-ancestor 'pattern' — filter by semantic ancestor
  • --has-child 'pattern' / --not-has-child 'pattern' — filter by direct semantic child
  • --contains 'pattern' / --not-contains 'pattern' — filter by semantic descendant
  • --has-descendant 'pattern' / --not-has-descendant 'pattern' — aliases for contains filters
  • --has 'pattern' / --not-has 'pattern' — aliases for contains filters
  • --follows 'pattern' / --not-follows 'pattern' — filter by earlier sibling
  • --precedes 'pattern' / --not-precedes 'pattern' — filter by later sibling
  • --immediately-follows 'pattern' / --not-immediately-follows 'pattern' — filter by previous sibling
  • --immediately-precedes 'pattern' / --not-immediately-precedes 'pattern' — filter by next sibling
  • --first / --not-first, --last / --not-last, --nth n / --not-nth n — filter by sibling position
  • --comment text / --not-comment text — filter by associated comments
  • --comment-before text, --comment-after text, --comment-inside text, --comment-inline text — filter by comment location

Comment values are substrings by default. Use /.../ or ~r/.../ for regexes, including flags like /todo/i.

Examples

mix ex_ast.replace 'IO.inspect(expr, _)' 'expr' lib/
mix ex_ast.replace 'dbg(expr)' 'expr'
mix ex_ast.replace --dry-run '%Step{id: "subject"}' 'SharedSteps.subject_step(@opts)'
mix ex_ast.replace --not-inside 'test _ do _ end' 'IO.inspect(expr)' 'expr'
mix ex_ast.replace 'IO.inspect(expr)' 'Logger.debug(inspect(expr))' lib/ --parent 'def _ do ... end'
mix ex_ast.replace 'Repo.get!(schema, id)' 'Repo.fetch!(schema, id)' lib/ --contains 'Repo.transaction(_)' --not-contains 'IO.inspect(...)'
mix ex_ast.replace 'Logger.debug(record)' 'Logger.info(record)' lib/ --immediately-precedes 'Repo.delete(record)'
mix ex_ast.replace 'IO.inspect(expr)' 'dbg(expr)' lib/ --comment-inline temporary
mix ex_ast.replace 'IO.inspect(expr)' 'dbg(expr)' lib/ --comment-inline '/temporary|debug/i'