---
title: "autoVars(): function in Emit, TypeShade API reference"
description: "autoVars, a function in Emit. Turns every value that is later assigned to into a WGSL var."
url: https://typeshade.dev/api/autovars/
language: en
---

# `autoVars()`

Function in Emit

Turns every value that is later assigned to into a WGSL var.

`import { autoVars } from 'typeshade'`

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

## Syntax

```ts
function autoVars(m: ModuleDecl): ModuleDecl
```

## Parameters

**`m` `ModuleDecl`**

The module to process.

## Return value

`ModuleDecl`

A new module with the rewritten functions; `m` itself is unchanged.

## Description

A shader author writes a mutable value as a plain `const x = expr` and then calls `x.assign(...)` or a compound assignment such as `+=` on it; nothing marks `x` as a variable up front. This pass finds each such value in every function of `m`, declares a `var` for it just before the first statement that uses it, and rewrites every reference to that value, including the assignment targets themselves, to read and write the new `var`. An assignment through a member or index access (`c.a`, `m[i]`) declares the `var` for the base value `c` or `m`. Values that already have a name (function parameters, constants, override constants and existing `var` or `let` bindings) are left alone.

## Source

[`src/core/passes/opt/auto-vars.ts`, line 188](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/passes/opt/auto-vars.ts#L188), at commit af54ddad5
