---
title: "rewriteExprsInFunc(): function in IR, TypeShade API"
description: "rewriteExprsInFunc, a function in IR. Applies rewrite to every expression in the body of f and returns the rewritten function."
url: https://typeshade.dev/api/rewriteexprsinfunc/
language: en
---

# `rewriteExprsInFunc()`

Function in IR

Applies rewrite to every expression in the body of f and returns the rewritten function.

`import { rewriteExprsInFunc } from 'typeshade'`

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

## Syntax

```ts
function rewriteExprsInFunc(f: FuncDecl, rewrite: (e: Expr) => Expr): FuncDecl
```

## Parameters

**`f` `FuncDecl`**

The function whose body is rewritten.

**`rewrite` `(e: Expr) => Expr`**

Called once per expression. Return the expression unchanged to keep it, or a new expression to replace it.

## Return value

`FuncDecl`

A copy of `f` with the rewritten body, or `f` itself when no expression changed.

## Description

The walk visits every statement and expression shape in the body. Each expression is passed to `rewrite` after its children have been rewritten, and whatever `rewrite` returns is taken as finished output: it is not walked again.

Object identity is preserved wherever possible. A subtree that `rewrite` leaves alone comes back as the original object, and `f` itself is returned when nothing in the body changed. An expression object that appears at several places in the body is rewritten once, and the same new object is used at every occurrence, so passes that correlate a variable’s declaration, assignments and reads by object identity keep working on the result.

## Examples

### Example

```ts
// Replace every literal `0` with `1`.
const g = rewriteExprsInFunc(f, (e) =>
  e.op === 'lit' && e.value === 0 ? { ...e, value: 1 } : e,
)
```

## Source

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