Skip to content

Commit ba1ff76

Browse files
committed
Use existing atom escape code
1 parent c286284 commit ba1ff76

File tree

3 files changed

+4
-19
lines changed

3 files changed

+4
-19
lines changed

compiler-core/src/codegen.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
Result,
1010
};
1111
use ecow::EcoString;
12+
use erlang::escape_atom_string;
1213
use itertools::Itertools;
1314
use std::fmt::Debug;
1415

@@ -113,7 +114,7 @@ impl<'a> ErlangApp<'a> {
113114
.chain(native_modules)
114115
.unique()
115116
.sorted()
116-
.map(|m| Self::format_atom(&m))
117+
.map(|m| escape_atom_string((&m).clone().into()))
117118
.join(",\n ");
118119

119120
// TODO: When precompiling for production (i.e. as a precompiled hex
@@ -152,22 +153,6 @@ impl<'a> ErlangApp<'a> {
152153

153154
writer.write(&path, &text)
154155
}
155-
156-
fn format_atom(s: &EcoString) -> EcoString {
157-
let mut chars = s.chars();
158-
159-
let Some(first) = chars.next() else {
160-
return "''".into();
161-
};
162-
163-
let needs_escape = |c: char| !(c.is_alphanumeric() || c == '_' || c == '@');
164-
165-
if !first.is_ascii_lowercase() || chars.any(needs_escape) {
166-
format!("'{}'", s).into()
167-
} else {
168-
s.clone()
169-
}
170-
}
171156
}
172157

173158
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

compiler-core/src/erlang.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ fn atom(value: &str) -> Document<'_> {
642642
}
643643
}
644644

645-
fn escape_atom_string(value: String) -> EcoString {
645+
pub fn escape_atom_string(value: String) -> EcoString {
646646
if is_erlang_reserved_word(&value) {
647647
// Escape because of keyword collision
648648
eco_format!("'{value}'")

test/external_only_erlang/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if g run --target=javascript; then
3333
exit 1
3434
fi
3535

36-
echo Running erlang shippment should succeed
36+
echo Running erlang shipment should succeed
3737
g export erlang-shipment
3838
grep "external_only_erlang_ffi" "build/erlang-shipment/external_only_erlang/ebin/external_only_erlang.app"
3939

0 commit comments

Comments
 (0)