Repository: astral-sh/ruff
File: crates/ruff_graph/src/collector.rs (Rust)

Code before:
            if alias.name.as_str() != "*" {
                components.push(alias.name.as_str());
            }

            if let Some(module_name) = ModuleName::from_components(components) {
                self.imports.push(CollectedImport {
                    module_name,
                    import_kind: ImportKind::ImportFrom,
                    type_checking,
                });
            }
        }
    }
}

impl<'ast> SourceOrderVisitor<'ast> for Collector<'_> {
    fn visit_stmt(&mut self, stmt: &'ast Stmt) {
        match stmt {
            Stmt::ImportFrom(stmt) => {
                self.collect_import_from(stmt, false);
            }
            Stmt::Import(stmt) => {
                self.collect_import(stmt, false);
            }
            Stmt::If(ast::StmtIf { test, body, .. }) => {
                if is_type_checking_condition(test) {
                    // Collect all imports from the body with type_checking: true
                    for stmt in body {
                        match stmt {
                            Stmt::Import(stmt) => {
                                self.collect_import(stmt, true);
                            }
                            Stmt::ImportFrom(stmt) => {
                                self.collect_import_from(stmt, true);
                            }
                            _ => {
                                // Ignore non-import statements in TYPE_CHECKING blocks
                            }
                        }
                    }
                } else {
                    // Regular if statement, traverse normally
                    walk_stmt(self, stmt);
                }
            }
            Stmt::FunctionDef(_)
            | Stmt::ClassDef(_)
            | Stmt::While(_)
            | Stmt::With(_)
            | Stmt::Match(_)
            | Stmt::Try(_)

Review comment (suggestion):
I think a simpler approach is to pass the `exclude_type_checking` option to `Collector` and simply skip the entire body if `test` is a `TYPE_CHECKING` condition. 

This removes the need to split `collect_import_from` and `collect_import` out of `visit_stmt` or that we need to set `type_checking` on `CollectedImport`

Code after:
                    }

                    // Add the alias name, unless it's a wildcard import.
                    if alias.name.as_str() != "*" {
                        components.push(alias.name.as_str());
                    }

                    if let Some(module_name) = ModuleName::from_components(components) {
                        self.imports.push(CollectedImport::ImportFrom(module_name));
                    }
                }
            }
            Stmt::Import(ast::StmtImport {
                names,
                range: _,
                node_index: _,
            }) => {
                for alias in names {
                    if let Some(module_name) = ModuleName::new(alias.name.as_str()) {
                        self.imports.push(CollectedImport::Import(module_name));
                    }
                }
            }
            Stmt::If(ast::StmtIf {
                test,
                body,
                elif_else_clauses,
                range: _,
                node_index: _,
            }) => {
                // Skip TYPE_CHECKING blocks if not requested
                if self.type_checking_imports || !is_type_checking_condition(test) {
                    self.visit_body(body);
                }

                for clause in elif_else_clauses {
                    self.visit_elif_else_clause(clause);
                }
            }
            Stmt::FunctionDef(_)
            | Stmt::ClassDef(_)
            | Stmt::While(_)
            | Stmt::With(_)
            | Stmt::Match(_)
            | Stmt::Try(_)
            | Stmt::For(_) => {
                // Always traverse into compound statements.
                walk_stmt(self, stmt);
            }

            Stmt::Return(_)