Repository navigation
fix(checker): reject exports originating from global augmentations - #64236
Closed
erantianantha wants to merge 1 commit into
Closed
erantianantha wants to merge 1 commit into
erantianantha wants to merge 1 commit into
Conversation
Fixes microsoft#64152. When checking an export specifier without a module specifier (e.g. `export { XYZ }`), the checker verifies that the resolved symbol is a local declaration rather than a global declaration (reporting TS2661 "Cannot export '{0}'. Only local declarations can be exported from a module."). Previously, only symbols declared in global source files (ast.IsGlobalSourceFile) were checked, which created a hole for declarations originating inside `declare global { ... }` augmentations in external modules. Update the check to also recognize declarations within module blocks whose parent is a global scope augmentation (`ast.IsModuleBlock(container) && ast.IsGlobalScopeAugmentation(container.Parent)`). Add compiler test case: exportSpecifierForAGlobalAugmentation.ts
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused checker change correctly addresses the reported hole and includes matching regression coverage.
Pull request overview
Fixes rejection of exports sourced from global augmentations.
Changes:
- Detects declarations within global augmentation module blocks.
- Adds a compiler regression test and reference baselines.
- Includes an unrelated whitespace-only change.
File summaries
| File | Description |
|---|---|
tsc/internal/checker/checker.go |
Rejects exports originating from global augmentations. |
tsc/internal/checker/relater.go |
Adds a non-functional blank line. |
tsc/testdata/tests/cases/compiler/exportSpecifierForAGlobalAugmentation.ts |
Adds the regression case. |
tsc/testdata/baselines/reference/compiler/exportSpecifierForAGlobalAugmentation.errors.txt |
Captures expected TS2661. |
tsc/testdata/baselines/reference/compiler/exportSpecifierForAGlobalAugmentation.js |
Captures emitted output. |
tsc/testdata/baselines/reference/compiler/exportSpecifierForAGlobalAugmentation.symbols |
Captures symbol information. |
tsc/testdata/baselines/reference/compiler/exportSpecifierForAGlobalAugmentation.types |
Captures inferred types. |
Review details
- Files reviewed: 7/7 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Member
|
Sorry, there's already a PR out at #64162. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #64152.
Problem
When exporting bindings from a module without a local declaration (e.g.
export { Math };), TypeScript correctly issues error TS2661 (Cannot export '{0}'. Only local declarations can be exported from a module.).However, exports originating from a global scope augmentation (
declare global { ... }) were previously permitted without error:Root Cause
In
checkExportSpecifier(tsc/internal/checker/checker.go), the global declaration check only tested whether the declaration container was a global source file (ast.IsGlobalSourceFile(declarationContainer)). Declarations inside a global augmentation have anast.ModuleBlockdeclaration container whose parent is a global scope augmentation (ast.IsGlobalScopeAugmentation(declarationContainer.Parent)), bypassing the error check.Fix
Update
checkExportSpecifierto treat declarations inside module blocks of global scope augmentations as non-local export sources:Add compiler test case
exportSpecifierForAGlobalAugmentation.tsand reference baselines.