Skip to content

Commit

Permalink
fix(isolated-declarations): bindings referenced in `TSModuleDeclarati…
Browse files Browse the repository at this point in the history
…on` are removed incorrectly (#5680)

close: #5667
  • Loading branch information
Dunqing committed Sep 10, 2024
1 parent afc4548 commit 6e8409a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
10 changes: 4 additions & 6 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ impl<'a> IsolatedDeclarations<'a> {
variable_transformed_indexes.push_back(FxHashSet::default());
}
Declaration::TSModuleDeclaration(decl) => {
if decl.kind.is_global() {
// declare global { ... } or declare module "foo" { ... }
// We need to emit it anyway
if decl.kind.is_global() || decl.id.is_string_literal() {
// We need to visit the module declaration to collect all references
self.scope.visit_ts_module_declaration(decl);
transformed_indexes.insert(new_stmts.len());
}
Expand Down Expand Up @@ -293,11 +296,6 @@ impl<'a> IsolatedDeclarations<'a> {
new_ast_stmts.push(Statement::ImportDeclaration(decl));
}
}
Statement::TSModuleDeclaration(decl) => {
if decl.kind.is_global() || decl.id.is_string_literal() {
new_ast_stmts.push(Statement::TSModuleDeclaration(decl));
}
}
_ => {}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_isolated_declarations/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl<'a> Scope<'a> {
}

/// Linear tree of declaration scopes.
#[derive(Debug)]
pub struct ScopeTree<'a> {
levels: Vec<'a, Scope<'a>>,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import 'foo';
declare module 'foo' {
interface Foo {}
const foo = 42;
import "foo";
declare module "foo" {
interface Foo {}
const foo = 42;
}

declare global {
interface Bar {}
const bar = 42 ;
interface Bar {}
const bar = 42;
}

import { type X } from "./x";
type Y = 1;

declare module "foo" {
interface Foo {
x: X;
y: Y;
}
}

// should not be emitted
module baz {
interface Baz {}
const baz = 42;
interface Baz {}
const baz = 42;
}

declare module x {
interface Qux {}
const qux = 42;
interface Qux {}
const qux = 42;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ declare global {
interface Bar {}
const bar = 42;
}
import { type X } from "./x";
type Y = 1;
declare module "foo" {
interface Foo {
x: X;
y: Y;
}
}
export {};

0 comments on commit 6e8409a

Please sign in to comment.