Move alias type inference (experimental) behind --infer/-i flag (#2418)

* put alias type inference behind --infer/-i flag

* revert cargo.lock
This commit is contained in:
Bailey Layzer 2020-08-26 22:48:13 -07:00 committed by GitHub
parent 8f5df89a78
commit a64270829e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 16 deletions

View file

@ -19,6 +19,7 @@ pub struct AliasArgs {
pub name: Tagged<String>,
pub args: Vec<Value>,
pub block: Block,
pub infer: Option<bool>,
pub save: Option<bool>,
}
@ -37,6 +38,7 @@ impl WholeStreamCommand for Alias {
SyntaxShape::Block,
"the block to run as the body of the alias",
)
.switch("infer", "infer argument types (experimental)", Some('i'))
.switch("save", "save the alias to your config", Some('s'))
}
@ -79,6 +81,7 @@ pub async fn alias(
name,
args: list,
block,
infer,
save,
},
_ctx,
@ -92,11 +95,15 @@ pub async fn alias(
let left_brace = raw_input.find('{').unwrap_or(0);
let right_brace = raw_input.rfind('}').unwrap_or_else(|| raw_input.len());
let left = raw_input[..left_brace]
.replace("--save", "")
.replace("-s", "");
.replace("--save", "") // TODO using regex (or reconstruct string from AST?)
.replace("-si", "-i")
.replace("-s ", "")
.replace("-is", "-i");
let right = raw_input[right_brace..]
.replace("--save", "")
.replace("-s", "");
.replace("-si", "-i")
.replace("-s ", "")
.replace("-is", "-i");
raw_input = format!("{}{}{}", left, &raw_input[left_brace..right_brace], right);
// create a value from raw_input alias
@ -137,13 +144,26 @@ pub async fn alias(
}
}
Ok(OutputStream::one(ReturnSuccess::action(
CommandAction::AddAlias(
name.to_string(),
to_arg_shapes(processed_args, &block, &registry)?,
block,
),
)))
if let Some(true) = infer {
Ok(OutputStream::one(ReturnSuccess::action(
CommandAction::AddAlias(
name.to_string(),
to_arg_shapes(processed_args, &block, &registry)?,
block,
),
)))
} else {
Ok(OutputStream::one(ReturnSuccess::action(
CommandAction::AddAlias(
name.to_string(),
processed_args
.into_iter()
.map(|arg| (arg, SyntaxShape::Any))
.collect(),
block,
),
)))
}
}
fn to_arg_shapes(

View file

@ -22,7 +22,7 @@ fn alias_parses_path_tilde() {
let actual = nu!(
cwd: "tests/fixtures/formats",
r#"
alias new-cd [dir] { cd $dir }
alias -i new-cd [dir] { cd $dir }
new-cd ~
pwd
"#
@ -39,7 +39,7 @@ fn error_alias_wrong_shape_shallow() {
let actual = nu!(
cwd: ".",
r#"
alias round-to [num digits] { echo $num | str from -d $digits }
alias -i round-to [num digits] { echo $num | str from -d $digits }
round-to 3.45 a
"#
);
@ -52,7 +52,7 @@ fn error_alias_wrong_shape_deep_invocation() {
let actual = nu!(
cwd: ".",
r#"
alias round-to [nums digits] { echo $nums | each {= $(str from -d $digits)}}
alias -i round-to [nums digits] { echo $nums | each {= $(str from -d $digits)}}
round-to 3.45 a
"#
);
@ -65,7 +65,7 @@ fn error_alias_wrong_shape_deep_binary() {
let actual = nu!(
cwd: ".",
r#"
alias round-plus-one [nums digits] { echo $nums | each {= $(str from -d $digits | str to-decimal) + 1}}
alias -i round-plus-one [nums digits] { echo $nums | each {= $(str from -d $digits | str to-decimal) + 1}}
round-plus-one 3.45 a
"#
);
@ -78,7 +78,7 @@ fn error_alias_wrong_shape_deeper_binary() {
let actual = nu!(
cwd: ".",
r#"
alias round-one-more [num digits] { echo $num | str from -d $(= $digits + 1) }
alias -i round-one-more [num digits] { echo $num | str from -d $(= $digits + 1) }
round-one-more 3.45 a
"#
);
@ -91,7 +91,7 @@ fn error_alias_syntax_shape_clash() {
let actual = nu!(
cwd: ".",
r#"
alias clash [a] { echo 1.1 2 3 | each { str from -d $a } | range $a } }
alias -i clash [a] { echo 1.1 2 3 | each { str from -d $a } | range $a } }
"#
);