refactor(semantic): impl IntoIterator for &AstNodes (#5873)

Simple quality-of-life change.
This commit is contained in:
DonIsaac 2024-09-19 03:22:29 +00:00
parent 1ccf2907be
commit 3d13c6d1f9
11 changed files with 19 additions and 10 deletions

View file

@ -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));

View file

@ -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);
}

View file

@ -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) => {

View file

@ -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() {

View file

@ -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;

View file

@ -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) => {

View file

@ -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);
}

View file

@ -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)));

View file

@ -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);

View file

@ -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));
}

View file

@ -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>,