sourceSpanOf
On this page

sourceSpanOf()

Function in IR

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

import { sourceSpanOf } from 'typeshade'

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

Syntax

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

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}`)
}

See also

Source

src/core/ir/span.ts, line 110, at commit 26de7be8

Edit this page Report a problem