feat(const): implement run_const for const (#15082)

- fixes #14559

# Description
Allow using `const` keyword in const contexts. The `run_const` body is
basically empty as the actual logic is already handled by the parser.

# User-Facing Changes
`const` keyword can now be used in `const` contexts.

# Tests + Formatting

I'll add some tests before marking this ready. 

- 🟢 toolkit fmt
- 🟢 toolkit clippy
- 🟢 toolkit test
- 🟢 toolkit test stdlib

# After Submitting
N/A

---------

Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com>
This commit is contained in:
Bahex 2025-02-12 18:59:51 +03:00 committed by GitHub
parent 3eae657121
commit 2e1b6acc0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 0 deletions

View File

@ -72,6 +72,19 @@ impl Command for Const {
}
}
fn run_const(
&self,
_working_set: &StateWorkingSet,
_call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(PipelineData::empty())
}
fn is_const(&self) -> bool {
true
}
fn examples(&self) -> Vec<Example> {
vec![
Example {

View File

@ -433,3 +433,12 @@ fn const_takes_pipeline() {
let actual = nu!(r#"const list = 'bar_baz_quux' | split row '_'; $list | length"#);
assert_eq!(actual.out, "3");
}
#[test]
fn const_const() {
let actual = nu!(r#"const y = (const x = "foo"; $x + $x); $y"#);
assert_eq!(actual.out, "foofoo");
let actual = nu!(r#"const y = (const x = "foo"; $x + $x); $x"#);
assert!(actual.err.contains("nu::parser::variable_not_found"));
}