diff --git a/crates/oxc_isolated_declarations/src/class.rs b/crates/oxc_isolated_declarations/src/class.rs index 9c7a3b8ac..67a77a8b3 100644 --- a/crates/oxc_isolated_declarations/src/class.rs +++ b/crates/oxc_isolated_declarations/src/class.rs @@ -347,11 +347,7 @@ impl<'a> IsolatedDeclarations<'a> { &self, decl: &Class<'a>, declare: Option, - ) -> Option>> { - 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, @@ -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() { @@ -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(), @@ -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( diff --git a/crates/oxc_isolated_declarations/src/declaration.rs b/crates/oxc_isolated_declarations/src/declaration.rs index f46f3074b..f3a421476 100644 --- a/crates/oxc_isolated_declarations/src/declaration.rs +++ b/crates/oxc_isolated_declarations/src/declaration.rs @@ -157,25 +157,19 @@ impl<'a> IsolatedDeclarations<'a> { ) -> Option> { 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) { diff --git a/crates/oxc_isolated_declarations/src/function.rs b/crates/oxc_isolated_declarations/src/function.rs index 8acee7ba7..775bbcecd 100644 --- a/crates/oxc_isolated_declarations/src/function.rs +++ b/crates/oxc_isolated_declarations/src/function.rs @@ -16,29 +16,25 @@ impl<'a> IsolatedDeclarations<'a> { &mut self, func: &Function<'a>, declare: Option, - ) -> Option>> { - 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( diff --git a/crates/oxc_isolated_declarations/src/module.rs b/crates/oxc_isolated_declarations/src/module.rs index 2dfb5b5e9..22f4bfc85 100644 --- a/crates/oxc_isolated_declarations/src/module.rs +++ b/crates/oxc_isolated_declarations/src/module.rs @@ -36,12 +36,18 @@ impl<'a> IsolatedDeclarations<'a> { decl: &ExportDefaultDeclaration<'a>, ) -> Option<(Option>, 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))) } diff --git a/crates/oxc_isolated_declarations/tests/fixtures/export-declare-declarations.ts b/crates/oxc_isolated_declarations/tests/fixtures/export-declare-declarations.ts new file mode 100644 index 000000000..d1f46ea33 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/fixtures/export-declare-declarations.ts @@ -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 } diff --git a/crates/oxc_isolated_declarations/tests/snapshots/export-declare-declarations.snap b/crates/oxc_isolated_declarations/tests/snapshots/export-declare-declarations.snap new file mode 100644 index 000000000..38857f953 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/snapshots/export-declare-declarations.snap @@ -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 };