Skip to content

Commit

Permalink
refactor(syntax): rename ReferenceFlag to ReferenceFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 20, 2024
1 parent c30e2e9 commit 29eec4b
Show file tree
Hide file tree
Showing 252 changed files with 502 additions and 493 deletions.
6 changes: 3 additions & 3 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use oxc_syntax::{
operator::{
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
},
reference::{ReferenceFlag, ReferenceId},
reference::{ReferenceFlags, ReferenceId},
scope::ScopeId,
symbol::SymbolId,
};
Expand Down Expand Up @@ -242,10 +242,10 @@ pub struct IdentifierReference<'a> {
/// Flags indicating how the reference is used.
///
/// This gets set in the bind step of semantic analysis, and will always be
/// [`ReferenceFlag::None`] immediately after parsing.
/// [`ReferenceFlags::None`] immediately after parsing.
#[serde(skip)]
#[clone_in(default)]
pub reference_flag: ReferenceFlag,
pub reference_flag: ReferenceFlags,
}

/// `x` in `const x = 0;`
Expand Down
11 changes: 8 additions & 3 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxc_allocator::{Box, FromIn, Vec};
use oxc_span::{Atom, GetSpan, SourceType, Span};
use oxc_syntax::{
operator::UnaryOperator,
reference::{ReferenceFlag, ReferenceId},
reference::{ReferenceFlags, ReferenceId},
scope::ScopeFlags,
};

