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:
overlookmotel 2024-08-19 01:32:04 +00:00
parent 5f074d03c9
commit 59d15c7086
7 changed files with 15 additions and 20 deletions

View file

@ -52,7 +52,7 @@ impl Rule for NoJasmineGlobals {
.filter(|(key, _)| NON_JASMINE_PROPERTY_NAMES.contains(&key.as_str()));
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);
if let Some((error, help)) = get_non_jasmine_property_messages(name) {
ctx.diagnostic(no_jasmine_globals_diagnostic(

View file

@ -48,7 +48,7 @@ impl Rule for NoMocksImport {
return;
};
for (reference_id, _) in require_reference_ids {
for reference_id in require_reference_ids {
let reference = ctx.symbols().get_reference(*reference_id);
let Some(parent) = ctx.nodes().parent_node(reference.node_id()) else {
return;

View file

@ -7,7 +7,7 @@ use oxc_ast::{
},
AstKind,
};
use oxc_semantic::{AstNode, ReferenceFlag, ReferenceId};
use oxc_semantic::{AstNode, ReferenceId};
use phf::phf_set;
use crate::LintContext;
@ -162,7 +162,7 @@ pub fn collect_possible_jest_call_node<'a, 'b>(
collect_ids_referenced_to_global(ctx)
.iter()
// 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()
.root_unresolved_references()
.iter()
.filter(|(name, _)| JEST_METHOD_NAMES.contains(name.as_str()))
.flat_map(|(_, reference_ids)| reference_ids.clone())
.collect::<Vec<(ReferenceId, ReferenceFlag)>>()
.collect()
}
/// join name of the expression. e.g.

View file

@ -256,7 +256,7 @@ impl<'a> SemanticBuilder<'a> {
.unresolved_references
.into_root()
.into_iter()
.map(|(k, v)| (k.into(), v))
.map(|(k, v)| (k.into(), v.into_iter().map(|(reference_id, _)| reference_id).collect()))
.collect();
let jsdoc = if self.build_jsdoc { self.jsdoc.build() } else { JSDocFinder::default() };

View file

@ -3,7 +3,7 @@ use std::hash::BuildHasherDefault;
use indexmap::IndexMap;
use oxc_index::IndexVec;
use oxc_span::CompactStr;
use oxc_syntax::reference::{ReferenceFlag, ReferenceId};
use oxc_syntax::reference::ReferenceId;
pub use oxc_syntax::scope::{ScopeFlags, ScopeId};
use rustc_hash::{FxHashMap, FxHasher};
@ -12,8 +12,7 @@ use crate::{symbol::SymbolId, AstNodeId};
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
pub(crate) type Bindings = FxIndexMap<CompactStr, SymbolId>;
pub(crate) type UnresolvedReference = (ReferenceId, ReferenceFlag);
pub type UnresolvedReferences = FxHashMap<CompactStr, Vec<UnresolvedReference>>;
pub type UnresolvedReferences = FxHashMap<CompactStr, Vec<ReferenceId>>;
/// Scope Tree
///
@ -144,7 +143,7 @@ impl ScopeTree {
pub fn root_unresolved_references_ids(
&self,
) -> 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]
@ -193,12 +192,8 @@ impl ScopeTree {
self.get_binding(self.root_scope_id(), name)
}
pub fn add_root_unresolved_reference(
&mut self,
name: CompactStr,
reference: UnresolvedReference,
) {
self.root_unresolved_references.entry(name).or_default().push(reference);
pub fn add_root_unresolved_reference(&mut self, name: CompactStr, reference_id: ReferenceId) {
self.root_unresolved_references.entry(name).or_default().push(reference_id);
}
/// Check if a symbol is declared in a certain scope.

View file

@ -2,10 +2,10 @@ use assert_unchecked::assert_unchecked;
use oxc_span::Atom;
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!
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.
// Indexed by scope depth. We recycle `UnresolvedReferences` instances during traversal

View file

@ -302,7 +302,7 @@ impl TraverseScoping {
) -> ReferenceId {
let reference = Reference::new(AstNodeId::DUMMY, flag);
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
}