lukeed / lukeed/comptime

[Feature Request] Support passing callsite information

Open
#1 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
179
Forks
4
PR merge metrics
No merged PRs in 30d

Description

I'd love to be able to get information about the callsite of the comptime, and potentially introspect a layer or two up the callstack. At a basic level, this would be something like { filepath: string, lineNumber: number }, but I imagine could include additional information such as surrounding AST.

An example of what this would be useful for is to inject callsite information automatically into OpenTelemtetry without having to do the expensive and inaccurate (in the case of bundling) operation of retrieving a stacktrace at runtime.

## Motivating examples:
```ts
// src/myfile.ts
import { $autoFunctionLabel, $autoCallsite, $autoLine, $autoSmartAssertCondition } from 'my-lib'

class MyClass {
myFunction() {
const span = tracer.startSpan($autoFunctionLabel())
span.setAttribute("callsite", $autoCallsite())

const x = 5;
const y = 10;

assert(x == y, `Assertion failed! Context: "${$autoLine()}"`)
// a more intelligent variant
assert(x == y, `Assertion failed! Condition: "${$autoSmartAssertCondition()}"`)
}
}

// transforms into

class MyClass {
myFunction() {
const span = tracer.startSpan("MyClass.myFunction")
span.setAttribute("callsite", "src/myfile.ts:42")

const x = 5;
const y = 10;

assert(x == y, `Assertion failed! Context: "assert(x == y, \`Assertion failed! Context: \${$autoLine()}\`)"`)
// a more intelligent variant
assert(x == y, `Assertion failed! Condition: "x == y"`)
}
}
```

(copied over from my issue on unplugin-macros https://github.com/unplugin/unplugin-macros/issues/132 😄 that I never got around to implementing)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing comptime integration with Vite and Rolldown and tracing how TypeScript callsites are transformed at build time. Define the smallest supported callsite shape first, then compare the result against the motivating examples for filepath, line number, function label, and source context.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, vite
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.