Skip to content

Commit

Permalink
feat: add some more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelarie committed Oct 14, 2024
1 parent 908fdbb commit e3782eb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ fn main() {

let aliases = find_aliases(&mut cursor, code.as_bytes());
for alias in aliases {
println!();
println!("Alias name: {}", alias.name);
println!("Alias content: {}", alias.content);
println!("Valid nushell: {}", alias.is_valid_nushell);
println!();
if alias.is_valid_nushell {
println!("alias {} = {}", alias.name, alias.content);
} else {
println!("# alias {} = {} # Invalid nushell alias", alias.name, alias.content);
}
}
}
10 changes: 4 additions & 6 deletions src/syntax_tree/alias.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::{
nushell::validate_alias_with_nu_parser, validate_nu_tree_sitter_code,
};
use super::nushell::validate_alias_with_nu_parser;

/// Unquote a string (remove the quotes if it has)
// https://www.gnu.org/software/bash/manual/html_node/Quoting.html
Expand Down Expand Up @@ -141,11 +139,11 @@ pub fn find_aliases(
if node.kind() == "command" {
if let Ok(alias) = extract_alias(node, source) {
let (name, content) = alias;
let is_valid_nushell = validate_nu_tree_sitter_code(&content);
let is_valid_nushell_code =

let is_valid_nushell =
validate_alias_with_nu_parser(&name, &content);

println!("Parsed: Is valid -> {}", is_valid_nushell_code);
// println!("Parsed: Is valid -> {}", is_valid_nushell);

let alias = Alias {
name,
Expand Down
2 changes: 2 additions & 0 deletions src/syntax_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ pub use printer::print_tree;
pub use traverser::traverse_tree;

pub use alias::find_aliases;
#[allow(unused)]
pub use nushell::validate_nu_tree_sitter_code;
#[allow(unused)]
pub use nushell::validate_alias_with_nu_parser;
55 changes: 50 additions & 5 deletions src/syntax_tree/nushell.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use nu_cmd_lang::create_default_context;
use nu_command::add_shell_command_context;
use nu_parser::parse;
use nu_protocol::engine::{EngineState, StateWorkingSet};
use tree_sitter::Parser;
use tree_sitter_nu::LANGUAGE;

#[allow(unused)]
pub fn validate_nu_tree_sitter_code(content: &String) -> bool {
let mut parser = Parser::new();
let nu_lang = LANGUAGE.into();
Expand All @@ -16,15 +15,28 @@ pub fn validate_nu_tree_sitter_code(content: &String) -> bool {
parser.parse(content, None).is_some()
}

// Other engine state generation methods from nushell:
// let engine_state = nu_cmd_lang::create_default_context();
// #[cfg(feature = "plugin")]
// let engine_state = nu_cmd_plugin::add_plugin_command_context(engine_state);
// let engine_state = nu_command::add_shell_command_context(engine_state);
// let engine_state = nu_cmd_extra::add_extra_command_context(engine_state);
// let engine_state = nu_cli::add_cli_context(engine_state);
// nu_explore::add_explore_context(engine_state)
fn create_nu_engine_state() -> EngineState {
add_shell_command_context(create_default_context())
let engine_state = nu_command::add_shell_command_context(
nu_cmd_lang::create_default_context(),
);

engine_state
}

pub fn validate_alias_with_nu_parser(name: &str, content: &str) -> bool {
let engine_state = create_nu_engine_state();
let mut working_set = StateWorkingSet::new(&engine_state);

let alias_command = format!("alias {} = {}", name, content);
println!("alias_command: {}", alias_command);
let alias_bytes = alias_command.as_bytes();

let _ = working_set.add_file("alias.nu".into(), &alias_bytes.to_vec());
Expand All @@ -38,8 +50,12 @@ pub fn validate_alias_with_nu_parser(name: &str, content: &str) -> bool {

if !working_set.parse_errors.is_empty() {
for error in &working_set.parse_errors {
println!("Nushell alias parsing Error: {:?}", error);
println!("Error string: {}", error.to_string());
// TODO: Handle DidYouMean suggestions and generate them as comments
println!("ERROR: {:?}", error);
println!(
"Nushell alias parsing error string: {:?}",
error.to_string()
);
}
false
} else {
Expand Down Expand Up @@ -87,4 +103,33 @@ mod tests {
"Expected invalid alias to return false, but got true"
);
}

#[test]
fn validate_nu_alias_with_parser_invalid_source_content() {
let invalid_alias_name = "node15";
let invalid_alias_content = "source /usr/share/nvm/init-nvm.sh";
let result = validate_alias_with_nu_parser(
invalid_alias_name,
invalid_alias_content,
);
assert!(
!result,
"Expected invalid alias to return false, but got true"
);
}

// TODO: Fix this test
// #[test]
// fn validate_nu_alias_with_parser_invalid_non_zero_exit_code() {
// let invalid_alias_name = "zkn";
// let invalid_alias_content = "cd ~/notes; ~/scripts/zk-new";
// let result = validate_alias_with_nu_parser(
// invalid_alias_name,
// invalid_alias_content,
// );
// assert!(
// !result,
// "Expected invalid alias to return false, but got true"
// );
// }
}
14 changes: 8 additions & 6 deletions src/test/examples/bash_aliases
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
shopt -s expand_aliases

# Simple aliases
alias ls='ls --color=auto'
alias ll='ls -l'
alias "abc!"='echo String with special characters'
alias la= 'ls -A' # This alias is invalid and should be ignored
alias gitlog='git log --graph --oneline --decorate --all'
alias invalid_nushell_alias='echo $HOME'
alias ls='ls --color=auto' # Should error with nu::parser::unknown_flag
alias ll='ls -l' # VALID
alias "abc!"='echo String with special characters' # VALID
alias la= 'ls -A' # This alias is invalid and should be ignored + nu::parser::unknown_flag
alias gitlog='git log --graph --oneline --decorate --all' # VALID
alias invalid_nushell_alias='echo $HOME' # Should error with nu::parser::env_var_not_var
alias node15="source /usr/share/nvm/init-nvm.sh" # Should error
alias zkn="cd ~/notes; ~/scripts/zk-new"

# Cases like this are not handled yet by the Parser
my_function() {
Expand Down

0 comments on commit e3782eb

Please sign in to comment.