mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +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,
|
&self,
|
||||||
decl: &Class<'a>,
|
decl: &Class<'a>,
|
||||||
declare: Option<bool>,
|
declare: Option<bool>,
|
||||||
) -> Option<Box<'a, Class<'a>>> {
|
) -> Box<'a, Class<'a>> {
|
||||||
if decl.declare {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(super_class) = &decl.super_class {
|
if let Some(super_class) = &decl.super_class {
|
||||||
let is_not_allowed = match super_class {
|
let is_not_allowed = match super_class {
|
||||||
Expression::Identifier(_) => false,
|
Expression::Identifier(_) => false,
|
||||||
|
|
@ -500,7 +496,7 @@ impl<'a> IsolatedDeclarations<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if property.computed && self.report_property_key(&property.key) {
|
if property.computed && self.report_property_key(&property.key) {
|
||||||
return None;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if property.key.is_private_identifier() {
|
if property.key.is_private_identifier() {
|
||||||
|
|
@ -550,7 +546,7 @@ impl<'a> IsolatedDeclarations<'a> {
|
||||||
|
|
||||||
let body = self.ast.class_body(decl.body.span, elements);
|
let body = self.ast.class_body(decl.body.span, elements);
|
||||||
|
|
||||||
Some(self.ast.alloc_class(
|
self.ast.alloc_class(
|
||||||
decl.span,
|
decl.span,
|
||||||
decl.r#type,
|
decl.r#type,
|
||||||
self.ast.vec(),
|
self.ast.vec(),
|
||||||
|
|
@ -562,7 +558,7 @@ impl<'a> IsolatedDeclarations<'a> {
|
||||||
body,
|
body,
|
||||||
decl.r#abstract,
|
decl.r#abstract,
|
||||||
declare.unwrap_or_else(|| self.is_declare()),
|
declare.unwrap_or_else(|| self.is_declare()),
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn create_formal_parameters(
|
pub(crate) fn create_formal_parameters(
|
||||||
|
|
|
||||||
|
|
@ -157,25 +157,19 @@ impl<'a> IsolatedDeclarations<'a> {
|
||||||
) -> Option<Declaration<'a>> {
|
) -> Option<Declaration<'a>> {
|
||||||
match decl {
|
match decl {
|
||||||
Declaration::FunctionDeclaration(func) => {
|
Declaration::FunctionDeclaration(func) => {
|
||||||
if !check_binding
|
let needs_transform = !check_binding
|
||||||
|| func.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name))
|
|| func.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name));
|
||||||
{
|
needs_transform
|
||||||
self.transform_function(func, None).map(Declaration::FunctionDeclaration)
|
.then(|| Declaration::FunctionDeclaration(self.transform_function(func, None)))
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Declaration::VariableDeclaration(decl) => self
|
Declaration::VariableDeclaration(decl) => self
|
||||||
.transform_variable_declaration(decl, check_binding)
|
.transform_variable_declaration(decl, check_binding)
|
||||||
.map(Declaration::VariableDeclaration),
|
.map(Declaration::VariableDeclaration),
|
||||||
Declaration::ClassDeclaration(decl) => {
|
Declaration::ClassDeclaration(decl) => {
|
||||||
if !check_binding
|
let needs_transform = !check_binding
|
||||||
|| decl.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name))
|
|| decl.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name));
|
||||||
{
|
needs_transform
|
||||||
self.transform_class(decl, None).map(Declaration::ClassDeclaration)
|
.then(|| Declaration::ClassDeclaration(self.transform_class(decl, None)))
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Declaration::TSTypeAliasDeclaration(alias_decl) => {
|
Declaration::TSTypeAliasDeclaration(alias_decl) => {
|
||||||
if !check_binding || self.scope.has_reference(&alias_decl.id.name) {
|
if !check_binding || self.scope.has_reference(&alias_decl.id.name) {
|
||||||
|
|
|
||||||
|
|
@ -16,29 +16,25 @@ impl<'a> IsolatedDeclarations<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
func: &Function<'a>,
|
func: &Function<'a>,
|
||||||
declare: Option<bool>,
|
declare: Option<bool>,
|
||||||
) -> Option<Box<'a, Function<'a>>> {
|
) -> Box<'a, Function<'a>> {
|
||||||
if func.declare {
|
let return_type = self.infer_function_return_type(func);
|
||||||
None
|
if return_type.is_none() {
|
||||||
} else {
|
self.error(function_must_have_explicit_return_type(get_function_span(func)));
|
||||||
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,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
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(
|
pub(crate) fn transform_formal_parameter(
|
||||||
|
|
|
||||||
|
|
@ -36,12 +36,18 @@ impl<'a> IsolatedDeclarations<'a> {
|
||||||
decl: &ExportDefaultDeclaration<'a>,
|
decl: &ExportDefaultDeclaration<'a>,
|
||||||
) -> Option<(Option<Statement<'a>>, Statement<'a>)> {
|
) -> Option<(Option<Statement<'a>>, Statement<'a>)> {
|
||||||
let declaration = match &decl.declaration {
|
let declaration = match &decl.declaration {
|
||||||
ExportDefaultDeclarationKind::FunctionDeclaration(decl) => self
|
ExportDefaultDeclarationKind::FunctionDeclaration(decl) => Some((
|
||||||
.transform_function(decl, Some(false))
|
None,
|
||||||
.map(|d| (None, ExportDefaultDeclarationKind::FunctionDeclaration(d))),
|
ExportDefaultDeclarationKind::FunctionDeclaration(
|
||||||
ExportDefaultDeclarationKind::ClassDeclaration(decl) => self
|
self.transform_function(decl, Some(false)),
|
||||||
.transform_class(decl, Some(false))
|
),
|
||||||
.map(|d| (None, ExportDefaultDeclarationKind::ClassDeclaration(d))),
|
)),
|
||||||
|
ExportDefaultDeclarationKind::ClassDeclaration(decl) => Some((
|
||||||
|
None,
|
||||||
|
ExportDefaultDeclarationKind::ClassDeclaration(
|
||||||
|
self.transform_class(decl, Some(false)),
|
||||||
|
),
|
||||||
|
)),
|
||||||
ExportDefaultDeclarationKind::TSInterfaceDeclaration(_) => {
|
ExportDefaultDeclarationKind::TSInterfaceDeclaration(_) => {
|
||||||
Some((None, decl.declaration.clone_in(self.ast.allocator)))
|
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