View Source Sourceror.Patch (Sourceror v1.6.0)
Functions that generate patches for common operations.
Functions in this module assume that the AST was parsed using Sourceror
functions and that it wasn't modified. If you changed the tree before calling
Sourceror.Patch
functions, then the patch ranges are not guaranteed to match
1:1 with the original source code.
Summary
Functions
Creates a new patch.
Renames a qualified or unqualified function call.
Renames an identifier(ie a variable name).
Generates patches that rename the keys of a keyword list.
Generates a patch that replaces the node with the given replacement.
Types
Functions
Creates a new patch.
Renames a qualified or unqualified function call.
iex> original = "String.to_atom(foo)"
iex> ast = Sourceror.parse_string!(original)
iex> patch = Sourceror.Patch.rename_call(ast, :to_existing_atom)
iex> Sourceror.patch_string(original, [patch])
"String.to_existing_atom(foo)"
If the call is a sigil, you only need to provide the replacement letter:
iex> original = "~H(foo)"
iex> ast = Sourceror.parse_string!(original)
iex> patch = Sourceror.Patch.rename_call(ast, :F)
iex> Sourceror.patch_string(original, [patch])
"~F(foo)"
Renames an identifier(ie a variable name).
Examples
iex> original = "foo"
iex> ast = Sourceror.parse_string!(original)
iex> patch = Sourceror.Patch.rename_identifier(ast, :bar)
iex> Sourceror.patch_string(original, [patch])
"bar"
Generates patches that rename the keys of a keyword list.
The replacements is a keyword list, with the keys to replace as keys, and the replacement as the value.
Examples
iex> original = "[a: b, c: d, e: f]"
iex> ast = Sourceror.parse_string!(original)
iex> patches = Sourceror.Patch.rename_kw_keys(ast, a: :foo, e: :bar)
iex> Sourceror.patch_string(original, patches)
"[foo: b, c: d, bar: f]"
@spec replace(zipper :: Sourceror.Zipper.t(), replacement :: String.t()) :: t()
Generates a patch that replaces the node with the given replacement.
Examples
iex> original = "foo"
iex> ast = Sourceror.parse_string!(original)
iex> patch = Sourceror.Patch.replace(ast, "bar")
iex> Sourceror.patch_string(original, [patch])
"bar"