fix(isolated-declarations): false positives for non-exported binding elements (#3751)

This commit is contained in:
Dunqing 2024-06-19 14:06:08 +00:00
parent df9971da4c
commit bf1c250d56
4 changed files with 61 additions and 5 deletions

View file

@ -1,13 +1,15 @@
use oxc_allocator::Box;
#[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*;
use oxc_ast::Visit;
use oxc_ast::{syntax_directed_operations::BoundNames, Visit};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{GetSpan, SPAN};
use oxc_syntax::scope::ScopeFlags;
use crate::{
diagnostics::{inferred_type_of_expression, signature_computed_property_name},
diagnostics::{
binding_element_export, inferred_type_of_expression, signature_computed_property_name,
},
IsolatedDeclarations,
};
@ -47,9 +49,13 @@ impl<'a> IsolatedDeclarations<'a> {
check_binding: bool,
) -> Option<VariableDeclarator<'a>> {
if decl.id.kind.is_destructuring_pattern() {
self.error(OxcDiagnostic::error(
"Binding elements can't be exported directly with --isolatedDeclarations.",
));
if check_binding {
decl.id.bound_names(&mut |id| {
if self.scope.has_reference(&id.name) {
self.error(binding_element_export(id.span));
}
});
}
return None;
}

View file

@ -95,3 +95,8 @@ pub fn implicitly_adding_undefined_to_type(span: Span) -> OxcDiagnostic {
)
.with_label(span)
}
pub fn binding_element_export(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Binding elements can't be exported directly with --isolatedDeclarations.")
.with_label(span)
}

View file

@ -0,0 +1,10 @@
// Correct
const [A, B] = [1, 2, 3];
export function foo(): number {
return A;
}
// Incorrect
const { c, d } = { c: 1, d: 2 };
const [ e ] = [4];
export { c, d, e }

View file

@ -0,0 +1,35 @@
---
source: crates/oxc_isolated_declarations/tests/mod.rs
input_file: crates/oxc_isolated_declarations/tests/fixtures/non-exported-binding-elements.ts
---
==================== .D.TS ====================
export declare function foo(): number;
export { c, d, e };
==================== Errors ====================
x Binding elements can't be exported directly with --isolatedDeclarations.
,-[8:9]
7 | // Incorrect
8 | const { c, d } = { c: 1, d: 2 };
: ^
9 | const [ e ] = [4];
`----
x Binding elements can't be exported directly with --isolatedDeclarations.
,-[8:12]
7 | // Incorrect
8 | const { c, d } = { c: 1, d: 2 };
: ^
9 | const [ e ] = [4];
`----
x Binding elements can't be exported directly with --isolatedDeclarations.
,-[9:9]
8 | const { c, d } = { c: 1, d: 2 };
9 | const [ e ] = [4];
: ^
10 | export { c, d, e }
`----