Skip to content

Commit 3241e3d

Browse files
committed
Use existing atom escape code
1 parent 9f0b0f6 commit 3241e3d

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

@@ -115,7 +116,7 @@ impl<'a> ErlangApp<'a> {
115116
.chain(native_modules)
116117
.unique()
117118
.sorted()
118-
.map(|m| Self::format_atom(&m))
119+
.map(|m| escape_atom_string((&m).clone().into()))
119120
.join(",\n ");
120121

121122
// TODO: When precompiling for production (i.e. as a precompiled hex
@@ -154,22 +155,6 @@ impl<'a> ErlangApp<'a> {
154155

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

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

compiler-core/src/erlang.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ fn atom(value: &str) -> Document<'_> {
654654
}
655655
}
656656

657-
fn escape_atom_string(value: String) -> EcoString {
657+
pub fn escape_atom_string(value: String) -> EcoString {
658658
if is_erlang_reserved_word(&value) {
659659
// Escape because of keyword collision
660660
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)