diff --git a/CHANGELOG.md b/CHANGELOG.md index 9912a23..c0a444b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable milestones for the nera project are documented in this file. +## Formatter keeps named fun names unquoted + +- `nera fmt` now prints a named fun's variable name as written, so + `fun Loop(0) -> ...` stays valid Erlang. Previously it printed + `fun 'Loop'(0) -> ...`, which `erlc` rejects with a syntax error. + ## Binary comprehension bodies written as segments are rejected - A binary comprehension body with a size, a type or several segments, such as diff --git a/crates/nera-fmt/src/format.rs b/crates/nera-fmt/src/format.rs index 92ce73e..66ce37d 100644 --- a/crates/nera-fmt/src/format.rs +++ b/crates/nera-fmt/src/format.rs @@ -1387,7 +1387,7 @@ impl<'src> Formatter<'src> { fn format_fun_clause(&mut self, clause: &FunctionClause, name: &Option) -> Doc { let mut parts = Vec::new(); if let Some(name_id) = name { - parts.push(self.format_atom(*name_id)); + parts.push(self.format_fun_name(*name_id)); } let params: Vec = clause .patterns @@ -1402,6 +1402,17 @@ impl<'src> Formatter<'src> { concat_all(parts) } + /// Print a named fun's name. Erlang requires a variable here, so a name + /// shaped like one is never quoted; the parser also accepts atoms. + fn format_fun_name(&self, id: AtomId) -> Doc { + let name = self.atom_str(id); + if nera_syntax::token::is_variable_name(name) { + text(name.to_string()) + } else { + self.format_atom(id) + } + } + fn format_fun_ref(&mut self, target: &FunRefTarget) -> Doc { match target { FunRefTarget::Local(name, arity) => { diff --git a/crates/nera-fmt/tests/named_fun_names.rs b/crates/nera-fmt/tests/named_fun_names.rs new file mode 100644 index 0000000..49d4983 --- /dev/null +++ b/crates/nera-fmt/tests/named_fun_names.rs @@ -0,0 +1,44 @@ +use nera_fmt::{config::FormatConfig, format_source}; + +fn format(source: &str) -> String { + format_source(source, &FormatConfig::default()).unwrap() +} + +// erlc accepts only a variable as a named fun's name, so quoting one makes the +// formatted file stop compiling. +#[test] +fn variable_names_stay_unquoted_in_every_clause() { + let source = "count(N) -> + Loop = fun Loop(0) -> done; + Loop(M) when M > 0 -> Loop(M - 1) + end, + Loop(N). +"; + assert_eq!(format(source), source); +} + +#[test] +fn underscore_names_stay_unquoted() { + let source = "add() -> fun _(N) -> N + 1 + end. +unused() -> fun _BlackAdder(N) -> N + 42 + end. +stack() -> fun _stack@2(N) -> N + end. +"; + assert_eq!(format(source), source); +} + +// Nera also reads an atom as the name. Only a name that is not a valid +// variable still needs its quotes. +#[test] +fn atom_names_keep_quotes_only_when_they_need_them() { + let formats_to = |name: &str, expected: &str| { + let source = format!("a() -> fun {name}(N) -> N\n end.\n"); + let wanted = format!("a() -> fun {expected}(N) -> N\n end.\n"); + assert_eq!(format(&source), wanted); + }; + formats_to("'two words'", "'two words'"); + formats_to("plain", "plain"); + formats_to("'Loop'", "Loop"); +} diff --git a/crates/nera-syntax/src/token.rs b/crates/nera-syntax/src/token.rs index 8f82a9d..e8f1739 100644 --- a/crates/nera-syntax/src/token.rs +++ b/crates/nera-syntax/src/token.rs @@ -419,6 +419,12 @@ pub fn atom_needs_quoting(text: &str) -> bool { !identifier || reserved } +/// Whether `text` is exactly one variable token, such as `Loop`, `_` or `_F1`. +pub fn is_variable_name(text: &str) -> bool { + let mut lexer = Token::lexer(text); + matches!(lexer.next(), Some(Ok(Token::Variable))) && lexer.next().is_none() +} + fn scan_triple_string(lex: &mut logos::Lexer<'_, Token>) -> Result<(), ()> { let end = lex.remainder().find("\"\"\"").ok_or(())?; lex.bump(end + 3);