mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(isolated-declarations): retain declare declarations when they are exported (#8477)
close: #8473
This commit is contained in:
parent
7ee7634f1e
commit
407187894e
6 changed files with 74 additions and 50 deletions
|
|
@ -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,
|
||||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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)))
|
||||
}
|
||||
|
|
|
|||
15
crates/oxc_isolated_declarations/tests/fixtures/export-declare-declarations.ts
vendored
Normal file
15
crates/oxc_isolated_declarations/tests/fixtures/export-declare-declarations.ts
vendored
Normal file
|
|
@ -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 }
|
||||
|
|
@ -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 };
|
||||
Loading…
Reference in a new issue