View Source Sourceror.Patch (Sourceror v1.4.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

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

@type change() :: String.t() | (String.t() -> String.t())
@type t() :: %Sourceror.Patch{
  change: change(),
  preserve_indentation: boolean() | nil,
  range: map()
}

Functions

Link to this function

new(range, change, preserve_indentation \\ true)

View Source
@spec new(range :: map(), change :: change(), preserve_indentation :: boolean()) ::
  t()

Creates a new patch.

Link to this function

rename_call(arg, new_name)

View Source
@spec rename_call(call :: Macro.t(), new_name :: atom() | String.t()) :: t()

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)"
Link to this function

rename_identifier(arg, new_name)

View Source
@spec rename_identifier(identifier :: Macro.t(), new_name :: atom() | String.t()) ::
  t()

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"
Link to this function

rename_kw_keys(arg, replacements)

View Source
@spec rename_kw_keys(keyword :: Macro.t(), replacements :: keyword()) :: [t()]

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]"
Link to this function

replace(zipper, replacement)

View Source
@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"