-
Notifications
You must be signed in to change notification settings - Fork 102
Implement succeeded block for handling command and function success
#787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ph0enixKM
merged 7 commits into
staging
from
copilot/fix-ba6afbb4-4a3a-4521-a1b2-661b2e03a5fa
Sep 28, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b6d74a1
Initial plan
Copilot f32f8a6
Implement succeeded block feature with basic functionality
Copilot e326b67
Complete succeeded block implementation with comprehensive tests
Copilot fd24894
Clean up test files based on code review feedback
Copilot 91ce117
Remove unused is_main field from Succeeded struct
Copilot f11ae6a
Add echo "Succeeded" to function_succeeded_fail test
Copilot d39147e
Shorten tests
lens0021 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod ifcond; | ||
| pub mod ifchain; | ||
| pub mod failed; | ||
| pub mod succeeded; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| use heraclitus_compiler::prelude::*; | ||
| use crate::fragments; | ||
| use crate::modules::prelude::*; | ||
| use crate::modules::block::Block; | ||
| use crate::modules::types::Type; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct Succeeded { | ||
| pub is_parsed: bool, | ||
| error_position: Option<PositionInfo>, | ||
| function_name: Option<String>, | ||
| block: Box<Block> | ||
| } | ||
|
|
||
| impl Succeeded { | ||
| pub fn set_position(&mut self, position: PositionInfo) { | ||
| self.error_position = Some(position); | ||
| } | ||
|
|
||
| pub fn set_function_name(&mut self, name: String) { | ||
| self.function_name = Some(name); | ||
| } | ||
| } | ||
|
|
||
| impl SyntaxModule<ParserMetadata> for Succeeded { | ||
| syntax_name!("Succeeded Expression"); | ||
|
|
||
| fn new() -> Self { | ||
| Succeeded { | ||
| is_parsed: false, | ||
| function_name: None, | ||
| error_position: None, | ||
| block: Box::new(Block::new().with_needs_noop().with_condition()) | ||
| } | ||
| } | ||
|
|
||
| fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { | ||
| match token(meta, "succeeded") { | ||
| Ok(_) => { | ||
| let tok = meta.get_current_token(); | ||
| syntax(meta, &mut *self.block)?; | ||
| if self.block.is_empty() { | ||
| let message = Message::new_warn_at_token(meta, tok) | ||
| .message("Empty succeeded block") | ||
| .comment("You should use 'trust' modifier to run commands without handling errors"); | ||
| meta.add_message(message); | ||
| } | ||
| self.is_parsed = true; | ||
| Ok(()) | ||
| }, | ||
| Err(_) => { | ||
| // If we're in a trust context, mark as parsed | ||
| if meta.context.is_trust_ctx { | ||
| self.is_parsed = true; | ||
| } | ||
| // Otherwise, return quietly (no succeeded block found) | ||
| Ok(()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TranslateModule for Succeeded { | ||
| fn translate(&self, meta: &mut TranslateMetadata) -> FragmentKind { | ||
| if self.is_parsed { | ||
| let block = self.block.translate(meta); | ||
| // the condition of '$?' clears the status code thus we need to store it in a variable | ||
| let status_variable_stmt = VarStmtFragment::new("__status", Type::Num, fragments!("$?")); | ||
| let status_variable_expr = VarExprFragment::from_stmt(&status_variable_stmt); | ||
|
|
||
| match &block { | ||
| FragmentKind::Empty => { | ||
| status_variable_stmt.to_frag() | ||
| }, | ||
| FragmentKind::Block(block) if block.statements.is_empty() => { | ||
| status_variable_stmt.to_frag() | ||
| }, | ||
| _ => { | ||
| BlockFragment::new(vec![ | ||
| status_variable_stmt.to_frag(), | ||
| fragments!("if [ ", status_variable_expr.to_frag(), " = 0 ]; then"), | ||
| block, | ||
| fragments!("fi"), | ||
| ], false).to_frag() | ||
| } | ||
| } | ||
| } else { | ||
| FragmentKind::Empty | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Output | ||
| // Cannot use both 'succeeded' and 'failed' blocks for the same command | ||
|
|
||
| $ echo "test" $ succeeded { | ||
| echo "Success!" | ||
| } failed { | ||
| echo "Failed!" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| import { text_contains } from "std/text" | ||
|
|
||
| main { | ||
| if text_contains("Hello World", "World") { | ||
| echo "Succeeded" | ||
| } | ||
| } | ||
| if text_contains("Hello World", "World") { | ||
| echo "Succeeded" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| fun test_failable(): Num? { | ||
| if 1 > 5 { | ||
| fail 1 | ||
| } | ||
| return 42 | ||
| } | ||
|
|
||
| test_failable() succeeded { | ||
| echo "Succeeded" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| fun test_failable(): Num? { | ||
| if 1 < 5 { | ||
| fail 1 | ||
| } | ||
| return 42 | ||
| } | ||
|
|
||
| test_failable() succeeded { | ||
| echo "This should not print!" | ||
| } | ||
| echo "Succeeded" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| $ printf "S" $ succeeded { | ||
| echo "ucceeded" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Add to the end of this main block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added echo "Succeeded" to the end of the main block. This demonstrates that execution continues normally after a failed function call with a succeeded block - the succeeded block doesn't execute (as expected), but main continues and prints "Succeeded". f11ae6a