From 192c55a75e13636dcfc4dce70295ffe4fa53a52d Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Wed, 31 Dec 2025 09:54:32 -0300 Subject: [PATCH] refactor: use early exits to reduce nesting --- .../plotnik-cli/src/commands/debug/source.rs | 32 +++++++++---------- .../src/bytecode/emit/typescript.rs | 7 ++-- crates/plotnik-lib/src/diagnostics/printer.rs | 27 ++++++++-------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/crates/plotnik-cli/src/commands/debug/source.rs b/crates/plotnik-cli/src/commands/debug/source.rs index 03908fea..5adea924 100644 --- a/crates/plotnik-cli/src/commands/debug/source.rs +++ b/crates/plotnik-cli/src/commands/debug/source.rs @@ -107,7 +107,7 @@ fn format_node_with_field( let text = node .utf8_text(source.as_bytes()) .unwrap_or(""); - if text == kind { + return if text == kind { format!("{}{}(\"{}\")", indent, field_prefix, escape_string(kind)) } else { format!( @@ -117,22 +117,22 @@ fn format_node_with_field( kind, escape_string(text) ) - } - } else { - let mut out = format!("{}{}({}", indent, field_prefix, kind); - for (child, child_field) in children { - out.push('\n'); - out.push_str(&format_node_with_field( - child, - child_field, - source, - depth + 1, - include_anonymous, - )); - } - out.push(')'); - out + }; + } + + let mut out = format!("{}{}({}", indent, field_prefix, kind); + for (child, child_field) in children { + out.push('\n'); + out.push_str(&format_node_with_field( + child, + child_field, + source, + depth + 1, + include_anonymous, + )); } + out.push(')'); + out } fn escape_string(s: &str) -> String { diff --git a/crates/plotnik-lib/src/bytecode/emit/typescript.rs b/crates/plotnik-lib/src/bytecode/emit/typescript.rs index dc91a489..263255b8 100644 --- a/crates/plotnik-lib/src/bytecode/emit/typescript.rs +++ b/crates/plotnik-lib/src/bytecode/emit/typescript.rs @@ -716,11 +716,10 @@ impl<'a> TsEmitter<'a> { let Some(type_def) = self.types.get(type_id) else { return (type_id, false); }; - if type_def.type_kind() == Some(TypeKind::Optional) { - (QTypeId(type_def.data), true) - } else { - (type_id, false) + if type_def.type_kind() != Some(TypeKind::Optional) { + return (type_id, false); } + (QTypeId(type_def.data), true) } fn needs_generated_name(&self, type_def: &TypeDef) -> bool { diff --git a/crates/plotnik-lib/src/diagnostics/printer.rs b/crates/plotnik-lib/src/diagnostics/printer.rs index 5b60176a..37e46f09 100644 --- a/crates/plotnik-lib/src/diagnostics/printer.rs +++ b/crates/plotnik-lib/src/diagnostics/printer.rs @@ -63,20 +63,21 @@ impl<'a> DiagnosticsPrinter<'a> { .span(adjust_range(related.span.range, primary_content.len())) .label(&related.message), ); - } else { - // Different file: create separate snippet - let related_content = self.sources.content(related.span.source); - let mut snippet = Snippet::source(related_content).line_start(1); - if let Some(name) = self.source_path(related.span.source) { - snippet = snippet.path(name); - } - snippet = snippet.annotation( - AnnotationKind::Context - .span(adjust_range(related.span.range, related_content.len())) - .label(&related.message), - ); - cross_file_snippets.push(snippet); + continue; } + + // Different file: create separate snippet + let related_content = self.sources.content(related.span.source); + let mut snippet = Snippet::source(related_content).line_start(1); + if let Some(name) = self.source_path(related.span.source) { + snippet = snippet.path(name); + } + snippet = snippet.annotation( + AnnotationKind::Context + .span(adjust_range(related.span.range, related_content.len())) + .label(&related.message), + ); + cross_file_snippets.push(snippet); } let level = severity_to_level(diag.severity());