nushell/crates/nu-command/tests/commands
Jakub Žádník a3f817d71b
Re-implement aliases (#8123)
# Description

This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.

Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.

The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).

Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754

No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)

Should use custom command instead:
https://github.com/nushell/nushell/issues/6048

# User-Facing Changes

Since aliases are now basically commands, it has some new implications:

1. `alias spam = "spam"` (requires command call)
	* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
	* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
	* TODO: Needs a better error message when attempting to do so

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 20:44:05 +13:00
..
assignment allow different types of lists to be appended (#8157) 2023-02-22 12:53:36 +00:00
bytes special-case ExternalStream in bytes starts-with (#8203) 2023-02-26 15:17:44 +01:00
date Standardise the use of ShellError::UnsupportedInput and ShellError::TypeMismatch and add spans to every instance of the former (#7217) 2022-12-23 01:48:53 -05:00
hash_
math support multiplication operation on string and list values (#8061) 2023-02-13 16:35:53 +00:00
move_
network Fix typos and use more idiomatic assertions (#7755) 2023-01-15 15:03:32 +13:00
path Display empty records and lists (#7925) 2023-02-22 16:18:33 +00:00
platform Validate input range of du flags (#7962) 2023-02-06 21:34:32 +01:00
query
random Use imported names in Command::run signatures (#7967) 2023-02-05 22:17:46 +01:00
skip last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by raise error when given non-lists (#7623) 2022-12-31 13:35:12 +02:00
str_ Improve error when regex rejects pattern. Resolution of #8037 (#8050) 2023-02-12 13:25:40 +00:00
take last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by raise error when given non-lists (#7623) 2022-12-31 13:35:12 +02:00
url Feat/7725 url join (#7823) 2023-01-22 19:49:40 +01:00
alias.rs Re-implement aliases (#8123) 2023-02-27 20:44:05 +13:00
all.rs Use with-env to avoid calling external command on invalid command (#8209) 2023-02-25 09:36:51 -08:00
any.rs Use with-env to avoid calling external command on invalid command (#8209) 2023-02-25 09:36:51 -08:00
append.rs revert changes on prepend and append (#7660) 2023-01-02 17:09:55 -08:00
break_.rs
cal.rs return Error if get meet nothing and without "i" (#7002) 2022-12-31 13:27:09 +02:00
cd.rs Re-enable some good tests, remove some bad tests (#7875) 2023-01-28 14:56:47 -06:00
compact.rs
continue_.rs
cp.rs cp progress bar implementation (#8012) 2023-02-22 11:57:38 -08:00
def.rs fixes a def parsing bug with a default list (#8096) 2023-02-22 12:53:11 +00:00
default.rs Make get hole errors and cell path hole errors identical (improvement on #7002) (#7647) 2023-01-02 14:45:43 -08:00
do_.rs Bare word improvements (#8066) 2023-02-16 02:30:56 +00:00
drop.rs last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by raise error when given non-lists (#7623) 2022-12-31 13:35:12 +02:00
each.rs Remove deprecated --numbered flag from four commands (#7777) 2023-02-02 16:59:58 -06:00
echo.rs
empty.rs
enter.rs
error_make.rs
every.rs
exec.rs Add "fall-through" signatures (#7527) 2022-12-22 00:33:26 +02:00
export_def.rs
fill.rs string | fill counts clusters, not graphemes; and doesn't count ANSI escape codes (#8134) 2023-02-20 06:32:20 -06:00
find.rs Display empty records and lists (#7925) 2023-02-22 16:18:33 +00:00
first.rs Make stream info visible to users in describe (#7589) 2023-01-03 21:08:05 -08:00
flatten.rs
for_.rs Fix typos and use more idiomatic assertions (#7755) 2023-01-15 15:03:32 +13:00
format.rs
g.rs
get.rs Make get hole errors and cell path hole errors identical (improvement on #7002) (#7647) 2023-01-02 14:45:43 -08:00
glob.rs Expose filtering by file type in glob (#7834) 2023-01-28 07:50:12 -06:00
group_by.rs
headers.rs
help.rs Re-implement aliases (#8123) 2023-02-27 20:44:05 +13:00
histogram.rs
insert.rs Remove deprecated --numbered flag from four commands (#7777) 2023-02-02 16:59:58 -06:00
into_filesize.rs
into_int.rs
last.rs Make stream info visible to users in describe (#7589) 2023-01-03 21:08:05 -08:00
length.rs
let_.rs check external failed in let assignment (#8164) 2023-02-22 17:35:09 +00:00
lines.rs
loop_.rs Fix typos and use more idiomatic assertions (#7755) 2023-01-15 15:03:32 +13:00
ls.rs Fix typos and use more idiomatic assertions (#7755) 2023-01-15 15:03:32 +13:00
merge.rs
mkdir.rs Filesystem commands print --verbose to stderr (#8014) 2023-02-09 10:29:34 -08:00
mod.rs special-case ExternalStream in bytes starts-with (#8203) 2023-02-26 15:17:44 +01:00
mut_.rs
n.rs
nu_check.rs
open.rs Move some from xxx commands to plugin (#7942) 2023-02-13 12:42:08 +00:00
p.rs
parse.rs allow parse to work better with streams (#7870) 2023-02-08 20:59:02 -06:00
prepend.rs revert changes on prepend and append (#7660) 2023-01-02 17:09:55 -08:00
print.rs
range.rs
redirection.rs Let redirection keep exit code (#7848) 2023-01-30 16:49:31 +01:00
reduce.rs Test return in reduce closure from #7961 (#7973) 2023-02-06 11:01:08 +01:00
reject.rs Re-enable some good tests, remove some bad tests (#7875) 2023-01-28 14:56:47 -06:00
rename.rs fix rename when it is passed an empty column list to rename (#8086) 2023-02-15 21:47:27 +00:00
return_.rs ensure that when nu evaluates files, it allows early returns (#7415) 2023-02-02 00:02:27 +01:00
reverse.rs last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by raise error when given non-lists (#7623) 2022-12-31 13:35:12 +02:00
rm.rs
roll.rs Remove deprecated --numbered flag from four commands (#7777) 2023-02-02 16:59:58 -06:00
rotate.rs
run_external.rs pipe binary data to external commands (#8058) 2023-02-24 21:39:52 +01:00
save.rs allow Range to expand to array-like when converting to json (#8047) 2023-02-24 15:31:33 -06:00
select.rs Error out when Select gets same row (#8200) 2023-02-26 18:14:15 -08:00
semicolon.rs
seq_char.rs
seq.rs Make stream info visible to users in describe (#7589) 2023-01-03 21:08:05 -08:00
shells.rs
sort_by.rs last, skip, drop, take until, take while, skip until, skip while, where, reverse, shuffle, append, prepend and sort-by raise error when given non-lists (#7623) 2022-12-31 13:35:12 +02:00
sort.rs
source_env.rs ensure that when nu evaluates files, it allows early returns (#7415) 2023-02-02 00:02:27 +01:00
split_by.rs
split_column.rs
split_row.rs
table.rs table --collapse dont do truncation return message instead (#8172) 2023-02-22 18:35:45 +00:00
to_text.rs Make to text stream ListStreams (#7577) 2022-12-22 16:38:07 -08:00
touch.rs
transpose.rs
try_.rs fix some typos (#7773) 2023-01-16 12:43:46 +01:00
uniq_by.rs
uniq.rs
update.rs Remove deprecated --numbered flag from four commands (#7777) 2023-02-02 16:59:58 -06:00
upsert.rs Remove deprecated --numbered flag from four commands (#7777) 2023-02-02 16:59:58 -06:00
use_.rs Allow main command to define top-level module command (#7764) 2023-01-22 21:34:15 +02:00
where_.rs Remove deprecated --numbered flag from four commands (#7777) 2023-02-02 16:59:58 -06:00
which.rs Re-implement aliases (#8123) 2023-02-27 20:44:05 +13:00
while_.rs Fix typos and use more idiomatic assertions (#7755) 2023-01-15 15:03:32 +13:00
with_env.rs Re-enable some good tests, remove some bad tests (#7875) 2023-01-28 14:56:47 -06:00
wrap.rs
zip.rs Use variable names directly in the format strings (#7906) 2023-01-29 19:37:54 -06:00