Skip to content

feat(typescript): support declare global and declare namespace codegen - #150

Open
lx3133584 wants to merge 1 commit into
unjs:mainfrom
lx3133584:feat/declare-global-and-namespace
Open

lx3133584 wants to merge 1 commit into
unjs:mainfrom
lx3133584:feat/declare-global-and-namespace

Conversation

@lx3133584

@lx3133584 lx3133584 commented Aug 26, 2026

Copy link
Copy Markdown

Problem

TypeScript augmentations for declare global and declare namespace <name> were not directly supported by genAugmentation (which always generated declare module "...").

Root Cause

genAugmentation always wrapped the input specifier in genString(specifier) and prefixed with declare module. In TypeScript, declare global is a reserved declaration keyword without quotes or module keyword.

Fix

  • Updated genAugmentation to emit declare global when specifier === "global".
  • Added genNamespace helper to generate declare namespace <name> { ... } declarations.

Testing

  • Added unit tests in test/typescript.test.ts for declare global and declare namespace.
  • Verified all 63 unit tests and linter pass cleanly.

Summary by CodeRabbit

  • New Features

    • Added support for generating TypeScript namespace declarations.
    • Added support for generating global declarations, including global interface extensions.
    • Module augmentation generation continues to support named modules.
  • Tests

    • Added coverage for empty namespaces, namespaces containing interfaces, and global augmentations.

Fixes unjs#81

Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Aug 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 855cf072-39ac-4362-9fcf-43707bb3ac15

📥 Commits

Reviewing files that changed from the base of the PR and between a95f4d4 and 2fa07b3.

📒 Files selected for processing (2)
  • src/typescript.ts
  • test/typescript.test.ts

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

The TypeScript generator now supports declare global augmentations and adds a public genNamespace function. Tests cover global augmentations and namespace declarations with optional interfaces.

Changes

TypeScript declaration generation

Layer / File(s) Summary
Declaration generator updates
src/typescript.ts
genAugmentation generates declare global for the "global" specifier and declare module otherwise. The new genNamespace function generates namespace declarations with optional interfaces.
Generator coverage
test/typescript.test.ts
Tests cover global augmentations, empty namespaces, and namespaces containing an empty interface.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 2fa07

This change adds generation support for declare global and declare namespace while including focused tests, and no actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the two main changes: support for declare global and declare namespace code generation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant