---
title: "sourceSpanOf(): function in IR, TypeShade API reference"
description: "sourceSpanOf, a function in IR. The authored source span of an IR node, or undefined when it has none."
url: https://typeshade.dev/api/sourcespanof/
language: en
---

# `sourceSpanOf()`

Function in IR

The authored source span of an IR node, or undefined when it has none.

`import { sourceSpanOf } from 'typeshade'`
`import { sourceSpanOf } from 'typeshade/core/ir'`

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

## Syntax

```ts
function sourceSpanOf(node: Stmt | Expr | FuncDecl): SourceSpan | undefined
```

## Parameters

**`node` `Stmt | Expr | FuncDecl`**

the statement, expression or function declaration to ask about.

## Return value

`SourceSpan | undefined`

the span, or `undefined` when the node carries none.

## Description

A node has one when it was lowered from `"use typeshade"` TypeScript and no pass has rebuilt it from scratch since. Three cases give `undefined`, and each is a real answer rather than a failure:

- The node was authored through the `fn()` EDSL, which has no AST to read a span from. That surface keeps its own line-level tracing (`setSourceTracing`, `getLoc`), which captures a stack frame’s `file:line:col` — a point, not a span. Nothing here converts one into the other, because the width would have to be invented.
- The node was synthesised rather than authored: the loop counter a `while` lowers to, a helper `fp64Lower` injects, a `var` `autoVars` materialises.
- A pass rebuilt the node from named fields instead of spreading the original. The shared walkers all spread, so this is rare, and where it happens the rewritten node is not the statement the author wrote.

Reading through this function rather than the field directly is what lets a later increment widen where spans come from — more expression kinds, a fallback table for a node shape that cannot carry a field — without a breaking change.

## Examples

### Example

```ts
import { compile, sourceSpanOf } from 'typeshade'

const { module } = compile(src)
for (const s of module.funcs[0]!.body) {
  const span = sourceSpanOf(s)
  if (span) console.log(`${span.file}:${span.line + 1}:${span.character + 1}`)
}
```

## In the guide

- [Diagnostics](https://typeshade.dev/guide/internals/diagnostics/)

## See also

- [`SourceSpan`](https://typeshade.dev/api/sourcespan/)

## Source

[`src/core/ir/span.ts`, line 111](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/span.ts#L111), at commit af54ddad5
