From 4bce59df68227332cff5927b9c2692ca70bf8e4b Mon Sep 17 00:00:00 2001 From: rzvxa <3788964+rzvxa@users.noreply.github.com> Date: Mon, 17 Jun 2024 14:16:55 +0000 Subject: [PATCH] refactor(semantic/cfg)!: re-export `petgraph` as `control_flow::graph`. (#3722) So we can replace or extend it easily. --- .../src/rules/eslint/getter_return.rs | 3 ++- .../src/rules/eslint/no_fallthrough.rs | 6 ++++-- .../src/rules/eslint/no_this_before_super.rs | 18 ++++++------------ .../src/rules/eslint/no_unreachable.rs | 2 +- .../src/rules/react/require_render_return.rs | 4 ++-- .../src/rules/react/rules_of_hooks.rs | 3 ++- crates/oxc_semantic/src/control_flow/mod.rs | 11 ++++++++++- .../src/{pg.rs => control_flow/visit.rs} | 0 crates/oxc_semantic/src/lib.rs | 7 ++----- 9 files changed, 29 insertions(+), 25 deletions(-) rename crates/oxc_semantic/src/{pg.rs => control_flow/visit.rs} (100%) diff --git a/crates/oxc_linter/src/rules/eslint/getter_return.rs b/crates/oxc_linter/src/rules/eslint/getter_return.rs index 187f7422c583c..8db35a1198e2b 100644 --- a/crates/oxc_linter/src/rules/eslint/getter_return.rs +++ b/crates/oxc_linter/src/rules/eslint/getter_return.rs @@ -9,7 +9,8 @@ use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_semantic::{ - pg::neighbors_filtered_by_edge_weight, EdgeType, InstructionKind, ReturnInstructionKind, + control_flow::graph::visit::neighbors_filtered_by_edge_weight, EdgeType, InstructionKind, + ReturnInstructionKind, }; use oxc_span::Span; diff --git a/crates/oxc_linter/src/rules/eslint/no_fallthrough.rs b/crates/oxc_linter/src/rules/eslint/no_fallthrough.rs index ea268565513d1..bdc219c802d07 100644 --- a/crates/oxc_linter/src/rules/eslint/no_fallthrough.rs +++ b/crates/oxc_linter/src/rules/eslint/no_fallthrough.rs @@ -8,8 +8,10 @@ use oxc_ast::{ use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_semantic::{ - petgraph::{visit::EdgeRef, Direction}, - pg::neighbors_filtered_by_edge_weight, + control_flow::graph::{ + visit::{neighbors_filtered_by_edge_weight, EdgeRef}, + Direction, + }, BasicBlockId, EdgeType, ErrorEdgeKind, InstructionKind, }; use oxc_span::{GetSpan, Span}; diff --git a/crates/oxc_linter/src/rules/eslint/no_this_before_super.rs b/crates/oxc_linter/src/rules/eslint/no_this_before_super.rs index 69d31058a1a05..77ce3096284f8 100644 --- a/crates/oxc_linter/src/rules/eslint/no_this_before_super.rs +++ b/crates/oxc_linter/src/rules/eslint/no_this_before_super.rs @@ -8,8 +8,8 @@ use oxc_ast::{ }; use oxc_macros::declare_oxc_lint; use oxc_semantic::{ - petgraph::visit::EdgeRef, pg::neighbors_filtered_by_edge_weight, AstNodeId, BasicBlockId, - ControlFlowGraph, EdgeType, + control_flow::graph::visit::{neighbors_filtered_by_edge_weight, EdgeRef}, + AstNodeId, BasicBlockId, ControlFlowGraph, EdgeType, }; use oxc_span::{GetSpan, Span}; @@ -156,11 +156,8 @@ impl NoThisBeforeSuper { fn analyze( cfg: &ControlFlowGraph, id: BasicBlockId, - basic_blocks_with_super_called: &HashSet, - basic_blocks_with_local_violations: &HashMap< - oxc_semantic::petgraph::prelude::NodeIndex, - Vec, - >, + basic_blocks_with_super_called: &HashSet, + basic_blocks_with_local_violations: &HashMap>, follow_join: bool, ) -> Vec { neighbors_filtered_by_edge_weight( @@ -219,11 +216,8 @@ impl NoThisBeforeSuper { fn check_for_violation( cfg: &ControlFlowGraph, output: Vec, - basic_blocks_with_super_called: &HashSet, - basic_blocks_with_local_violations: &HashMap< - oxc_semantic::petgraph::prelude::NodeIndex, - Vec, - >, + basic_blocks_with_super_called: &HashSet, + basic_blocks_with_local_violations: &HashMap>, ) -> bool { // Deciding whether we definitely call this before super in all // codepaths is as simple as seeing if any individual codepath diff --git a/crates/oxc_linter/src/rules/eslint/no_unreachable.rs b/crates/oxc_linter/src/rules/eslint/no_unreachable.rs index a988c41cbdde1..08bb39f02e944 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unreachable.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unreachable.rs @@ -2,7 +2,7 @@ use oxc_ast::{ast::VariableDeclarationKind, AstKind}; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_semantic::{ - petgraph::{ + control_flow::graph::{ visit::{depth_first_search, Control, DfsEvent, EdgeRef}, Direction, }, diff --git a/crates/oxc_linter/src/rules/react/require_render_return.rs b/crates/oxc_linter/src/rules/react/require_render_return.rs index fc75760e2bd56..0d1be3d304ba2 100644 --- a/crates/oxc_linter/src/rules/react/require_render_return.rs +++ b/crates/oxc_linter/src/rules/react/require_render_return.rs @@ -3,8 +3,8 @@ use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_semantic::{ - pg::neighbors_filtered_by_edge_weight, EdgeType, Instruction, InstructionKind, - ReturnInstructionKind, + control_flow::graph::visit::neighbors_filtered_by_edge_weight, EdgeType, Instruction, + InstructionKind, ReturnInstructionKind, }; use oxc_span::{GetSpan, Span}; diff --git a/crates/oxc_linter/src/rules/react/rules_of_hooks.rs b/crates/oxc_linter/src/rules/react/rules_of_hooks.rs index ccb3bcb9bc64c..df3eb7a8e13b6 100644 --- a/crates/oxc_linter/src/rules/react/rules_of_hooks.rs +++ b/crates/oxc_linter/src/rules/react/rules_of_hooks.rs @@ -4,7 +4,8 @@ use oxc_ast::{ }; use oxc_macros::declare_oxc_lint; use oxc_semantic::{ - algo, petgraph::visit::Control, AstNodeId, AstNodes, EdgeType, ErrorEdgeKind, InstructionKind, + control_flow::graph::{algo, visit::Control}, + AstNodeId, AstNodes, EdgeType, ErrorEdgeKind, InstructionKind, }; use oxc_span::{Atom, CompactStr}; use oxc_syntax::operator::AssignmentOperator; diff --git a/crates/oxc_semantic/src/control_flow/mod.rs b/crates/oxc_semantic/src/control_flow/mod.rs index 6464cfe155b27..0abba185b5fc5 100644 --- a/crates/oxc_semantic/src/control_flow/mod.rs +++ b/crates/oxc_semantic/src/control_flow/mod.rs @@ -1,5 +1,6 @@ mod builder; mod dot; +pub mod visit; use itertools::Itertools; use oxc_ast::AstKind; @@ -9,9 +10,17 @@ use petgraph::{ Direction, Graph, }; +pub mod graph { + pub use petgraph::*; + pub mod visit { + pub use super::super::visit::*; + pub use petgraph::visit::*; + } +} + use crate::{AstNodeId, AstNodes}; -pub use builder::{ControlFlowGraphBuilder, CtxCursor, CtxFlags}; +pub(crate) use builder::{ControlFlowGraphBuilder, CtxCursor, CtxFlags}; pub use dot::{DebugDot, DebugDotContext, DisplayDot}; pub type BasicBlockId = NodeIndex; diff --git a/crates/oxc_semantic/src/pg.rs b/crates/oxc_semantic/src/control_flow/visit.rs similarity index 100% rename from crates/oxc_semantic/src/pg.rs rename to crates/oxc_semantic/src/control_flow/visit.rs diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index 1c33cd291867e..09cdc214a8287 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -2,21 +2,18 @@ mod binder; mod builder; mod checker; mod class; -mod control_flow; mod diagnostics; mod jsdoc; mod label; mod module_record; mod node; -pub mod pg; mod reference; mod scope; mod symbol; -use std::sync::Arc; +pub mod control_flow; -pub use petgraph; -pub use petgraph::algo; +use std::sync::Arc; pub use builder::{SemanticBuilder, SemanticBuilderReturn}; use class::ClassTable;