Skip to content

Commit

Permalink
fix(isolated-declarations): false positives for non-exported binding …
Browse files Browse the repository at this point in the history
…elements
  • Loading branch information
Dunqing committed Jun 19, 2024
1 parent b5b3de8 commit f182d7b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
16 changes: 11 additions & 5 deletions crates/oxc_isolated_declarations/src/declaration.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_isolated_declarations/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
@@ -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 }
`----

0 comments on commit f182d7b

Please sign in to comment.