Skip to content

Commit

Permalink
fix(transformer/typescript) shadowed imports have not been removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jul 30, 2024
1 parent 3b60b99 commit 7edaefb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
10 changes: 10 additions & 0 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{cell::Cell, rc::Rc};

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

pub fn has_value_reference(&self, name: &str, ctx: &TraverseCtx<'a>) -> bool {
if let Some(symbol_id) = ctx.scopes().get_root_binding(name) {
// `import T from 'mod'; const T = 1;` The T has a value redeclaration
// `import T from 'mod'; type T = number;` The T has a type redeclaration
// If there is still a value symbol after SymbolFlags::Import is removed, then it's a value redeclaration.
// That means the import is shadowed, and we can safely remove the import.
let has_value_redeclaration =
(ctx.symbols().get_flag(symbol_id) - SymbolFlags::Import).is_value();
if has_value_redeclaration {
return false;
}
if ctx
.symbols()
.get_resolved_references(symbol_id)
Expand Down
2 changes: 1 addition & 1 deletion tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: 12619ffe

Passed: 7/7
Passed: 8/8

# All Passed:
* babel-plugin-transform-typescript
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// CASE 1: redeclaration of VariableDeclaration
import { A } from './a';
const A: A = 0;
export {A};

// CASE 2: redeclaration of TypeAlias
import { T } from "./t";
type T = number;
export { T }

// CASE 3: redeclaration of VariableDeclaration and TypeAlias
import { B } from './b';
const B: B = 0;
type B = number;
export { B }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// CASE 1: redeclaration of VariableDeclaration
const A = 0;
export { A };

// CASE 2: redeclaration of TypeAlias
import { T } from "./t";
export { T };

// CASE 3: redeclaration of VariableDeclaration and TypeAlias
const B = 0;
export { B };

0 comments on commit 7edaefb

Please sign in to comment.