Skip to content

feat: expose AST handles on MacroContext#151

Merged
sxzz merged 1 commit into
mainfrom
sxzz/unplugin-macros-issue-132
May 11, 2026
Merged

feat: expose AST handles on MacroContext#151
sxzz merged 1 commit into
mainfrom
sxzz/unplugin-macros-issue-132

Conversation

@sxzz
Copy link
Copy Markdown
Member

@sxzz sxzz commented May 11, 2026

What

Adds an ast field to MacroContext that exposes the AST handles for the current macro invocation:

  • ast.call — the CallExpression node for this macro invocation. For await fn() macros the inner CallExpression is returned (not the wrapping AwaitExpression). For tagged-template macros, a synthesized CallExpression is provided whose loc/start/end match the original TaggedTemplateExpression.
  • ast.program — the Program AST of the enclosing file.

Also:

  • Documents the field (and the wider MacroContext surface) in the README, with a $callsite example.
  • Adds a fixture (tests/fixtures/ast.js + tests/fixtures/macros/ast.ts) and a type-level test in tests/define.test.ts.

Why

Closes #132. Macro authors previously had no way to get callsite-aware info (line/column, raw call source, surrounding program) without resorting to runtime stack walks or re-parsing the file. Exposing the already-available Babel nodes is essentially free and unlocks patterns like $callsite(), conditional rewrites, and lightweight code analysis at compile time.

Test notes

  • New snapshot in tests/__snapshots__/fixtures.test.ts.snap covers the ast.js fixture (line/column/calleeName/callSource/programType all round-trip correctly).
  • New type assertions in tests/define.test.ts ensure this.ast.call.type and this.ast.program.type are correctly narrowed.

Follow-ups

  • unpluginContext remains marked experimental; ast is intentionally a stable, minimal surface (call + program) so we can extend it later without breaking changes.

@bolt-new-by-stackblitz
Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@sxzz sxzz merged commit 8f6deff into main May 11, 2026
12 checks passed
@sxzz sxzz deleted the sxzz/unplugin-macros-issue-132 branch May 11, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add callsite information to the MacroContext

1 participant