From 637283ffad895358cca644223f83eab23123db4a Mon Sep 17 00:00:00 2001 From: mike <98623181+1Kinoti@users.noreply.github.com> Date: Sat, 8 Apr 2023 23:04:57 +0300 Subject: [PATCH] allow register to accept a const argument (#8758) # Description this pr allows `register` to be used with const variables ```nu const math_plugin = "~/.config/nushell/plugins/nu_plugin_math" register $math_plugin ``` should close #8208, previous work #8435 --- crates/nu-parser/src/eval.rs | 4 ++++ crates/nu-parser/src/parse_keywords.rs | 29 +++++++++++----------- src/tests/test_parser.rs | 33 ++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/crates/nu-parser/src/eval.rs b/crates/nu-parser/src/eval.rs index 2ecc1214f..1cf02f0b2 100644 --- a/crates/nu-parser/src/eval.rs +++ b/crates/nu-parser/src/eval.rs @@ -19,6 +19,10 @@ pub fn eval_constant( val: b.clone(), span: expr.span, }), + Expr::Filepath(path) => Ok(Value::String { + val: path.clone(), + span: expr.span, + }), Expr::Var(var_id) => match working_set.find_constant(*var_id) { Some(val) => Ok(val.clone()), None => Err(ParseError::NotAConstant(expr.span)), diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 716c08d03..ce5e90338 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -2853,25 +2853,24 @@ pub fn parse_register(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipe let arguments = call .positional_nth(0) .map(|expr| { - let name_expr = working_set.get_span_contents(expr.span); - let (name, err) = unescape_unquote_string(name_expr, expr.span); - if let Some(err) = err { - working_set.error(err) - } - - let path = if let Some(p) = find_in_dirs(&name, working_set, &cwd, PLUGIN_DIRS_VAR) { - p - } else { - return Err(ParseError::RegisteredFileNotFound(name, expr.span)); + let val = match eval_constant(working_set, expr) { + Ok(val) => val, + Err(err) => return Err(err), }; - if path.exists() & path.is_file() { + let filename = match value_as_string(val, expr.span) { + Ok(str) => str, + Err(err) => return Err(err), + }; + + let Some(path) = find_in_dirs(&filename, working_set, &cwd, PLUGIN_DIRS_VAR) else { + return Err(ParseError::RegisteredFileNotFound(filename, expr.span)) + }; + + if path.exists() && path.is_file() { Ok((path, expr.span)) } else { - Err(ParseError::RegisteredFileNotFound( - format!("{path:?}"), - expr.span, - )) + Err(ParseError::RegisteredFileNotFound(filename, expr.span)) } }) .expect("required positional has being checked"); diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index 9c754ca47..957811cb2 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -487,3 +487,36 @@ fn unbalanced_delimiter2() -> TestResult { fn unbalanced_delimiter3() -> TestResult { fail_test(r#"{"#, "Unexpected end of code") } + +#[test] +fn register_with_string_literal() -> TestResult { + fail_test(r#"register 'nu-plugin-math'"#, "File not found") +} + +#[test] +fn register_with_string_constant() -> TestResult { + let input = "\ +const file = 'nu-plugin-math' +register $file +"; + // should not fail with `not a constant` + fail_test(input, "File not found") +} + +#[test] +fn register_with_string_variable() -> TestResult { + let input = "\ +let file = 'nu-plugin-math' +register $file +"; + fail_test(input, "Value is not a parse-time constant") +} + +#[test] +fn register_with_non_string_constant() -> TestResult { + let input = "\ +const file = 6 +register $file +"; + fail_test(input, "expected string, found int") +}