Skip to content

Commit 48031ad

Browse files
committed
fix(transformer/typescript) shadowed imports have not been removed (#4550)
close: #4423
1 parent 4efd54b commit 48031ad

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

crates/oxc_transformer/src/typescript/annotations.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{cell::Cell, rc::Rc};
44

55
use oxc_allocator::Vec as ArenaVec;
66
use oxc_ast::ast::*;
7+
use oxc_semantic::SymbolFlags;
78
use oxc_span::{Atom, GetSpan, Span, SPAN};
89
use oxc_syntax::{
910
operator::AssignmentOperator, reference::ReferenceFlag, scope::ScopeFlags, symbol::SymbolId,
@@ -496,6 +497,15 @@ impl<'a> TypeScriptAnnotations<'a> {
496497

497498
pub fn has_value_reference(&self, name: &str, ctx: &TraverseCtx<'a>) -> bool {
498499
if let Some(symbol_id) = ctx.scopes().get_root_binding(name) {
500+
// `import T from 'mod'; const T = 1;` The T has a value redeclaration
501+
// `import T from 'mod'; type T = number;` The T has a type redeclaration
502+
// If the symbol is still a value symbol after SymbolFlags::Import is removed, then it's a value redeclaration.
503+
// That means the import is shadowed, and we can safely remove the import.
504+
let has_value_redeclaration =
505+
(ctx.symbols().get_flag(symbol_id) - SymbolFlags::Import).is_value();
506+
if has_value_redeclaration {
507+
return false;
508+
}
499509
if ctx
500510
.symbols()
501511
.get_resolved_references(symbol_id)

tasks/transform_conformance/oxc.snap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
commit: 12619ffe
22

3-
Passed: 4/7
3+
Passed: 5/8
44

55
# All Passed:
66
* babel-plugin-transform-react-jsx
77

88

9-
# babel-plugin-transform-typescript (3/6)
9+
# babel-plugin-transform-typescript (4/7)
1010
* computed-constant-value/input.ts
1111
* enum-member-reference/input.ts
1212
* export-elimination/input.ts
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// CASE 1: redeclaration of VariableDeclaration
2+
import { A } from './a';
3+
const A: A = 0;
4+
export {A};
5+
6+
// CASE 2: redeclaration of TypeAlias
7+
import { T } from "./t";
8+
type T = number;
9+
export { T }
10+
11+
// CASE 3: redeclaration of VariableDeclaration and TypeAlias
12+
import { B } from './b';
13+
const B: B = 0;
14+
type B = number;
15+
export { B }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// CASE 1: redeclaration of VariableDeclaration
2+
const A = 0;
3+
export { A };
4+
5+
// CASE 2: redeclaration of TypeAlias
6+
import { T } from "./t";
7+
export { T };
8+
9+
// CASE 3: redeclaration of VariableDeclaration and TypeAlias
10+
const B = 0;
11+
export { B };
12+

0 commit comments

Comments
 (0)