Skip to content

Commit

Permalink
refactor(semantic): s/AstNodeId/NodeId
Browse files Browse the repository at this point in the history
part of #5689
  • Loading branch information
Boshen committed Sep 13, 2024
1 parent 71116a1 commit f7df926
Show file tree
Hide file tree
Showing 45 changed files with 176 additions and 188 deletions.
22 changes: 11 additions & 11 deletions crates/oxc_cfg/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod context;

use context::Ctx;
pub use context::{CtxCursor, CtxFlags};
use oxc_syntax::node::AstNodeId;
use oxc_syntax::node::NodeId;
use petgraph::Direction;

use super::{
Expand Down Expand Up @@ -107,11 +107,11 @@ impl<'a> ControlFlowGraphBuilder<'a> {
self.graph.add_edge(a, b, weight);
}

pub fn push_statement(&mut self, stmt: AstNodeId) {
pub fn push_statement(&mut self, stmt: NodeId) {
self.push_instruction(InstructionKind::Statement, Some(stmt));
}

pub fn push_return(&mut self, kind: ReturnInstructionKind, node: AstNodeId) {
pub fn push_return(&mut self, kind: ReturnInstructionKind, node: NodeId) {
self.push_instruction(InstructionKind::Return(kind), Some(node));
}

Expand Down Expand Up @@ -166,20 +166,20 @@ impl<'a> ControlFlowGraphBuilder<'a> {
);
}

pub fn append_condition_to(&mut self, block: BasicBlockId, node: Option<AstNodeId>) {
pub fn append_condition_to(&mut self, block: BasicBlockId, node: Option<NodeId>) {
self.push_instruction_to(block, InstructionKind::Condition, node);
}

pub fn append_iteration(&mut self, node: Option<AstNodeId>, kind: IterationInstructionKind) {
pub fn append_iteration(&mut self, node: Option<NodeId>, kind: IterationInstructionKind) {
self.push_instruction(InstructionKind::Iteration(kind), node);
}

pub fn append_throw(&mut self, node: AstNodeId) {
pub fn append_throw(&mut self, node: NodeId) {
self.push_instruction(InstructionKind::Throw, Some(node));
self.append_unreachable();
}

pub fn append_break(&mut self, node: AstNodeId, label: Option<&'a str>) {
pub fn append_break(&mut self, node: NodeId, label: Option<&'a str>) {
let kind = match label {
Some(_) => LabeledInstruction::Labeled,
None => LabeledInstruction::Unlabeled,
Expand All @@ -193,7 +193,7 @@ impl<'a> ControlFlowGraphBuilder<'a> {
self.ctx(label).r#break(bb);
}

pub fn append_continue(&mut self, node: AstNodeId, label: Option<&'a str>) {
pub fn append_continue(&mut self, node: NodeId, label: Option<&'a str>) {
let kind = match label {
Some(_) => LabeledInstruction::Labeled,
None => LabeledInstruction::Unlabeled,
Expand Down Expand Up @@ -221,7 +221,7 @@ impl<'a> ControlFlowGraphBuilder<'a> {

/// # Panics
#[inline]
pub(self) fn push_instruction(&mut self, kind: InstructionKind, node_id: Option<AstNodeId>) {
pub(self) fn push_instruction(&mut self, kind: InstructionKind, node_id: Option<NodeId>) {
self.push_instruction_to(self.current_node_ix, kind, node_id);
}

Expand All @@ -230,12 +230,12 @@ impl<'a> ControlFlowGraphBuilder<'a> {
&mut self,
block: BasicBlockId,
kind: InstructionKind,
node_id: Option<AstNodeId>,
node_id: Option<NodeId>,
) {
self.basic_block_mut(block).instructions.push(Instruction { kind, node_id });
}

pub fn enter_statement(&mut self, stmt: AstNodeId) {
pub fn enter_statement(&mut self, stmt: NodeId) {
self.push_statement(stmt);
}
}
6 changes: 3 additions & 3 deletions crates/oxc_cfg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod dot;
pub mod visit;

use itertools::Itertools;
use oxc_syntax::node::AstNodeId;
use oxc_syntax::node::NodeId;
use petgraph::{
stable_graph::NodeIndex,
visit::{Control, DfsEvent, EdgeRef},
Expand Down Expand Up @@ -44,11 +44,11 @@ impl BasicBlock {
#[derive(Debug, Clone)]
pub struct Instruction {
pub kind: InstructionKind,
pub node_id: Option<AstNodeId>,
pub node_id: Option<NodeId>,
}

impl Instruction {
pub fn new(kind: InstructionKind, node_id: Option<AstNodeId>) -> Self {
pub fn new(kind: InstructionKind, node_id: Option<NodeId>) -> Self {
Self { kind, node_id }
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_cfg/tests/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_cfg::{ControlFlowGraphBuilder, CtxCursor};
use oxc_syntax::node::AstNodeId;
use oxc_syntax::node::NodeId;
/// same as but just the skeleton
/// ```js
/// A: {
Expand Down Expand Up @@ -29,7 +29,7 @@ fn labeled_statement_with_multiple_loops_continue_and_break() {
cfg.ctx(None).default().allow_break().allow_continue();
cfg.ctx(None).mark_break(c2).mark_continue(c2).resolve_with_upper_label();

cfg.append_break(AstNodeId::DUMMY, A);
cfg.append_break(NodeId::DUMMY, A);

// labeled block end
cfg.ctx(A).mark_break(labeled).resolve();
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_ast::{ast::BindingIdentifier, AstKind};
use oxc_semantic::{AstNode, AstNodeId, IsGlobalReference, SymbolId};
use oxc_semantic::{AstNode, IsGlobalReference, NodeId, SymbolId};
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator};

Expand Down Expand Up @@ -246,7 +246,7 @@ pub fn nth_outermost_paren_parent<'a, 'b>(
/// [`Expression::get_inner_expression`].
pub fn iter_outer_expressions<'a, 'ctx>(
ctx: &'ctx LintContext<'a>,
node_id: AstNodeId,
node_id: NodeId,
) -> impl Iterator<Item = &'ctx AstNode<'a>> + 'ctx {
ctx.nodes().iter_parents(node_id).skip(1).filter(|parent| {
!matches!(
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/func_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use oxc_ast::{
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::AstNodeId;
use oxc_semantic::NodeId;
use oxc_span::{Atom, GetSpan, Span};
use oxc_syntax::identifier::is_identifier_name;
use phf::phf_set;
Expand Down Expand Up @@ -479,7 +479,7 @@ impl Rule for FuncNames {
}
}

fn guess_function_name<'a>(ctx: &LintContext<'a>, parent_id: AstNodeId) -> Option<Cow<'a, str>> {
fn guess_function_name<'a>(ctx: &LintContext<'a>, parent_id: NodeId) -> Option<Cow<'a, str>> {
for parent_kind in ctx.nodes().iter_parents(parent_id).map(AstNode::kind) {
match parent_kind {
AstKind::ParenthesizedExpression(_)
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_constructor_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_ast::{
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::AstNodeId;
use oxc_semantic::NodeId;
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule, AstNode};
Expand Down Expand Up @@ -65,7 +65,7 @@ fn is_constructor(node: &AstNode<'_>) -> bool {
)
}

fn is_definitely_in_constructor(ctx: &LintContext, node_id: AstNodeId) -> bool {
fn is_definitely_in_constructor(ctx: &LintContext, node_id: NodeId) -> bool {
ctx.nodes()
.ancestors(node_id)
.map(|id| ctx.nodes().get_node(id))
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_import_assign.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::{ast::Expression, AstKind};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNodeId, SymbolId};
use oxc_semantic::{NodeId, SymbolId};
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::UnaryOperator;
use phf::phf_set;
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Rule for NoImportAssign {
/// - `Reflect.deleteProperty`
/// - `Reflect.set`
/// - `Reflect.setPrototypeOf`
fn is_argument_of_well_known_mutation_function(node_id: AstNodeId, ctx: &LintContext<'_>) -> bool {
fn is_argument_of_well_known_mutation_function(node_id: NodeId, ctx: &LintContext<'_>) -> bool {
let current_node = ctx.nodes().get_node(node_id);
let call_expression_node =
ctx.nodes().parent_node(node_id).and_then(|node| ctx.nodes().parent_kind(node.id()));
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_multi_str.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::AstNodeId;
use oxc_semantic::NodeId;
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule, AstNode};
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Rule for NoMultiStr {
}
}

fn is_within_jsx_attribute_item(id: AstNodeId, ctx: &LintContext) -> bool {
fn is_within_jsx_attribute_item(id: NodeId, ctx: &LintContext) -> bool {
if matches!(ctx.nodes().parent_kind(id), Some(AstKind::JSXAttributeItem(_))) {
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_this_before_super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use oxc_cfg::{
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::AstNodeId;
use oxc_semantic::NodeId;
use oxc_span::{GetSpan, Span};

use crate::{context::LintContext, rule::Rule, AstNode};
Expand Down Expand Up @@ -62,7 +62,7 @@ impl Rule for NoThisBeforeSuper {
// first pass -> find super calls and local violations
let mut wanted_nodes = Vec::new();
let mut basic_blocks_with_super_called = HashSet::<BasicBlockId>::new();
let mut basic_blocks_with_local_violations = HashMap::<BasicBlockId, Vec<AstNodeId>>::new();
let mut basic_blocks_with_local_violations = HashMap::<BasicBlockId, Vec<NodeId>>::new();
for node in semantic.nodes().iter() {
match node.kind() {
AstKind::Function(_) | AstKind::ArrowFunctionExpression(_) => {
Expand Down Expand Up @@ -153,7 +153,7 @@ impl NoThisBeforeSuper {
cfg: &ControlFlowGraph,
id: BasicBlockId,
basic_blocks_with_super_called: &HashSet<BasicBlockId>,
basic_blocks_with_local_violations: &HashMap<BasicBlockId, Vec<AstNodeId>>,
basic_blocks_with_local_violations: &HashMap<BasicBlockId, Vec<NodeId>>,
follow_join: bool,
) -> Vec<DefinitelyCallsThisBeforeSuper> {
neighbors_filtered_by_edge_weight(
Expand Down Expand Up @@ -212,7 +212,7 @@ impl NoThisBeforeSuper {
cfg: &ControlFlowGraph,
output: Vec<DefinitelyCallsThisBeforeSuper>,
basic_blocks_with_super_called: &HashSet<BasicBlockId>,
basic_blocks_with_local_violations: &HashMap<BasicBlockId, Vec<AstNodeId>>,
basic_blocks_with_local_violations: &HashMap<BasicBlockId, Vec<NodeId>>,
) -> bool {
// Deciding whether we definitely call this before super in all
// codepaths is as simple as seeing if any individual codepath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use itertools::Itertools;
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNode, AstNodeId, AstNodes};
use oxc_semantic::{AstNode, AstNodes, NodeId};
use oxc_span::Span;
use oxc_syntax::class::ElementKind;

Expand Down Expand Up @@ -114,7 +114,7 @@ impl Rule for NoUnusedPrivateClassMembers {
}
}

fn is_read(current_node_id: AstNodeId, nodes: &AstNodes) -> bool {
fn is_read(current_node_id: NodeId, nodes: &AstNodes) -> bool {
for (curr, parent) in nodes
.iter_parents(nodes.parent_id(current_node_id).unwrap_or(current_node_id))
.tuple_windows::<(&AstNode<'_>, &AstNode<'_>)>()
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/allowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! consider variables ignored by name pattern, but by where they are declared.
#[allow(clippy::wildcard_imports)]
use oxc_ast::{ast::*, AstKind};
use oxc_semantic::{AstNode, AstNodeId, Semantic};
use oxc_semantic::{AstNode, NodeId, Semantic};
use oxc_span::GetSpan;

use super::{options::ArgsOption, NoUnusedVars, Symbol};
Expand Down Expand Up @@ -154,7 +154,7 @@ impl NoUnusedVars {
pub(super) fn is_allowed_type_parameter(
&self,
symbol: &Symbol<'_, '_>,
declaration_id: AstNodeId,
declaration_id: NodeId,
) -> bool {
matches!(symbol.nodes().parent_kind(declaration_id), Some(AstKind::TSMappedType(_)))
}
Expand Down Expand Up @@ -234,15 +234,15 @@ impl NoUnusedVars {
.any(|p| p.has_modifier() || p.pattern.has_any_used_binding(ctx))
}

/// `params_id` is the [`AstNodeId`] to a [`AstKind::FormalParameters`] node.
/// `params_id` is the [`NodeId`] to a [`AstKind::FormalParameters`] node.
///
/// The following allowed conditions are handled:
/// 1. setter parameters - removing them causes a syntax error.
/// 2. TS constructor property definitions - they declare class members.
fn is_allowed_param_because_of_method<'a>(
semantic: &Semantic<'a>,
param: &FormalParameter<'a>,
params_id: AstNodeId,
params_id: NodeId,
) -> bool {
let mut parents_iter = semantic.nodes().iter_parents(params_id).skip(1).map(AstNode::kind);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use oxc_ast::{
ast::{Expression, VariableDeclarator},
AstKind,
};
use oxc_semantic::{AstNode, AstNodeId};
use oxc_semantic::{AstNode, NodeId};
use oxc_span::CompactStr;
use regex::Regex;

Expand All @@ -28,7 +28,7 @@ impl NoUnusedVars {
fixer: RuleFixer<'_, 'a>,
symbol: &Symbol<'_, 'a>,
decl: &VariableDeclarator<'a>,
decl_id: AstNodeId,
decl_id: NodeId,
) -> RuleFix<'a> {
if decl.init.as_ref().is_some_and(|init| is_skipped_init(symbol, init)) {
return fixer.noop();
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use oxc_ast::{
AstKind,
};
use oxc_semantic::{
AstNode, AstNodeId, AstNodes, Reference, ScopeId, ScopeTree, Semantic, SymbolFlags, SymbolId,
AstNode, AstNodes, NodeId, Reference, ScopeId, ScopeTree, Semantic, SymbolFlags, SymbolId,
SymbolTable,
};
use oxc_span::{GetSpan, Span};
Expand Down Expand Up @@ -77,7 +77,7 @@ impl<'s, 'a> Symbol<'s, 'a> {
}

#[inline]
fn declaration_id(&self) -> AstNodeId {
fn declaration_id(&self) -> NodeId {
self.symbols().get_declaration(self.id)
}

Expand Down Expand Up @@ -107,14 +107,14 @@ impl<'s, 'a> Symbol<'s, 'a> {

pub fn iter_relevant_parents(
&self,
node_id: AstNodeId,
node_id: NodeId,
) -> impl Iterator<Item = &AstNode<'a>> + Clone + '_ {
self.nodes().iter_parents(node_id).skip(1).filter(|n| Self::is_relevant_kind(n.kind()))
}

pub fn iter_relevant_parent_and_grandparent_kinds(
&self,
node_id: AstNodeId,
node_id: NodeId,
) -> impl Iterator<Item = (/* parent */ AstKind<'a>, /* grandparent */ AstKind<'a>)> + Clone + '_
{
let parents_iter = self
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[allow(clippy::wildcard_imports)]
use oxc_ast::{ast::*, AstKind};
use oxc_semantic::{AstNode, AstNodeId, Reference, ScopeId, SymbolFlags, SymbolId};
use oxc_semantic::{AstNode, NodeId, Reference, ScopeId, SymbolFlags, SymbolId};
use oxc_span::{GetSpan, Span};

use super::{ignored::FoundStatus, NoUnusedVars, Symbol};
Expand Down Expand Up @@ -427,7 +427,7 @@ impl<'s, 'a> Symbol<'s, 'a> {
}

/// Check if a [`AstNode`] is within a return statement or implicit return.
fn is_in_return_statement(&self, node_id: AstNodeId) -> bool {
fn is_in_return_statement(&self, node_id: NodeId) -> bool {
for parent in self.iter_relevant_parents(node_id).map(AstNode::kind) {
match parent {
AstKind::ReturnStatement(_) => return true,
Expand Down Expand Up @@ -660,7 +660,7 @@ impl<'s, 'a> Symbol<'s, 'a> {

/// Find the [`SymbolId`] for the nearest function declaration or expression
/// that is a parent of `node_id`.
fn get_nearest_function(&self, node_id: AstNodeId) -> Option<SymbolId> {
fn get_nearest_function(&self, node_id: NodeId) -> Option<SymbolId> {
// set to `true` when we find an arrow function and we want to get its
// name from the variable its assigned to.
let mut needs_variable_identifier = false;
Expand Down
Loading

0 comments on commit f7df926

Please sign in to comment.