sourceSpanOf
이 페이지에서

sourceSpanOf()

함수, IR 분류

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

import { sourceSpanOf } from 'typeshade'

시그니처, 설명, 예제는 커밋 c66579bf의 컴파일러 소스에서 그대로 가져온 영어 원문입니다.

구문

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

매개변수

node Stmt | Expr | FuncDecl

the statement, expression or function declaration to ask about.

반환값

SourceSpan | undefined

the span, or undefined when the node carries none.

설명

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.

예제

예제

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

함께 보기

소스

src/core/ir/span.ts, 110행, 커밋 c66579bf 기준

이 페이지 편집 문제 보고