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

View File

@ -22,7 +22,7 @@ fn alias_parses_path_tilde() {
let actual = nu!( let actual = nu!(
cwd: "tests/fixtures/formats", cwd: "tests/fixtures/formats",
r#" r#"
alias new-cd [dir] { cd $dir } alias -i new-cd [dir] { cd $dir }
new-cd ~ new-cd ~
pwd pwd
"# "#
@ -39,7 +39,7 @@ fn error_alias_wrong_shape_shallow() {
let actual = nu!( let actual = nu!(
cwd: ".", cwd: ".",
r#" 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 round-to 3.45 a
"# "#
); );
@ -52,7 +52,7 @@ fn error_alias_wrong_shape_deep_invocation() {
let actual = nu!( let actual = nu!(
cwd: ".", cwd: ".",
r#" 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 round-to 3.45 a
"# "#
); );
@ -65,7 +65,7 @@ fn error_alias_wrong_shape_deep_binary() {
let actual = nu!( let actual = nu!(
cwd: ".", cwd: ".",
r#" 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 round-plus-one 3.45 a
"# "#
); );
@ -78,7 +78,7 @@ fn error_alias_wrong_shape_deeper_binary() {
let actual = nu!( let actual = nu!(
cwd: ".", cwd: ".",
r#" 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 round-one-more 3.45 a
"# "#
); );
@ -91,7 +91,7 @@ fn error_alias_syntax_shape_clash() {
let actual = nu!( let actual = nu!(
cwd: ".", cwd: ".",
r#" 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 } }
"# "#
); );