Skip to content

Commit

Permalink
refactor(semantic): impl IntoIterator for &AstNodes
Browse files Browse the repository at this point in the history
Simple quality-of-life change.
  • Loading branch information
DonIsaac committed Sep 19, 2024
1 parent faa835b commit cbccd10
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_linter/examples/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() -> std::io::Result<()> {

let mut errors: Vec<OxcDiagnostic> = vec![];

for node in semantic_ret.semantic.nodes().iter() {
for node in semantic_ret.semantic.nodes() {
match node.kind() {
AstKind::DebuggerStatement(stmt) => {
errors.push(no_debugger(stmt.span));
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Linter {
}
}

for node in semantic.nodes().iter() {
for node in semantic.nodes() {
for (rule, ctx) in &rules {
rule.run(node, ctx);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/func_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl Rule for FuncNames {
fn run_once(&self, ctx: &LintContext<'_>) {
let mut invalid_funcs: Vec<(&Function, &AstNode)> = vec![];

for node in ctx.nodes().iter() {
for node in ctx.nodes() {
match node.kind() {
// check function if it invalid, do not report it because maybe later the function is calling itself
AstKind::Function(func) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_this_before_super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Rule for NoThisBeforeSuper {
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<NodeId>>::new();
for node in semantic.nodes().iter() {
for node in semantic.nodes() {
match node.kind() {
AstKind::Function(_) | AstKind::ArrowFunctionExpression(_) => {
if Self::is_wanted_node(node, ctx).unwrap_or_default() {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Rule for NoUnreachable {
_ => Control::Continue,
});
}
for node in ctx.nodes().iter() {
for node in ctx.nodes() {
// exit early if we are not visiting a statement.
if !node.kind().is_statement() {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Rule for NoNamedAsDefaultMember {
};
};

for item in ctx.semantic().nodes().iter() {
for item in ctx.semantic().nodes() {
match item.kind() {
AstKind::MemberExpression(member_expr) => process_member_expr(member_expr),
AstKind::VariableDeclarator(decl) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/prefer_hooks_in_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Rule for PreferHooksInOrder {
fn run_once(&self, ctx: &LintContext) {
let mut hook_groups: FxHashMap<ScopeId, Vec<AstNode>> = FxHashMap::default();

for node in ctx.nodes().iter() {
for node in ctx.nodes() {
hook_groups.entry(node.scope_id()).or_default().push(*node);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jsdoc/require_returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Rule for RequireReturns {

// Value of map: (AstNode, Span, Attrs: (isAsync, hasReturnValue))
let mut functions_to_check = FxHashMap::default();
'visit_node: for node in ctx.nodes().iter() {
'visit_node: for node in ctx.nodes() {
match node.kind() {
AstKind::Function(func) => {
functions_to_check.insert(node.id(), (node, func.span, (func.r#async, false)));
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/examples/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn main() -> std::io::Result<()> {
.expect("we set semantic to build the control flow (`with_cfg`) for us so it should always be `Some`");

let mut ast_nodes_by_block = HashMap::<_, Vec<_>>::new();
for node in semantic.semantic.nodes().iter() {
for node in semantic.semantic.nodes() {
let block = node.cfg_id();
let block_ix = cfg.graph.node_weight(block).unwrap();
ast_nodes_by_block.entry(*block_ix).or_default().push(node);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ mod tests {
";
let allocator = Allocator::default();
let semantic = get_semantic(&allocator, source, SourceType::default());
for node in semantic.nodes().iter() {
for node in semantic.nodes() {
if let AstKind::IdentifierReference(id) = node.kind() {
assert!(!semantic.is_reference_to_global_variable(id));
}
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_semantic/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ impl<'a> AstNodes<'a> {
}
}

impl<'ast, 'a> IntoIterator for &'ast AstNodes<'a> {
type Item = &'ast AstNode<'a>;
type IntoIter = std::slice::Iter<'ast, AstNode<'a>>;

fn into_iter(self) -> Self::IntoIter {
self.nodes.iter()
}
}

#[derive(Debug, Clone)]
pub struct AstNodeParentIter<'s, 'a> {
current_node_id: Option<NodeId>,
Expand Down

0 comments on commit cbccd10

Please sign in to comment.