diff --git a/crates/oxc_isolated_declarations/src/scope.rs b/crates/oxc_isolated_declarations/src/scope.rs index 3130646c4d670..84cef4a5d622f 100644 --- a/crates/oxc_isolated_declarations/src/scope.rs +++ b/crates/oxc_isolated_declarations/src/scope.rs @@ -66,7 +66,7 @@ impl<'a> ScopeTree<'a> { fn add_reference(&mut self, name: Atom<'a>, flags: KindFlags) { let scope = self.levels.last_mut().unwrap(); - scope.references.insert(name, flags); + scope.references.entry(name).and_modify(|f| *f |= flags).or_insert(flags); } /// Resolve references in the current scope, and propagate unresolved ones. @@ -84,7 +84,10 @@ impl<'a> ScopeTree<'a> { }); // Merge unresolved references to the parent scope. - self.levels.last_mut().unwrap().references.extend(current_references); + let parent_scope = self.levels.last_mut().unwrap(); + for (name, flags) in current_references { + parent_scope.references.entry(name).and_modify(|f| *f |= flags).or_insert(flags); + } } } diff --git a/crates/oxc_isolated_declarations/tests/fixtures/shadowed.ts b/crates/oxc_isolated_declarations/tests/fixtures/shadowed.ts index 157fb222b944b..3a813578a19bd 100644 --- a/crates/oxc_isolated_declarations/tests/fixtures/shadowed.ts +++ b/crates/oxc_isolated_declarations/tests/fixtures/shadowed.ts @@ -13,4 +13,21 @@ type Module = () => void; namespace Module { export const x = 1; } -export type ModuleType = Module; \ No newline at end of file +export type ModuleType = Module; + +// https://github.com/oxc-project/oxc/issues/10996 +// Variable emit should not be dropped when variable is shadowed as a type name +// and the type is part of a class implements interface +export interface Thing {} + +const Test1 = 'test1' as const; +export type Test1 = typeof Test1; +export class Class1 { + readonly id: 'test1' = Test1; +} + +const Test2 = 'test2' as const; +export type Test2 = typeof Test2; +export class Class2 implements Thing { + readonly id: 'test2' = Test2; +} \ No newline at end of file diff --git a/crates/oxc_isolated_declarations/tests/snapshots/shadowed.snap b/crates/oxc_isolated_declarations/tests/snapshots/shadowed.snap index e59a8b2e408d0..6c93773a998a2 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/shadowed.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/shadowed.snap @@ -17,4 +17,18 @@ declare namespace Module { const x = 1; } export type ModuleType = Module; +// https://github.com/oxc-project/oxc/issues/10996 +// Variable emit should not be dropped when variable is shadowed as a type name +// and the type is part of a class implements interface +export interface Thing {} +declare const Test1: "test1"; +export type Test1 = typeof Test1; +export declare class Class1 { + readonly id: "test1"; +} +declare const Test2: "test2"; +export type Test2 = typeof Test2; +export declare class Class2 implements Thing { + readonly id: "test2"; +} export {};