Skip to content

Commit ceebcc0

Browse files
committed
feat(semantic): introduce Redeclaraion for symbol_declarations
1 parent 0a6af60 commit ceebcc0

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

crates/oxc_linter/src/rules/eslint/no_redeclare.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ impl Rule for NoRedeclare {
8282
ctx.diagnostic(no_redeclare_as_builtin_in_diagnostic(name, decl_span));
8383
}
8484

85-
for span in ctx.scoping().symbol_redeclarations(symbol_id) {
85+
for rd in ctx.scoping().symbol_redeclarations(symbol_id) {
8686
if is_builtin {
87-
ctx.diagnostic(no_redeclare_as_builtin_in_diagnostic(name, *span));
87+
ctx.diagnostic(no_redeclare_as_builtin_in_diagnostic(name, rd.span));
8888
} else {
89-
ctx.diagnostic(no_redeclare_diagnostic(name, decl_span, *span));
89+
ctx.diagnostic(no_redeclare_diagnostic(name, decl_span, rd.span));
9090
}
9191
}
9292
}

crates/oxc_linter/src/rules/eslint/no_shadow_restricted_names.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ impl Rule for NoShadowRestrictedNames {
101101
let span = ctx.scoping().symbol_span(symbol_id);
102102
ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, span));
103103

104-
for &span in ctx.scoping().symbol_redeclarations(symbol_id) {
105-
ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, span));
104+
for rd in ctx.scoping().symbol_redeclarations(symbol_id) {
105+
ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, rd.span));
106106
}
107107
}
108108
}

crates/oxc_semantic/src/scoping.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ use oxc_syntax::{
1515
pub type Bindings<'a> = ArenaHashMap<'a, &'a str, SymbolId>;
1616
pub type UnresolvedReferences<'a> = ArenaHashMap<'a, &'a str, ArenaVec<'a, ReferenceId>>;
1717

18+
#[derive(Debug)]
19+
pub struct Redeclaration {
20+
pub span: Span,
21+
}
22+
1823
/// # Symbol Table and Scope Tree
1924
///
2025
/// ## Symbol Table
@@ -104,7 +109,7 @@ pub struct ScopingInner<'cell> {
104109
/* Symbol Table Fields */
105110
symbol_names: ArenaVec<'cell, Atom<'cell>>,
106111
resolved_references: ArenaVec<'cell, ArenaVec<'cell, ReferenceId>>,
107-
symbol_redeclarations: FxHashMap<SymbolId, ArenaVec<'cell, Span>>,
112+
symbol_redeclarations: FxHashMap<SymbolId, ArenaVec<'cell, Redeclaration>>,
108113
/* Scope Tree Fields */
109114
/// Symbol bindings in a scope.
110115
///
@@ -193,13 +198,13 @@ impl Scoping {
193198
}
194199

195200
#[inline]
196-
pub fn symbol_redeclarations(&self, symbol_id: SymbolId) -> &[Span] {
201+
pub fn symbol_redeclarations(&self, symbol_id: SymbolId) -> &[Redeclaration] {
197202
self.cell.borrow_dependent().symbol_redeclarations.get(&symbol_id).map_or_else(
198203
|| {
199-
static EMPTY: &[Span] = &[];
204+
static EMPTY: &[Redeclaration] = &[];
200205
EMPTY
201206
},
202-
|spans| spans.as_slice(),
207+
|v| v.as_slice(),
203208
)
204209
}
205210

@@ -253,12 +258,14 @@ impl Scoping {
253258

254259
pub fn add_symbol_redeclaration(&mut self, symbol_id: SymbolId, span: Span) {
255260
self.cell.with_dependent_mut(|allocator, cell| {
261+
let redeclaration = Redeclaration { span };
256262
match cell.symbol_redeclarations.entry(symbol_id) {
257-
Entry::Occupied(mut entry) => {
258-
entry.get_mut().push(span);
263+
Entry::Occupied(occupied) => {
264+
occupied.into_mut().push(redeclaration);
259265
}
260-
Entry::Vacant(entry) => {
261-
entry.insert(ArenaVec::from_array_in([span], allocator));
266+
Entry::Vacant(vacant) => {
267+
let v = ArenaVec::from_array_in([redeclaration], allocator);
268+
vacant.insert(v);
262269
}
263270
}
264271
});
@@ -790,8 +797,11 @@ impl Scoping {
790797
symbol_redeclarations: cell
791798
.symbol_redeclarations
792799
.iter()
793-
.map(|(k, v)| (*k, v.clone_in_with_semantic_ids(allocator)))
794-
.collect(),
800+
.map(|(k, v)| {
801+
let v = v.iter().map(|r| Redeclaration { span: r.span });
802+
(*k, ArenaVec::from_iter_in(v, allocator))
803+
})
804+
.collect::<FxHashMap<_, _>>(),
795805
bindings: cell
796806
.bindings
797807
.iter()

crates/oxc_transformer/src/typescript/namespace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ impl<'a> TypeScriptNamespace<'a, '_> {
468468
// namespace Foo {} // is redeclaration
469469
// ```
470470
let redeclarations = ctx.scoping().symbol_redeclarations(symbol_id);
471-
!redeclarations.is_empty() && redeclarations.contains(&id.span)
471+
!redeclarations.is_empty() && redeclarations.iter().any(|rd| rd.span == id.span)
472472
}
473473
}
474474
}

tasks/transform_checker/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,11 @@ impl PostTransformChecker<'_, '_> {
427427

428428
// Check redeclarations match
429429
let redeclaration_spans = self.get_pair(symbol_ids, |scoping, symbol_id| {
430-
let mut spans = scoping.symbol_redeclarations(symbol_id).to_vec();
430+
let mut spans = scoping
431+
.symbol_redeclarations(symbol_id)
432+
.iter()
433+
.map(|rd| rd.span)
434+
.collect::<Vec<_>>();
431435
spans.sort_unstable();
432436
spans
433437
});

0 commit comments

Comments
 (0)