Skip to content

Commit

Permalink
fix(isolated-declarations): retain declare declarations when they a…
Browse files Browse the repository at this point in the history
…re exported (#8477)

close: #8473
  • Loading branch information
Dunqing committed Jan 14, 2025
1 parent 7ee7634 commit 4071878
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 50 deletions.
12 changes: 4 additions & 8 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,7 @@ impl<'a> IsolatedDeclarations<'a> {
&self,
decl: &Class<'a>,
declare: Option<bool>,
) -> Option<Box<'a, Class<'a>>> {
if decl.declare {
return None;
}

) -> Box<'a, Class<'a>> {
if let Some(super_class) = &decl.super_class {
let is_not_allowed = match super_class {
Expression::Identifier(_) => false,
Expand Down Expand Up @@ -500,7 +496,7 @@ impl<'a> IsolatedDeclarations<'a> {
}

if property.computed && self.report_property_key(&property.key) {
return None;
continue;
}

if property.key.is_private_identifier() {
Expand Down Expand Up @@ -550,7 +546,7 @@ impl<'a> IsolatedDeclarations<'a> {

let body = self.ast.class_body(decl.body.span, elements);

Some(self.ast.alloc_class(
self.ast.alloc_class(
decl.span,
decl.r#type,
self.ast.vec(),
Expand All @@ -562,7 +558,7 @@ impl<'a> IsolatedDeclarations<'a> {
body,
decl.r#abstract,
declare.unwrap_or_else(|| self.is_declare()),
))
)
}

pub(crate) fn create_formal_parameters(
Expand Down
22 changes: 8 additions & 14 deletions crates/oxc_isolated_declarations/src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,19 @@ impl<'a> IsolatedDeclarations<'a> {
) -> Option<Declaration<'a>> {
match decl {
Declaration::FunctionDeclaration(func) => {
if !check_binding
|| func.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name))
{
self.transform_function(func, None).map(Declaration::FunctionDeclaration)
} else {
None
}
let needs_transform = !check_binding
|| func.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name));
needs_transform
.then(|| Declaration::FunctionDeclaration(self.transform_function(func, None)))
}
Declaration::VariableDeclaration(decl) => self
.transform_variable_declaration(decl, check_binding)
.map(Declaration::VariableDeclaration),
Declaration::ClassDeclaration(decl) => {
if !check_binding
|| decl.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name))
{
self.transform_class(decl, None).map(Declaration::ClassDeclaration)
} else {
None
}
let needs_transform = !check_binding
|| decl.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name));
needs_transform
.then(|| Declaration::ClassDeclaration(self.transform_class(decl, None)))
}
Declaration::TSTypeAliasDeclaration(alias_decl) => {
if !check_binding || self.scope.has_reference(&alias_decl.id.name) {
Expand Down
40 changes: 18 additions & 22 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,25 @@ impl<'a> IsolatedDeclarations<'a> {
&mut self,
func: &Function<'a>,
declare: Option<bool>,
) -> Option<Box<'a, Function<'a>>> {
if func.declare {
None
} else {
let return_type = self.infer_function_return_type(func);
if return_type.is_none() {
self.error(function_must_have_explicit_return_type(get_function_span(func)));
}
let params = self.transform_formal_parameters(&func.params);
Some(self.ast.alloc_function(
func.span,
func.r#type,
func.id.clone_in(self.ast.allocator),
false,
false,
declare.unwrap_or_else(|| self.is_declare()),
func.type_parameters.clone_in(self.ast.allocator),
func.this_param.clone_in(self.ast.allocator),
params,
return_type,
NONE,
))
) -> Box<'a, Function<'a>> {
let return_type = self.infer_function_return_type(func);
if return_type.is_none() {
self.error(function_must_have_explicit_return_type(get_function_span(func)));
}
let params = self.transform_formal_parameters(&func.params);
self.ast.alloc_function(
func.span,
func.r#type,
func.id.clone_in(self.ast.allocator),
false,
false,
declare.unwrap_or_else(|| self.is_declare()),
func.type_parameters.clone_in(self.ast.allocator),
func.this_param.clone_in(self.ast.allocator),
params,
return_type,
NONE,
)
}

pub(crate) fn transform_formal_parameter(
Expand Down
18 changes: 12 additions & 6 deletions crates/oxc_isolated_declarations/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ impl<'a> IsolatedDeclarations<'a> {
decl: &ExportDefaultDeclaration<'a>,
) -> Option<(Option<Statement<'a>>, Statement<'a>)> {
let declaration = match &decl.declaration {
ExportDefaultDeclarationKind::FunctionDeclaration(decl) => self
.transform_function(decl, Some(false))
.map(|d| (None, ExportDefaultDeclarationKind::FunctionDeclaration(d))),
ExportDefaultDeclarationKind::ClassDeclaration(decl) => self
.transform_class(decl, Some(false))
.map(|d| (None, ExportDefaultDeclarationKind::ClassDeclaration(d))),
ExportDefaultDeclarationKind::FunctionDeclaration(decl) => Some((
None,
ExportDefaultDeclarationKind::FunctionDeclaration(
self.transform_function(decl, Some(false)),
),
)),
ExportDefaultDeclarationKind::ClassDeclaration(decl) => Some((
None,
ExportDefaultDeclarationKind::ClassDeclaration(
self.transform_class(decl, Some(false)),
),
)),
ExportDefaultDeclarationKind::TSInterfaceDeclaration(_) => {
Some((None, decl.declaration.clone_in(self.ast.allocator)))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Reference, Unreferenced } from 'mod';

declare class Class {}
declare function Function(): Reference
declare const variable: number;
declare enum Enum { A, B, C }

// Should not be included in the output, because they are not exported
declare class UnreferencedClass {}
declare function UnreferencedFunction(): Unreferenced
declare const UnreferencedVariable: number;
declare enum UnreferencedEnum { A, B, C }


export { Class, Function, variable, Enum }
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/oxc_isolated_declarations/tests/mod.rs
input_file: crates/oxc_isolated_declarations/tests/fixtures/export-declare-declarations.ts
---
```
==================== .D.TS ====================
import { Reference } from "mod";
declare class Class {}
declare function Function(): Reference;
declare const variable: number;
declare enum Enum {
A = 0,
B = 1,
C = 2,
}
export { Class, Function, variable, Enum };

0 comments on commit 4071878

Please sign in to comment.