Skip to content

Commit

Permalink
refactor(semantic): root_unresolved_references contain only `Refere…
Browse files Browse the repository at this point in the history
…nceId` (#4959)

`ScopeTree::root_unresolved_references` does not need to record `ReferenceFlag` as well as `ReferenceId` - it's never read.
  • Loading branch information
overlookmotel committed Aug 19, 2024
1 parent 5f074d0 commit 59d15c7
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/no_jasmine_globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/no_mocks_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/utils/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)),
);
}

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
Expand Down
15 changes: 5 additions & 10 deletions crates/oxc_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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
///
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_semantic/src/unresolved_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 59d15c7

Please sign in to comment.