Expand Down Expand Up @@ -330,15 +330,20 @@ impl<'a> Hash for IdentifierReference<'a> {

impl<'a> IdentifierReference<'a> {
pub fn new(span: Span, name: Atom<'a>) -> Self {
Self { span, name, reference_id: Cell::default(), reference_flag: ReferenceFlag::default() }
Self {
span,
name,
reference_id: Cell::default(),
reference_flag: ReferenceFlags::default(),
}
}

pub fn new_read(span: Span, name: Atom<'a>, reference_id: Option<ReferenceId>) -> Self {
Self {
span,
name,
reference_id: Cell::new(reference_id),
reference_flag: ReferenceFlag::Read,
reference_flag: ReferenceFlags::Read,
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_minifier/src/node_util/check_for_state_change.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use oxc_ast::ast::*;

use oxc_semantic::ReferenceFlag;
use oxc_semantic::ReferenceFlags;
use oxc_syntax::operator::UnaryOperator;

/// A "simple" operator is one whose children are expressions, has no direct side-effects.
Expand Down Expand Up @@ -33,7 +33,7 @@ impl<'a, 'b> CheckForStateChange<'a, 'b> for Expression<'a> {
.expressions
.iter()
.any(|expr| expr.check_for_state_change(check_for_new_objects)),
Self::Identifier(ident) => ident.reference_flag == ReferenceFlag::Write,
Self::Identifier(ident) => ident.reference_flag == ReferenceFlags::Write,
Self::UnaryExpression(unary_expr) => {
unary_expr.check_for_state_change(check_for_new_objects)
}
Expand Down
64 changes: 32 additions & 32 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
label::LabelBuilder,
module_record::ModuleRecordBuilder,
node::{AstNodeId, AstNodes, NodeFlags},
reference::{Reference, ReferenceFlag, ReferenceId},
reference::{Reference, ReferenceFlags, ReferenceId},
scope::{Bindings, ScopeFlags, ScopeId, ScopeTree},
symbol::{SymbolFlags, SymbolId, SymbolTable},
unresolved_stack::UnresolvedReferencesStack,
Expand Down Expand Up @@ -82,7 +82,7 @@ pub struct SemanticBuilder<'a> {
// and when we reach a value declaration we set it
// to value like
pub(crate) namespace_stack: Vec<SymbolId>,
current_reference_flag: ReferenceFlag,
current_reference_flag: ReferenceFlags,
pub(crate) hoisting_variables: FxHashMap<ScopeId, FxHashMap<Atom<'a>, SymbolId>>,

// builders
Expand Down Expand Up @@ -131,7 +131,7 @@ impl<'a> SemanticBuilder<'a> {
current_node_id: AstNodeId::new(0),
current_node_flags: NodeFlags::empty(),
current_symbol_flags: SymbolFlags::empty(),
current_reference_flag: ReferenceFlag::empty(),
current_reference_flag: ReferenceFlags::empty(),
current_scope_id,
function_stack: vec![],
namespace_stack: vec![],
Expand Down Expand Up @@ -478,18 +478,18 @@ impl<'a> SemanticBuilder<'a> {
// The non type-only ExportSpecifier can reference a type/value symbol,
// If the symbol is a value symbol and reference flag is not type-only, remove the type flag.
if symbol_flag.is_value() && !flag.is_type_only() {
*reference.flag_mut() -= ReferenceFlag::Type;
*reference.flag_mut() -= ReferenceFlags::Type;
} else {
// If the symbol is a type symbol and reference flag is not type-only, remove the value flag.
*reference.flag_mut() -= ReferenceFlag::Value;
*reference.flag_mut() -= ReferenceFlags::Value;
}

// import type { T } from './mod'; type A = typeof T
// ^ can reference type-only import
// If symbol is type-import, we need to replace the ReferenceFlag::Value with ReferenceFlag::Type
// If symbol is type-import, we need to replace the ReferenceFlags::Value with ReferenceFlags::Type
if flag.is_ts_type_query() && symbol_flag.is_type_import() {
*reference.flag_mut() -= ReferenceFlag::Value;
*reference.flag_mut() |= ReferenceFlag::Type;
*reference.flag_mut() -= ReferenceFlags::Value;
*reference.flag_mut() |= ReferenceFlags::Type;
}

reference.set_symbol_id(symbol_id);
Expand Down Expand Up @@ -1743,14 +1743,14 @@ impl<'a> SemanticBuilder<'a> {
AstKind::ExportNamedDeclaration(decl) => {
self.current_symbol_flags |= SymbolFlags::Export;
if decl.export_kind.is_type() {
self.current_reference_flag = ReferenceFlag::Type;
self.current_reference_flag = ReferenceFlags::Type;
}
}
AstKind::ExportSpecifier(s) => {
if self.current_reference_flag.is_type() || s.export_kind.is_type() {
self.current_reference_flag = ReferenceFlag::Type;
self.current_reference_flag = ReferenceFlags::Type;
} else {
self.current_reference_flag = ReferenceFlag::Read | ReferenceFlag::Type;
self.current_reference_flag = ReferenceFlags::Read | ReferenceFlags::Type;
}
}
AstKind::ImportSpecifier(specifier) => {
Expand Down Expand Up @@ -1839,17 +1839,17 @@ impl<'a> SemanticBuilder<'a> {
type_parameter.bind(self);
}
AstKind::TSInterfaceHeritage(_) => {
self.current_reference_flag = ReferenceFlag::Type;
self.current_reference_flag = ReferenceFlags::Type;
}
AstKind::TSTypeQuery(_) => {
// type A = typeof a;
// ^^^^^^^^
self.current_reference_flag = ReferenceFlag::Read | ReferenceFlag::TSTypeQuery;
self.current_reference_flag = ReferenceFlags::Read | ReferenceFlags::TSTypeQuery;
}
AstKind::TSTypeParameterInstantiation(_) => {
// type A<T> = typeof a<T>;
// ^^^ avoid treat T as a value and TSTypeQuery
self.current_reference_flag -= ReferenceFlag::Read | ReferenceFlag::TSTypeQuery;
self.current_reference_flag -= ReferenceFlags::Read | ReferenceFlags::TSTypeQuery;
}
AstKind::TSTypeName(_) => {
match self.nodes.parent_kind(self.current_node_id) {
Expand All @@ -1858,15 +1858,15 @@ impl<'a> SemanticBuilder<'a> {
// ^
AstKind::TSModuleReference(_),
) => {
self.current_reference_flag = ReferenceFlag::Read;
self.current_reference_flag = ReferenceFlags::Read;
}
Some(AstKind::TSQualifiedName(_)) => {
// import A = a.b
// ^^^ Keep the current reference flag
}
_ => {
if !self.current_reference_flag.is_ts_type_query() {
self.current_reference_flag = ReferenceFlag::Type;
self.current_reference_flag = ReferenceFlags::Type;
}
}
}
Expand All @@ -1875,7 +1875,7 @@ impl<'a> SemanticBuilder<'a> {
// export = a;
// ^ can reference value or type
if export.expression.is_identifier_reference() {
self.current_reference_flag = ReferenceFlag::Read | ReferenceFlag::Type;
self.current_reference_flag = ReferenceFlags::Read | ReferenceFlags::Type;
}
}
AstKind::IdentifierReference(ident) => {
Expand All @@ -1888,24 +1888,24 @@ impl<'a> SemanticBuilder<'a> {
if !self.current_reference_flag.is_type()
&& self.is_not_expression_statement_parent()
{
self.current_reference_flag |= ReferenceFlag::Read;
self.current_reference_flag |= ReferenceFlags::Read;
}
self.current_reference_flag |= ReferenceFlag::Write;
self.current_reference_flag |= ReferenceFlags::Write;
}
AstKind::AssignmentExpression(expr) => {
if expr.operator != AssignmentOperator::Assign
|| self.is_not_expression_statement_parent()
{
self.current_reference_flag |= ReferenceFlag::Read;
self.current_reference_flag |= ReferenceFlags::Read;
}
}
AstKind::MemberExpression(_) => {
if !self.current_reference_flag.is_type() {
self.current_reference_flag = ReferenceFlag::Read;
self.current_reference_flag = ReferenceFlags::Read;
}
}
AstKind::AssignmentTarget(_) => {
self.current_reference_flag |= ReferenceFlag::Write;
self.current_reference_flag |= ReferenceFlags::Write;
}
AstKind::LabeledStatement(stmt) => {
self.label_builder.enter(stmt, self.current_node_id);
Expand Down Expand Up @@ -1938,7 +1938,7 @@ impl<'a> SemanticBuilder<'a> {
}
AstKind::ExportSpecifier(_) => {
if !self.current_reference_flag.is_type_only() {
self.current_reference_flag = ReferenceFlag::empty();
self.current_reference_flag = ReferenceFlags::empty();
}
}
AstKind::LabeledStatement(_) => self.label_builder.leave(),
Expand Down Expand Up @@ -1968,27 +1968,27 @@ impl<'a> SemanticBuilder<'a> {
self.namespace_stack.pop();
}
AstKind::TSTypeName(_) => {
self.current_reference_flag -= ReferenceFlag::Type;
self.current_reference_flag -= ReferenceFlags::Type;
}
AstKind::UpdateExpression(_) => {
if self.is_not_expression_statement_parent() {
self.current_reference_flag -= ReferenceFlag::Read;
self.current_reference_flag -= ReferenceFlags::Read;
}
self.current_reference_flag -= ReferenceFlag::Write;
self.current_reference_flag -= ReferenceFlags::Write;
}
AstKind::AssignmentExpression(expr) => {
if expr.operator != AssignmentOperator::Assign
|| self.is_not_expression_statement_parent()
{
self.current_reference_flag -= ReferenceFlag::Read;
self.current_reference_flag -= ReferenceFlags::Read;
}
}
AstKind::MemberExpression(_)
| AstKind::TSTypeQuery(_)
| AstKind::ExportNamedDeclaration(_) => {
self.current_reference_flag = ReferenceFlag::empty();
self.current_reference_flag = ReferenceFlags::empty();
}
AstKind::AssignmentTarget(_) => self.current_reference_flag -= ReferenceFlag::Write,
AstKind::AssignmentTarget(_) => self.current_reference_flag -= ReferenceFlags::Write,
_ => {}
}
}
Expand All @@ -2011,9 +2011,9 @@ impl<'a> SemanticBuilder<'a> {
}

/// Resolve reference flags for the current ast node.
fn resolve_reference_usages(&self) -> ReferenceFlag {
fn resolve_reference_usages(&self) -> ReferenceFlags {
if self.current_reference_flag.is_empty() {
ReferenceFlag::Read
ReferenceFlags::Read
} else {
self.current_reference_flag
}
Expand All @@ -2029,7 +2029,7 @@ impl<'a> SemanticBuilder<'a> {
Some(AstKind::JSXMemberExpressionObject(_)) => {}
_ => return,
}
let reference = Reference::new(self.current_node_id, ReferenceFlag::read());
let reference = Reference::new(self.current_node_id, ReferenceFlags::read());
self.declare_reference(ident.name.clone(), reference);
}

Expand Down
Loading

0 comments on commit 29eec4b

Please sign in to comment.