Skip to content

Commit

Permalink
Replace Document with EcoString
Browse files Browse the repository at this point in the history
  • Loading branch information
GearsDatapacks authored and lpil committed Mar 4, 2025
1 parent bd7d760 commit 37b2008
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 96 deletions.
20 changes: 10 additions & 10 deletions compiler-core/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl<'a> Generator<'a> {
fn parameter((i, arg): (usize, &TypedRecordConstructorArg)) -> Document<'_> {
arg.label
.as_ref()
.map(|(_, s)| maybe_escape_identifier_doc(s))
.map(|(_, s)| maybe_escape_identifier(s).to_doc())
.unwrap_or_else(|| eco_format!("x{i}").to_doc())
}

Expand Down Expand Up @@ -435,9 +435,9 @@ impl<'a> Generator<'a> {
let unqualified_imports = unqualified.iter().map(|i| {
let alias = i.as_name.as_ref().map(|n| {
self.register_in_scope(n);
maybe_escape_identifier_doc(n)
maybe_escape_identifier(n).to_doc()
});
let name = maybe_escape_identifier_doc(&i.name);
let name = maybe_escape_identifier(&i.name).to_doc();
Member { name, alias }
});

Expand Down Expand Up @@ -473,7 +473,7 @@ impl<'a> Generator<'a> {
fn module_constant(
&mut self,
publicity: Publicity,
name: &'a str,
name: &'a EcoString,
value: &'a TypedConstant,
) -> Output<'a> {
let head = if publicity.is_private() {
Expand All @@ -487,7 +487,7 @@ impl<'a> Generator<'a> {

Ok(docvec![
head,
maybe_escape_identifier_doc(name),
maybe_escape_identifier(name),
" = ",
document,
";",
Expand Down Expand Up @@ -539,7 +539,7 @@ impl<'a> Generator<'a> {

let document = docvec![
head,
maybe_escape_identifier_doc(name.as_str()),
maybe_escape_identifier(name),
fun_args(function.arguments.as_slice(), generator.tail_recursion_used),
" {",
docvec![line(), body].nest(INDENT).group(),
Expand Down Expand Up @@ -637,7 +637,7 @@ fn fun_args(args: &'_ [TypedArg], tail_recursion_used: bool) -> Document<'_> {
doc
}
Some(name) if tail_recursion_used => eco_format!("loop${name}").to_doc(),
Some(name) => maybe_escape_identifier_doc(name),
Some(name) => maybe_escape_identifier(name).to_doc(),
}))
}

Expand Down Expand Up @@ -769,11 +769,11 @@ fn escape_identifier(word: &str) -> EcoString {
eco_format!("{word}$")
}

fn maybe_escape_identifier_doc(word: &str) -> Document<'_> {
fn maybe_escape_identifier(word: &EcoString) -> EcoString {
if is_usable_js_identifier(word) {
word.to_doc()
word.clone()
} else {
escape_identifier(word).to_doc()
escape_identifier(word)
}
}

Expand Down
26 changes: 13 additions & 13 deletions compiler-core/src/javascript/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ impl<'module> Generator<'module> {
}
}

pub fn local_var<'a>(&mut self, name: &'a EcoString) -> Document<'a> {
pub fn local_var(&mut self, name: &EcoString) -> EcoString {
match self.current_scope_vars.get(name) {
None => {
let _ = self.current_scope_vars.insert(name.clone(), 0);
maybe_escape_identifier_doc(name)
maybe_escape_identifier(name)
}
Some(0) => maybe_escape_identifier_doc(name),
Some(n) if name == "$" => eco_format!("${n}").to_doc(),
Some(n) => eco_format!("{name}${n}").to_doc(),
Some(0) => maybe_escape_identifier(name),
Some(n) if name == "$" => eco_format!("${n}"),
Some(n) => eco_format!("{name}${n}"),
}
}

pub fn next_local_var<'a>(&mut self, name: &'a EcoString) -> Document<'a> {
pub fn next_local_var(&mut self, name: &EcoString) -> EcoString {
let next = self.current_scope_vars.get(name).map_or(0, |i| i + 1);
let _ = self.current_scope_vars.insert(name.clone(), next);
self.local_var(name)
Expand All @@ -117,7 +117,7 @@ impl<'module> Generator<'module> {

fn tail_call_loop<'a>(&mut self, body: Document<'a>, args: &'a [TypedArg]) -> Output<'a> {
let loop_assignments = concat(args.iter().flat_map(Arg::get_variable_name).map(|name| {
let var = maybe_escape_identifier_doc(name);
let var = maybe_escape_identifier(name);
docvec!["let ", var, " = loop$", name, ";", line()]
}));
Ok(docvec![
Expand Down Expand Up @@ -500,7 +500,7 @@ impl<'module> Generator<'module> {
}
ValueConstructorVariant::ModuleFn { .. }
| ValueConstructorVariant::ModuleConstant { .. }
| ValueConstructorVariant::LocalVariable { .. } => Ok(self.local_var(name)),
| ValueConstructorVariant::LocalVariable { .. } => Ok(self.local_var(name).to_doc()),
}
}

Expand Down Expand Up @@ -1089,12 +1089,12 @@ impl<'module> Generator<'module> {
fn module_select<'a>(
&mut self,
module: &'a str,
label: &'a str,
label: &'a EcoString,
constructor: &'a ModuleValueConstructor,
) -> Document<'a> {
match constructor {
ModuleValueConstructor::Fn { .. } | ModuleValueConstructor::Constant { .. } => {
docvec!["$", module, ".", maybe_escape_identifier_doc(label)]
docvec!["$", module, ".", maybe_escape_identifier(label)]
}

ModuleValueConstructor::Record {
Expand Down Expand Up @@ -1340,7 +1340,7 @@ pub(crate) fn guard_constant_expression<'a>(
.iter()
.find(|assignment| assignment.name == name)
.map(|assignment| assignment.subject.clone())
.unwrap_or_else(|| maybe_escape_identifier_doc(name))),
.unwrap_or_else(|| maybe_escape_identifier(name).to_doc())),

expression => constant_expression(Context::Function, tracker, expression),
}
Expand Down Expand Up @@ -1455,12 +1455,12 @@ pub(crate) fn constant_expression<'a>(

Constant::Var { name, module, .. } => Ok({
match module {
None => maybe_escape_identifier_doc(name),
None => maybe_escape_identifier(name).to_doc(),
Some((module, _)) => {
// JS keywords can be accessed here, but we must escape anyway
// as we escape when exporting such names in the first place,
// and the imported name has to match the exported name.
docvec!["$", module, ".", maybe_escape_identifier_doc(name)]
docvec!["$", module, ".", maybe_escape_identifier(name)]
}
}
}),
Expand Down
Loading

0 comments on commit 37b2008

Please sign in to comment.