Parse and format aliases in import defs

This commit is contained in:
Agus Zubiaga 2023-11-29 10:54:25 -03:00
parent 933fde77a0
commit 76d799ea13
No known key found for this signature in database
12 changed files with 85 additions and 6 deletions

View file

@ -196,7 +196,7 @@ impl<'a> Formattable for ValueDef<'a> {
Expect { condition, .. } => condition.is_multiline(),
ExpectFx { condition, .. } => condition.is_multiline(),
Dbg { condition, .. } => condition.is_multiline(),
ModuleImport { name: _ } => false,
ModuleImport { name: _, alias: _ } => false,
}
}
@ -239,12 +239,19 @@ impl<'a> Formattable for ValueDef<'a> {
buf.newline();
fmt_body(buf, &body_pattern.value, &body_expr.value, indent);
}
ModuleImport { name } => {
ModuleImport { name, alias } => {
buf.indent(indent);
buf.push_str("import");
buf.spaces(1);
buf.push_str(name.value.as_str());
if let Some(alias_name) = alias {
buf.spaces(1);
buf.push_str("as");
buf.spaces(1);
buf.push_str(alias_name.value.as_str());
}
}
}
}

View file

@ -565,8 +565,9 @@ impl<'a> RemoveSpaces<'a> for ValueDef<'a> {
condition: arena.alloc(condition.remove_spaces(arena)),
preceding_comment: Region::zero(),
},
ModuleImport { name } => ModuleImport {
ModuleImport { name, alias } => ModuleImport {
name: name.remove_spaces(arena),
alias: alias.map(|alias_name| alias_name.remove_spaces(arena)),
},
}
}

View file

@ -459,6 +459,7 @@ pub enum ValueDef<'a> {
/// e.g. `import [Req] as Http from InternalHttp`.
ModuleImport {
name: Loc<crate::header::ModuleName<'a>>,
alias: Option<Loc<crate::header::ModuleName<'a>>>,
},
}
@ -1797,7 +1798,9 @@ impl<'a> Malformed for ValueDef<'a> {
condition,
preceding_comment: _,
} => condition.is_malformed(),
ValueDef::ModuleImport { name } => name.value.contains_dot(),
ValueDef::ModuleImport { name, alias } => {
name.value.contains_dot() || alias.map_or(false, |x| x.value.contains_dot())
}
}
}
}

View file

@ -844,11 +844,24 @@ fn import<'a>() -> impl Parser<'a, ValueDef<'a>, EImport> {
crate::parser::keyword_e(crate::keyword::IMPORT, EImport::Import),
spaces()
),
loc!(crate::module::module_name_help(EImport::ModuleName))
and!(
loc!(crate::module::module_name_help(EImport::ModuleName)),
optional(backtrackable(skip_first!(
and!(
spaces(),
and!(
crate::parser::keyword_e(crate::keyword::AS, EImport::As),
spaces()
)
),
loc!(crate::module::module_name_help(EImport::Alias))
)))
)
),
|loc_module_name| {
|(loc_module_name, optional_loc_alias)| {
ValueDef::ModuleImport {
name: loc_module_name,
alias: optional_loc_alias,
}
}
)

View file

@ -523,6 +523,8 @@ pub enum EExpect<'a> {
pub enum EImport {
Import(Position),
ModuleName(Position),
As(Position),
Alias(Position),
Space(BadInputError, Position),
}

View file

@ -18,6 +18,7 @@ Defs {
name: @8-12 ModuleName(
"Json",
),
alias: None,
},
],
}

View file

@ -0,0 +1,2 @@
import JsonEncode as JE
import BytesDecode as BD

View file

@ -0,0 +1,44 @@
Defs {
tags: [
Index(2147483648),
Index(2147483649),
],
regions: [
@0-23,
@24-49,
],
space_before: [
Slice(start = 0, length = 0),
Slice(start = 0, length = 1),
],
space_after: [
Slice(start = 0, length = 0),
Slice(start = 1, length = 0),
],
spaces: [
Newline,
],
type_defs: [],
value_defs: [
ModuleImport {
name: @7-17 ModuleName(
"JsonEncode",
),
alias: Some(
@21-23 ModuleName(
"JE",
),
),
},
ModuleImport {
name: @31-42 ModuleName(
"BytesDecode",
),
alias: Some(
@47-49 ModuleName(
"BD",
),
),
},
],
}

View file

@ -0,0 +1,2 @@
import JsonEncode as JE
import BytesDecode as BD

View file

@ -200,6 +200,7 @@ mod test_snapshots {
fail/if_guard_without_condition.expr,
fail/if_missing_else.expr,
fail/if_outdented_then.expr,
fail/import_with_lowercase_alias.moduledefs,
fail/imports_missing_comma.header,
fail/inline_hastype.expr,
fail/invalid_operator.expr,
@ -324,6 +325,7 @@ mod test_snapshots {
pass/highest_int.expr,
pass/if_def.expr,
pass/import.moduledefs,
pass/import_with_alias.moduledefs,
pass/int_with_underscore.expr,
pass/interface_with_newline.header,
pass/lambda_in_chain.expr,