---
title: "renameVarrefsInFunc(): function in IR, TypeShade API"
description: "renameVarrefsInFunc, a function in IR. Renames variable references in the body of f."
url: https://typeshade.dev/api/renamevarrefsinfunc/
language: en
---

# `renameVarrefsInFunc()`

Function in IR

Renames variable references in the body of f.

`import { renameVarrefsInFunc } from 'typeshade'`

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
function renameVarrefsInFunc(
  f: FuncDecl,
  rename: (name: string) => string | undefined,
): FuncDecl
```

## Parameters

**`f` `FuncDecl`**

The function whose body is rewritten.

**`rename` `(name: string) => string | undefined`**

Maps a current variable name to its new name, or returns `undefined` to keep the current name.

## Return value

`FuncDecl`

A copy of `f` with the renamed body, or `f` itself when no reference was renamed.

## Description

Every variable reference (an expression with `op: 'varref'`) is passed to `rename` by name. When `rename` returns a string, the reference is re-pointed at that name; when it returns `undefined`, the reference is kept as is. Unchanged references keep their original objects, and `f` itself is returned when no name changed (see [`rewriteExprsInFunc`](https://typeshade.dev/api/rewriteexprsinfunc/)).

## Examples

### Example

```ts
// Point reads of the `u` uniform block at `u_split`.
const g = renameVarrefsInFunc(f, (name) =>
  name === 'u' ? 'u_split' : undefined,
)
```

## See also

- [`rewriteExprsInFunc`](https://typeshade.dev/api/rewriteexprsinfunc/)

## Source

[`src/core/passes/rename-varrefs.ts`, line 198](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/passes/rename-varrefs.ts#L198), at commit af54ddad5
