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.
This commit is contained in:
Jakub Žádník
2023-02-27 09:44:05 +02:00
committed by GitHub
parent c6e2607868
commit a3f817d71b
21 changed files with 843 additions and 156 deletions

View File

@ -6,7 +6,7 @@ use nu_protocol::{
ImportPatternMember, PathMember, Pipeline, PipelineElement,
},
engine::{StateWorkingSet, DEFAULT_OVERLAY_NAME},
span, BlockId, Exportable, Module, PositionalArg, Span, Spanned, SyntaxShape, Type,
span, Alias, BlockId, Exportable, Module, PositionalArg, Span, Spanned, SyntaxShape, Type,
};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
@ -21,7 +21,7 @@ use crate::{
lex,
lite_parser::{lite_parse, LiteCommand, LiteElement},
parser::{
check_call, check_name, garbage, garbage_pipeline, parse, parse_import_pattern,
check_call, check_name, garbage, garbage_pipeline, parse, parse_call, parse_import_pattern,
parse_internal_call, parse_multispan_value, parse_signature, parse_string, parse_value,
parse_var_with_opt_type, trim_quotes, ParsedInternalCall,
},
@ -103,6 +103,34 @@ pub fn parse_def_predecl(
signature,
};
if working_set.add_predecl(Box::new(decl)).is_some() {
return Some(ParseError::DuplicateCommandDef(spans[1]));
}
}
} else if name == b"alias" && spans.len() >= 4 {
let (name_expr, ..) = parse_string(working_set, spans[1], expand_aliases_denylist);
let name = name_expr.as_string();
if let Some(name) = name {
if name.contains('#')
|| name.contains('^')
|| name.parse::<bytesize::ByteSize>().is_ok()
|| name.parse::<f64>().is_ok()
{
return Some(ParseError::CommandDefNotValid(spans[1]));
}
// The signature will get replaced by the replacement signature
// let mut signature = Signature::new(name.clone());
// signature.name = name;
// The fields get replaced during parsing
let decl = Alias {
name,
command: None,
wrapped_call: Expression::garbage(name_expr.span),
};
if working_set.add_predecl(Box::new(decl)).is_some() {
return Some(ParseError::DuplicateCommandDef(spans[1]));
}
@ -603,6 +631,201 @@ pub fn parse_alias(
) -> (Pipeline, Option<ParseError>) {
let spans = &lite_command.parts;
let (name_span, split_id) =
if spans.len() > 1 && working_set.get_span_contents(spans[0]) == b"export" {
(spans[1], 2)
} else {
(spans[0], 1)
};
let name = working_set.get_span_contents(name_span);
if name != b"alias" {
return (
garbage_pipeline(spans),
Some(ParseError::InternalError(
"Alias statement unparsable".into(),
span(spans),
)),
);
}
if let Some((span, err)) = check_name(working_set, spans) {
return (Pipeline::from_vec(vec![garbage(*span)]), Some(err));
}
if let Some(decl_id) = working_set.find_decl(b"alias", &Type::Any) {
let (command_spans, rest_spans) = spans.split_at(split_id);
let ParsedInternalCall { call, output, .. } = parse_internal_call(
working_set,
span(command_spans),
rest_spans,
decl_id,
expand_aliases_denylist,
);
if call.has_flag("help") {
return (
Pipeline::from_vec(vec![Expression {
expr: Expr::Call(call),
span: span(spans),
ty: output,
custom_completion: None,
}]),
None,
);
}
if spans.len() >= split_id + 3 {
let alias_name = working_set.get_span_contents(spans[split_id]);
let alias_name = if alias_name.starts_with(b"\"")
&& alias_name.ends_with(b"\"")
&& alias_name.len() > 1
{
alias_name[1..(alias_name.len() - 1)].to_vec()
} else {
alias_name.to_vec()
};
if let Some(mod_name) = module_name {
if alias_name == mod_name {
return (
Pipeline::from_vec(vec![Expression {
expr: Expr::Call(call),
span: span(spans),
ty: output,
custom_completion: None,
}]),
Some(ParseError::NamedAsModule(
"alias".to_string(),
String::from_utf8_lossy(&alias_name).to_string(),
spans[split_id],
)),
);
}
if &alias_name == b"main" {
return (
Pipeline::from_vec(vec![Expression {
expr: Expr::Call(call),
span: span(spans),
ty: output,
custom_completion: None,
}]),
Some(ParseError::ExportMainAliasNotAllowed(spans[split_id])),
);
}
}
let _equals = working_set.get_span_contents(spans[split_id + 1]);
let replacement_spans = &spans[(split_id + 2)..];
let (expr, err) = parse_call(
working_set,
replacement_spans,
replacement_spans[0],
expand_aliases_denylist,
false, // TODO: Should this be set properly???
);
if let Some(e) = err {
if let ParseError::MissingPositional(..) = e {
// ignore missing required positional
} else {
return (garbage_pipeline(replacement_spans), Some(e));
}
}
let (command, wrapped_call) = match expr {
Expression {
expr: Expr::Call(ref call),
..
} => (Some(working_set.get_decl(call.decl_id).clone_box()), expr),
Expression {
expr: Expr::ExternalCall(..),
..
} => (None, expr),
_ => {
return (
Pipeline::from_vec(vec![Expression {
expr: Expr::Call(call),
span: span(spans),
ty: output,
custom_completion: None,
}]),
Some(ParseError::InternalError(
"Parsed call not a call".into(),
expr.span,
)),
)
}
};
if let Some(decl_id) = working_set.find_predecl(&alias_name) {
let alias_decl = working_set.get_decl_mut(decl_id);
let alias = Alias {
name: String::from_utf8_lossy(&alias_name).to_string(),
command,
wrapped_call,
};
*alias_decl = Box::new(alias);
} else {
return (
garbage_pipeline(spans),
Some(ParseError::InternalError(
"Predeclaration failed to add declaration".into(),
spans[split_id],
)),
);
}
// It's OK if it returns None: The decl was already merged in previous parse pass.
working_set.merge_predecl(&alias_name);
}
let err = if spans.len() < 4 {
Some(ParseError::IncorrectValue(
"Incomplete alias".into(),
span(&spans[..split_id]),
"incomplete alias".into(),
))
} else {
None
};
return (
Pipeline::from_vec(vec![Expression {
expr: Expr::Call(call),
span: span(spans),
ty: Type::Any,
custom_completion: None,
}]),
err,
);
}
(
garbage_pipeline(spans),
Some(ParseError::InternalError(
"Alias statement unparsable".into(),
span(spans),
)),
)
}
pub fn parse_old_alias(
working_set: &mut StateWorkingSet,
lite_command: &LiteCommand,
module_name: Option<&[u8]>,
expand_aliases_denylist: &[usize],
) -> (Pipeline, Option<ParseError>) {
let spans = &lite_command.parts;
// if the call is "alias", turn it into "print $nu.scope.aliases"
if spans.len() == 1 {
let head = Expression {
@ -658,7 +881,7 @@ pub fn parse_alias(
let name = working_set.get_span_contents(name_span);
if name == b"alias" {
if name == b"old-alias" {
if let Some((span, err)) = check_name(working_set, spans) {
return (Pipeline::from_vec(vec![garbage(*span)]), Some(err));
}
@ -789,7 +1012,9 @@ pub fn parse_export_in_block(
let full_name = if lite_command.parts.len() > 1 {
let sub = working_set.get_span_contents(lite_command.parts[1]);
match sub {
b"alias" | b"def" | b"def-env" | b"extern" | b"use" => [b"export ", sub].concat(),
b"old-alias" | b"alias" | b"def" | b"def-env" | b"extern" | b"use" => {
[b"export ", sub].concat()
}
_ => b"export".to_vec(),
}
} else {
@ -857,6 +1082,9 @@ pub fn parse_export_in_block(
}
match full_name.as_slice() {
b"export old-alias" => {
parse_old_alias(working_set, lite_command, None, expand_aliases_denylist)
}
b"export alias" => parse_alias(working_set, lite_command, None, expand_aliases_denylist),
b"export def" | b"export def-env" => {
parse_def(working_set, lite_command, None, expand_aliases_denylist)
@ -1154,6 +1382,79 @@ pub fn parse_export_in_module(
result
}
b"old-alias" => {
let lite_command = LiteCommand {
comments: lite_command.comments.clone(),
parts: spans[1..].to_vec(),
};
let (pipeline, err) = parse_old_alias(
working_set,
&lite_command,
Some(module_name),
expand_aliases_denylist,
);
error = error.or(err);
let export_alias_decl_id =
if let Some(id) = working_set.find_decl(b"export old-alias", &Type::Any) {
id
} else {
return (
garbage_pipeline(spans),
vec![],
Some(ParseError::InternalError(
"missing 'export old-alias' command".into(),
export_span,
)),
);
};
// Trying to warp the 'old-alias' call into the 'export old-alias' in a very clumsy way
if let Some(PipelineElement::Expression(
_,
Expression {
expr: Expr::Call(ref alias_call),
..
},
)) = pipeline.elements.get(0)
{
call = alias_call.clone();
call.head = span(&spans[0..=1]);
call.decl_id = export_alias_decl_id;
} else {
error = error.or_else(|| {
Some(ParseError::InternalError(
"unexpected output from parsing a definition".into(),
span(&spans[1..]),
))
});
};
let mut result = vec![];
let alias_name = match spans.get(2) {
Some(span) => working_set.get_span_contents(*span),
None => &[],
};
let alias_name = trim_quotes(alias_name);
if let Some(alias_id) = working_set.find_alias(alias_name) {
result.push(Exportable::Alias {
name: alias_name.to_vec(),
id: alias_id,
});
} else {
error = error.or_else(|| {
Some(ParseError::InternalError(
"failed to find added alias".into(),
span(&spans[1..]),
))
});
}
result
}
b"alias" => {
let lite_command = LiteCommand {
comments: lite_command.comments.clone(),
@ -1211,8 +1512,8 @@ pub fn parse_export_in_module(
};
let alias_name = trim_quotes(alias_name);
if let Some(alias_id) = working_set.find_alias(alias_name) {
result.push(Exportable::Alias {
if let Some(alias_id) = working_set.find_decl(alias_name, &Type::Any) {
result.push(Exportable::Decl {
name: alias_name.to_vec(),
id: alias_id,
});
@ -1507,6 +1808,16 @@ pub fn parse_module_block(
(pipeline, err)
}
b"old-alias" => {
let (pipeline, err) = parse_old_alias(
working_set,
command,
None, // using aliases named as the module locally is OK
expand_aliases_denylist,
);
(pipeline, err)
}
b"alias" => {
let (pipeline, err) = parse_alias(
working_set,

View File

@ -20,8 +20,8 @@ use nu_protocol::{
use crate::parse_keywords::{
parse_alias, parse_def, parse_def_predecl, parse_export_in_block, parse_extern, parse_for,
parse_hide, parse_let_or_const, parse_module, parse_overlay, parse_source, parse_use,
parse_where, parse_where_expr,
parse_hide, parse_let_or_const, parse_module, parse_old_alias, parse_overlay, parse_source,
parse_use, parse_where, parse_where_expr,
};
use itertools::Itertools;
@ -253,6 +253,50 @@ pub fn check_name<'a>(
}
}
fn parse_external_arg(
working_set: &mut StateWorkingSet,
span: Span,
expand_aliases_denylist: &[usize],
) -> (Expression, Option<ParseError>) {
let contents = working_set.get_span_contents(span);
let mut error = None;
if contents.starts_with(b"$") || contents.starts_with(b"(") {
let (arg, err) = parse_dollar_expr(working_set, span, expand_aliases_denylist);
error = error.or(err);
(arg, error)
} else if contents.starts_with(b"[") {
let (arg, err) = parse_list_expression(
working_set,
span,
&SyntaxShape::Any,
expand_aliases_denylist,
);
error = error.or(err);
(arg, error)
} else {
// Eval stage trims the quotes, so we don't have to do the same thing when parsing.
let contents = if contents.starts_with(b"\"") {
let (contents, err) = unescape_string(contents, span);
error = error.or(err);
String::from_utf8_lossy(&contents).to_string()
} else {
String::from_utf8_lossy(contents).to_string()
};
(
Expression {
expr: Expr::String(contents),
span,
ty: Type::String,
custom_completion: None,
},
error,
)
}
}
pub fn parse_external_call(
working_set: &mut StateWorkingSet,
spans: &[Span],
@ -293,38 +337,9 @@ pub fn parse_external_call(
};
for span in &spans[1..] {
let contents = working_set.get_span_contents(*span);
if contents.starts_with(b"$") || contents.starts_with(b"(") {
let (arg, err) = parse_dollar_expr(working_set, *span, expand_aliases_denylist);
error = error.or(err);
args.push(arg);
} else if contents.starts_with(b"[") {
let (arg, err) = parse_list_expression(
working_set,
*span,
&SyntaxShape::Any,
expand_aliases_denylist,
);
error = error.or(err);
args.push(arg);
} else {
// Eval stage trims the quotes, so we don't have to do the same thing when parsing.
let contents = if contents.starts_with(b"\"") {
let (contents, err) = unescape_string(contents, *span);
error = error.or(err);
String::from_utf8_lossy(&contents).to_string()
} else {
String::from_utf8_lossy(contents).to_string()
};
args.push(Expression {
expr: Expr::String(contents),
span: *span,
ty: Type::String,
custom_completion: None,
});
}
let (arg, err) = parse_external_arg(working_set, *span, expand_aliases_denylist);
error = error.or(err);
args.push(arg);
}
(
Expression {
@ -784,12 +799,6 @@ pub fn parse_internal_call(
let signature = decl.signature();
let output = signature.output_type.clone();
working_set.type_scope.add_type(output.clone());
if signature.creates_scope {
working_set.enter_scope();
}
// The index into the positional parameter in the definition
let mut positional_idx = 0;
@ -797,6 +806,35 @@ pub fn parse_internal_call(
// Starting at the first argument
let mut spans_idx = 0;
if let Some(alias) = decl.as_alias() {
if let Expression {
expr: Expr::Call(wrapped_call),
..
} = &alias.wrapped_call
{
// Replace this command's call with the aliased call, but keep the alias name
call = *wrapped_call.clone();
call.head = command_span;
// Skip positionals passed to aliased call
positional_idx = call.positional_len();
} else {
return ParsedInternalCall {
call: Box::new(call),
output: Type::Any,
error: Some(ParseError::UnknownState(
"Alias does not point to internal call.".to_string(),
command_span,
)),
};
}
}
working_set.type_scope.add_type(output.clone());
if signature.creates_scope {
working_set.enter_scope();
}
while spans_idx < spans.len() {
let arg_span = spans[spans_idx];
@ -1163,16 +1201,61 @@ pub fn parse_call(
}
}
trace!("parsing: internal call");
// TODO: Try to remove the clone
let decl = working_set.get_decl(decl_id).clone();
// parse internal command
let parsed_call = parse_internal_call(
working_set,
span(&spans[cmd_start..pos]),
&spans[pos..],
decl_id,
expand_aliases_denylist,
);
let parsed_call = if let Some(alias) = decl.as_alias() {
if let Expression {
expr: Expr::ExternalCall(head, args, is_subexpression),
span: _,
ty,
custom_completion,
} = &alias.wrapped_call
{
trace!("parsing: alias of external call");
let mut error = None;
let mut final_args = args.clone();
for arg_span in spans.iter().skip(1) {
let (arg, err) =
parse_external_arg(working_set, *arg_span, expand_aliases_denylist);
error = error.or(err);
final_args.push(arg);
}
let mut head = head.clone();
head.span = spans[0]; // replacing the spans preserves syntax highlighting
return (
Expression {
expr: Expr::ExternalCall(head, final_args, *is_subexpression),
span: span(spans),
ty: ty.clone(),
custom_completion: *custom_completion,
},
error,
);
} else {
trace!("parsing: alias of internal call");
parse_internal_call(
working_set,
span(&spans[cmd_start..pos]),
&spans[pos..],
decl_id,
expand_aliases_denylist,
)
}
} else {
trace!("parsing: internal call");
parse_internal_call(
working_set,
span(&spans[cmd_start..pos]),
&spans[pos..],
decl_id,
expand_aliases_denylist,
)
};
(
Expression {
@ -5038,8 +5121,8 @@ pub fn parse_expression(
// For now, check for special parses of certain keywords
match bytes.as_slice() {
b"def" | b"extern" | b"for" | b"module" | b"use" | b"source" | b"alias" | b"export"
| b"hide" => (
b"def" | b"extern" | b"for" | b"module" | b"use" | b"source" | b"old-alias"
| b"alias" | b"export" | b"hide" => (
parse_call(
working_set,
&spans[pos..],
@ -5232,6 +5315,7 @@ pub fn parse_builtin_commands(
let (expr, err) = parse_for(working_set, &lite_command.parts, expand_aliases_denylist);
(Pipeline::from_vec(vec![expr]), err)
}
b"old-alias" => parse_old_alias(working_set, lite_command, None, expand_aliases_denylist),
b"alias" => parse_alias(working_set, lite_command, None, expand_aliases_denylist),
b"module" => parse_module(working_set, lite_command, expand_aliases_denylist),
b"use" => {