From 083e08144e6f7c9a0b8eed369472d9b4b2ab8315 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Thu, 11 Dec 2025 13:27:54 +0000 Subject: [PATCH] feat(semantic): add TS2670 error for global scope augmentation without declare modifier (#16746) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Report error when `global { }` is used without `declare` modifier and not in an already ambient context (like .d.ts files or inside `declare module` blocks). Discovered while working on https://github.com/oxc-project/oxc/issues/16743 ๐Ÿค– generated with help from Claude Opus 4.5 --- crates/oxc_semantic/src/checker/typescript.rs | 12 ++++++++++++ tasks/coverage/snapshots/parser_typescript.snap | 11 ++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/oxc_semantic/src/checker/typescript.rs b/crates/oxc_semantic/src/checker/typescript.rs index 2f9d26a153a9e..b99522a3687e5 100644 --- a/crates/oxc_semantic/src/checker/typescript.rs +++ b/crates/oxc_semantic/src/checker/typescript.rs @@ -159,8 +159,20 @@ pub fn check_ts_module_declaration<'a>(decl: &TSModuleDeclaration<'a>, ctx: &Sem check_ts_export_assignment_in_module_decl(decl, ctx); } +fn global_scope_augmentation_should_have_declare_modifier(span: Span) -> OxcDiagnostic { + ts_error( + "2670", + "Augmentations for the global scope should have 'declare' modifier unless they appear in already ambient context.", + ) + .with_label(span) +} + pub fn check_ts_global_declaration<'a>(decl: &TSGlobalDeclaration<'a>, ctx: &SemanticBuilder<'a>) { check_ts_module_or_global_declaration(decl.span, ctx); + + if !decl.declare && !ctx.in_declare_scope() { + ctx.error(global_scope_augmentation_should_have_declare_modifier(decl.global_span)); + } } fn check_ts_module_or_global_declaration(span: Span, ctx: &SemanticBuilder<'_>) { diff --git a/tasks/coverage/snapshots/parser_typescript.snap b/tasks/coverage/snapshots/parser_typescript.snap index 149d2ab84cb63..15c8927f9a17a 100644 --- a/tasks/coverage/snapshots/parser_typescript.snap +++ b/tasks/coverage/snapshots/parser_typescript.snap @@ -3,7 +3,7 @@ commit: c21f73fd parser_typescript Summary: AST Parsed : 9832/9833 (99.99%) Positive Passed: 9823/9833 (99.90%) -Negative Passed: 1485/2547 (58.30%) +Negative Passed: 1486/2547 (58.34%) Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration3.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration4.ts @@ -654,8 +654,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/moduleAugmen Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationGlobal6.ts -Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationGlobal6_1.ts - Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationGlobal7.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/moduleAugmentationGlobal7_1.ts @@ -8166,6 +8164,13 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc โ•ฐโ”€โ”€โ”€โ”€ help: Only 'readonly' modifier is allowed here. + ร— TS(2670): Augmentations for the global scope should have 'declare' modifier unless they appear in already ambient context. + โ•ญโ”€[typescript/tests/cases/compiler/moduleAugmentationGlobal6_1.ts:1:1] + 1 โ”‚ global { + ยท โ”€โ”€โ”€โ”€โ”€โ”€ + 2 โ”‚ interface Array { x } + โ•ฐโ”€โ”€โ”€โ”€ + ร— Identifier `Kettle` has already been declared โ•ญโ”€[typescript/tests/cases/compiler/moduleDuplicateIdentifiers.ts:20:14] 19 โ”‚