Capstone.Vendor.Sourceror.Patch (Capstone v0.33.3)

Copy Markdown View Source

Functions that generate patches for common operations.

Functions in this module assume that the AST was parsed using Capstone.Vendor.Sourceror functions and that it wasn't modified. If you changed the tree before calling Capstone.Vendor.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

change()

@type change() :: String.t() | (String.t() -> String.t())

t()

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

Functions

new(range, change, preserve_indentation \\ true)

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

Creates a new patch.

rename_call(arg, new_name)

@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 = Capstone.Vendor.Sourceror.parse_string!(original)
iex> patch = Capstone.Vendor.Sourceror.Patch.rename_call(ast, :to_existing_atom)
iex> Capstone.Vendor.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 = Capstone.Vendor.Sourceror.parse_string!(original)
iex> patch = Capstone.Vendor.Sourceror.Patch.rename_call(ast, :F)
iex> Capstone.Vendor.Sourceror.patch_string(original, [patch])
"~F(foo)"

rename_identifier(arg, new_name)

@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 = Capstone.Vendor.Sourceror.parse_string!(original)
iex> patch = Capstone.Vendor.Sourceror.Patch.rename_identifier(ast, :bar)
iex> Capstone.Vendor.Sourceror.patch_string(original, [patch])
"bar"

rename_kw_keys(arg, replacements)

@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 = Capstone.Vendor.Sourceror.parse_string!(original)
iex> patches = Capstone.Vendor.Sourceror.Patch.rename_kw_keys(ast, a: :foo, e: :bar)
iex> Capstone.Vendor.Sourceror.patch_string(original, patches)
"[foo: b, c: d, bar: f]"

replace(zipper, replacement)

@spec replace(
  zipper :: Capstone.Vendor.Sourceror.Zipper.t(),
  replacement :: String.t()
) :: t()

Generates a patch that replaces the node with the given replacement.

Examples

iex> original = "foo"
iex> ast = Capstone.Vendor.Sourceror.parse_string!(original)
iex> patch = Capstone.Vendor.Sourceror.Patch.replace(ast, "bar")
iex> Capstone.Vendor.Sourceror.patch_string(original, [patch])
"bar"