mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(semantic): root_unresolved_references contain only ReferenceId (#4959)
`ScopeTree::root_unresolved_references` does not need to record `ReferenceFlag` as well as `ReferenceId` - it's never read.
This commit is contained in:
parent
5f074d03c9
commit
59d15c7086
7 changed files with 15 additions and 20 deletions
|
|
@ -52,7 +52,7 @@ impl Rule for NoJasmineGlobals {
|
||||||
.filter(|(key, _)| NON_JASMINE_PROPERTY_NAMES.contains(&key.as_str()));
|
.filter(|(key, _)| NON_JASMINE_PROPERTY_NAMES.contains(&key.as_str()));
|
||||||
|
|
||||||
for (name, reference_ids) in jasmine_references {
|
for (name, reference_ids) in jasmine_references {
|
||||||
for &(reference_id, _) in reference_ids {
|
for &reference_id in reference_ids {
|
||||||
let reference = symbol_table.get_reference(reference_id);
|
let reference = symbol_table.get_reference(reference_id);
|
||||||
if let Some((error, help)) = get_non_jasmine_property_messages(name) {
|
if let Some((error, help)) = get_non_jasmine_property_messages(name) {
|
||||||
ctx.diagnostic(no_jasmine_globals_diagnostic(
|
ctx.diagnostic(no_jasmine_globals_diagnostic(
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ impl Rule for NoMocksImport {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
for (reference_id, _) in require_reference_ids {
|
for reference_id in require_reference_ids {
|
||||||
let reference = ctx.symbols().get_reference(*reference_id);
|
let reference = ctx.symbols().get_reference(*reference_id);
|
||||||
let Some(parent) = ctx.nodes().parent_node(reference.node_id()) else {
|
let Some(parent) = ctx.nodes().parent_node(reference.node_id()) else {
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use oxc_ast::{
|
||||||
},
|
},
|
||||||
AstKind,
|
AstKind,
|
||||||
};
|
};
|
||||||
use oxc_semantic::{AstNode, ReferenceFlag, ReferenceId};
|
use oxc_semantic::{AstNode, ReferenceId};
|
||||||
use phf::phf_set;
|
use phf::phf_set;
|
||||||
|
|
||||||
use crate::LintContext;
|
use crate::LintContext;
|
||||||
|
|
@ -162,7 +162,7 @@ pub fn collect_possible_jest_call_node<'a, 'b>(
|
||||||
collect_ids_referenced_to_global(ctx)
|
collect_ids_referenced_to_global(ctx)
|
||||||
.iter()
|
.iter()
|
||||||
// set the original of global test function to None
|
// set the original of global test function to None
|
||||||
.map(|(id, _)| (*id, None)),
|
.map(|&id| (id, None)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -239,13 +239,13 @@ fn find_original_name<'a>(import_decl: &'a ImportDeclaration<'a>, name: &str) ->
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_ids_referenced_to_global(ctx: &LintContext) -> Vec<(ReferenceId, ReferenceFlag)> {
|
fn collect_ids_referenced_to_global(ctx: &LintContext) -> Vec<ReferenceId> {
|
||||||
ctx.scopes()
|
ctx.scopes()
|
||||||
.root_unresolved_references()
|
.root_unresolved_references()
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(name, _)| JEST_METHOD_NAMES.contains(name.as_str()))
|
.filter(|(name, _)| JEST_METHOD_NAMES.contains(name.as_str()))
|
||||||
.flat_map(|(_, reference_ids)| reference_ids.clone())
|
.flat_map(|(_, reference_ids)| reference_ids.clone())
|
||||||
.collect::<Vec<(ReferenceId, ReferenceFlag)>>()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// join name of the expression. e.g.
|
/// join name of the expression. e.g.
|
||||||
|
|
|
||||||
|
|
@ -256,7 +256,7 @@ impl<'a> SemanticBuilder<'a> {
|
||||||
.unresolved_references
|
.unresolved_references
|
||||||
.into_root()
|
.into_root()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(k, v)| (k.into(), v))
|
.map(|(k, v)| (k.into(), v.into_iter().map(|(reference_id, _)| reference_id).collect()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let jsdoc = if self.build_jsdoc { self.jsdoc.build() } else { JSDocFinder::default() };
|
let jsdoc = if self.build_jsdoc { self.jsdoc.build() } else { JSDocFinder::default() };
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use std::hash::BuildHasherDefault;
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use oxc_index::IndexVec;
|
use oxc_index::IndexVec;
|
||||||
use oxc_span::CompactStr;
|
use oxc_span::CompactStr;
|
||||||
use oxc_syntax::reference::{ReferenceFlag, ReferenceId};
|
use oxc_syntax::reference::ReferenceId;
|
||||||
pub use oxc_syntax::scope::{ScopeFlags, ScopeId};
|
pub use oxc_syntax::scope::{ScopeFlags, ScopeId};
|
||||||
use rustc_hash::{FxHashMap, FxHasher};
|
use rustc_hash::{FxHashMap, FxHasher};
|
||||||
|
|
||||||
|
|
@ -12,8 +12,7 @@ use crate::{symbol::SymbolId, AstNodeId};
|
||||||
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
|
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
|
||||||
|
|
||||||
pub(crate) type Bindings = FxIndexMap<CompactStr, SymbolId>;
|
pub(crate) type Bindings = FxIndexMap<CompactStr, SymbolId>;
|
||||||
pub(crate) type UnresolvedReference = (ReferenceId, ReferenceFlag);
|
pub type UnresolvedReferences = FxHashMap<CompactStr, Vec<ReferenceId>>;
|
||||||
pub type UnresolvedReferences = FxHashMap<CompactStr, Vec<UnresolvedReference>>;
|
|
||||||
|
|
||||||
/// Scope Tree
|
/// Scope Tree
|
||||||
///
|
///
|
||||||
|
|
@ -144,7 +143,7 @@ impl ScopeTree {
|
||||||
pub fn root_unresolved_references_ids(
|
pub fn root_unresolved_references_ids(
|
||||||
&self,
|
&self,
|
||||||
) -> impl Iterator<Item = impl Iterator<Item = ReferenceId> + '_> + '_ {
|
) -> impl Iterator<Item = impl Iterator<Item = ReferenceId> + '_> + '_ {
|
||||||
self.root_unresolved_references.values().map(|v| v.iter().map(|(id, _)| *id))
|
self.root_unresolved_references.values().map(|v| v.iter().copied())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
@ -193,12 +192,8 @@ impl ScopeTree {
|
||||||
self.get_binding(self.root_scope_id(), name)
|
self.get_binding(self.root_scope_id(), name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_root_unresolved_reference(
|
pub fn add_root_unresolved_reference(&mut self, name: CompactStr, reference_id: ReferenceId) {
|
||||||
&mut self,
|
self.root_unresolved_references.entry(name).or_default().push(reference_id);
|
||||||
name: CompactStr,
|
|
||||||
reference: UnresolvedReference,
|
|
||||||
) {
|
|
||||||
self.root_unresolved_references.entry(name).or_default().push(reference);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if a symbol is declared in a certain scope.
|
/// Check if a symbol is declared in a certain scope.
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ use assert_unchecked::assert_unchecked;
|
||||||
use oxc_span::Atom;
|
use oxc_span::Atom;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::scope::UnresolvedReference;
|
use oxc_syntax::reference::{ReferenceFlag, ReferenceId};
|
||||||
|
|
||||||
/// The difference with Scope's `UnresolvedReferences` is that this type uses Atom as the key. its clone is very cheap!
|
/// The difference with Scope's `UnresolvedReferences` is that this type uses Atom as the key. its clone is very cheap!
|
||||||
type TempUnresolvedReferences<'a> = FxHashMap<Atom<'a>, Vec<UnresolvedReference>>;
|
type TempUnresolvedReferences<'a> = FxHashMap<Atom<'a>, Vec<(ReferenceId, ReferenceFlag)>>;
|
||||||
|
|
||||||
// Stack used to accumulate unresolved refs while traversing scopes.
|
// Stack used to accumulate unresolved refs while traversing scopes.
|
||||||
// Indexed by scope depth. We recycle `UnresolvedReferences` instances during traversal
|
// Indexed by scope depth. We recycle `UnresolvedReferences` instances during traversal
|
||||||
|
|
|
||||||
|
|
@ -302,7 +302,7 @@ impl TraverseScoping {
|
||||||
) -> ReferenceId {
|
) -> ReferenceId {
|
||||||
let reference = Reference::new(AstNodeId::DUMMY, flag);
|
let reference = Reference::new(AstNodeId::DUMMY, flag);
|
||||||
let reference_id = self.symbols.create_reference(reference);
|
let reference_id = self.symbols.create_reference(reference);
|
||||||
self.scopes.add_root_unresolved_reference(name, (reference_id, flag));
|
self.scopes.add_root_unresolved_reference(name, reference_id);
|
||||||
reference_id
|
reference_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue