Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(semantic)!: remove name from reference #4329

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_global_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ impl Rule for NoGlobalAssign {
for reference_id in reference_id_list {
let reference = symbol_table.get_reference(reference_id);
if reference.is_write() {
let name = reference.name();
if !self.excludes.contains(name) && ctx.env_contains_var(name) {
let name = ctx.semantic().reference_name(reference);
if !self.excludes.contains(&CompactStr::from(name))
&& ctx.env_contains_var(name)
{
ctx.diagnostic(no_global_assign_diagnostic(name, reference.span()));
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_undef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ impl Rule for NoUndef {
for reference_id_list in ctx.scopes().root_unresolved_references_ids() {
for reference_id in reference_id_list {
let reference = symbol_table.get_reference(reference_id);
let name = reference.name();
let name = ctx.semantic().reference_name(reference);

if ctx.env_contains_var(name) {
continue;
}

if ctx.globals().is_enabled(name.as_str()) {
if ctx.globals().is_enabled(name) {
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_linter/src/rules/jest/no_confusing_set_timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ fn collect_jest_reference_id(

for reference_id in reference_id_list {
let reference = symbol_table.get_reference(reference_id);
if !is_jest_call(reference.name()) {

if !is_jest_call(ctx.semantic().reference_name(reference)) {
continue;
}
let Some(parent_node) = nodes.parent_node(reference.node_id()) else {
Expand Down Expand Up @@ -162,7 +163,7 @@ fn handle_jest_set_time_out<'a>(
continue;
};

if !is_jest_call(reference.name()) {
if !is_jest_call(ctx.semantic().reference_name(reference)) {
if is_jest_fn_call(parent_node, id_to_jest_node_map, ctx) {
for (jest_reference_id, span) in jest_reference_id_list {
if jest_reference_id > &reference_id {
Expand Down
10 changes: 2 additions & 8 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1914,8 +1914,7 @@ impl<'a> SemanticBuilder<'a> {

fn reference_identifier(&mut self, ident: &IdentifierReference<'a>) {
let flag = self.resolve_reference_usages();
let name = ident.name.to_compact_str();
let reference = Reference::new(ident.span, name, self.current_node_id, flag);
let reference = Reference::new(ident.span, self.current_node_id, flag);
let reference_id = self.declare_reference(ident.name.clone(), reference);
ident.reference_id.set(Some(reference_id));
}
Expand All @@ -1939,12 +1938,7 @@ impl<'a> SemanticBuilder<'a> {
Some(AstKind::JSXMemberExpressionObject(_)) => {}
_ => return,
}
let reference = Reference::new(
ident.span,
ident.name.to_compact_str(),
self.current_node_id,
ReferenceFlag::read(),
);
let reference = Reference::new(ident.span, self.current_node_id, ReferenceFlag::read());
self.declare_reference(ident.name.clone(), reference);
}

Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ impl<'a> Semantic<'a> {
pub fn is_reference_to_global_variable(&self, ident: &IdentifierReference) -> bool {
self.scopes().root_unresolved_references().contains_key(ident.name.as_str())
}

pub fn reference_name(&self, reference: &Reference) -> &str {
let node = self.nodes.get_node(reference.node_id());
match node.kind() {
AstKind::IdentifierReference(id) => id.name.as_str(),
AstKind::JSXIdentifier(id) => id.name.as_str(),
_ => unreachable!(),
}
}
}

#[cfg(test)]
Expand Down
15 changes: 4 additions & 11 deletions crates/oxc_semantic/src/reference.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
#![allow(non_snake_case)]

use oxc_span::{CompactStr, Span};
use oxc_span::Span;
pub use oxc_syntax::reference::{ReferenceFlag, ReferenceId};
#[cfg(feature = "serialize")]
use serde::Serialize;
Expand All @@ -15,8 +15,6 @@ use crate::{symbol::SymbolId, AstNodeId};
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
pub struct Reference {
span: Span,
/// The name of the identifier that was referred to
name: CompactStr,
node_id: AstNodeId,
symbol_id: Option<SymbolId>,
/// Describes how this referenced is used by other AST nodes. References can
Expand All @@ -25,28 +23,23 @@ pub struct Reference {
}

impl Reference {
pub fn new(span: Span, name: CompactStr, node_id: AstNodeId, flag: ReferenceFlag) -> Self {
Self { span, name, node_id, symbol_id: None, flag }
pub fn new(span: Span, node_id: AstNodeId, flag: ReferenceFlag) -> Self {
Self { span, node_id, symbol_id: None, flag }
}

pub fn new_with_symbol_id(
span: Span,
name: CompactStr,
node_id: AstNodeId,
symbol_id: SymbolId,
flag: ReferenceFlag,
) -> Self {
Self { span, name, node_id, symbol_id: Some(symbol_id), flag }
Self { span, node_id, symbol_id: Some(symbol_id), flag }
}

pub fn span(&self) -> Span {
self.span
}

pub fn name(&self) -> &CompactStr {
&self.name
}

pub fn node_id(&self) -> AstNodeId {
self.node_id
}
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_semantic/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ fn get_scope_snapshot(semantic: &Semantic, scopes: impl Iterator<Item = ScopeId>
result.push('{');
result.push_str(format!("\"flag\": \"{:?}\",", reference.flag()).as_str());
result.push_str(format!("\"id\": {index:?},").as_str());
result.push_str(format!("\"name\": {:?},", reference.name()).as_str());
result.push_str(
format!("\"name\": {:?},", semantic.reference_name(reference)).as_str(),
);
result.push_str(
format!("\"node_id\": {}", reference.node_id().index()).as_str(),
);
Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,7 @@ struct Assignment<'a> {
impl<'a> Assignment<'a> {
// Creates `this.name = name`
fn create_this_property_assignment(&self, ctx: &mut TraverseCtx<'a>) -> Statement<'a> {
let reference_id = ctx.create_bound_reference(
self.name.to_compact_str(),
self.symbol_id,
ReferenceFlag::Read,
);
let reference_id = ctx.create_bound_reference(self.symbol_id, ReferenceFlag::Read);
let id = IdentifierReference::new_read(self.span, self.name.clone(), Some(reference_id));

ctx.ast.statement_expression(
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_traverse/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,10 @@ impl<'a> TraverseCtx<'a> {
/// This is a shortcut for `ctx.scoping.create_bound_reference`.
pub fn create_bound_reference(
&mut self,
name: CompactStr,
symbol_id: SymbolId,
flag: ReferenceFlag,
) -> ReferenceId {
self.scoping.create_bound_reference(name, symbol_id, flag)
self.scoping.create_bound_reference(symbol_id, flag)
}

/// Create an `IdentifierReference` bound to a `SymbolId`.
Expand Down
10 changes: 4 additions & 6 deletions crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,10 @@ impl TraverseScoping {
/// Create a reference bound to a `SymbolId`
pub fn create_bound_reference(
&mut self,
name: CompactStr,
symbol_id: SymbolId,
flag: ReferenceFlag,
) -> ReferenceId {
let reference =
Reference::new_with_symbol_id(SPAN, name, AstNodeId::DUMMY, symbol_id, flag);
let reference = Reference::new_with_symbol_id(SPAN, AstNodeId::DUMMY, symbol_id, flag);
let reference_id = self.symbols.create_reference(reference);
self.symbols.resolved_references[symbol_id].push(reference_id);
reference_id
Expand All @@ -282,7 +280,7 @@ impl TraverseScoping {
symbol_id: SymbolId,
flag: ReferenceFlag,
) -> IdentifierReference<'a> {
let reference_id = self.create_bound_reference(name.to_compact_str(), symbol_id, flag);
let reference_id = self.create_bound_reference(symbol_id, flag);
IdentifierReference {
span,
name,
Expand All @@ -297,7 +295,7 @@ impl TraverseScoping {
name: CompactStr,
flag: ReferenceFlag,
) -> ReferenceId {
let reference = Reference::new(SPAN, name.clone(), AstNodeId::DUMMY, flag);
let reference = Reference::new(SPAN, AstNodeId::DUMMY, flag);
let reference_id = self.symbols.create_reference(reference);
self.scopes.add_root_unresolved_reference(name, (reference_id, flag));
reference_id
Expand Down Expand Up @@ -330,7 +328,7 @@ impl TraverseScoping {
flag: ReferenceFlag,
) -> ReferenceId {
if let Some(symbol_id) = symbol_id {
self.create_bound_reference(name, symbol_id, flag)
self.create_bound_reference(symbol_id, flag)
} else {
self.create_unbound_reference(name, flag)
}
Expand Down