Skip to content

Commit

Permalink
String -> EcoString
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri authored and lpil committed Sep 20, 2024
1 parent 8f9c752 commit e5a38f8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 41 deletions.
14 changes: 7 additions & 7 deletions compiler-core/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,22 +373,22 @@ impl<'a> Generator<'a> {
imports
}

fn import_path(&self, package: &'a str, module: &'a str) -> String {
fn import_path(&self, package: &'a str, module: &'a str) -> EcoString {
// TODO: strip shared prefixed between current module and imported
// module to avoid descending and climbing back out again
if package == self.module.type_info.package || package.is_empty() {
// Same package
match self.current_module_name_segments_count {
1 => format!("./{module}.mjs"),
1 => eco_format!("./{module}.mjs"),
_ => {
let prefix = "../".repeat(self.current_module_name_segments_count - 1);
format!("{prefix}{module}.mjs")
eco_format!("{prefix}{module}.mjs")
}
}
} else {
// Different package
let prefix = "../".repeat(self.current_module_name_segments_count);
format!("{prefix}{package}/{module}.mjs")
eco_format!("{prefix}{package}/{module}.mjs")
}
}

Expand All @@ -413,7 +413,7 @@ impl<'a> Generator<'a> {
Some((AssignName::Variable(name), _)) => (false, name.as_str()),
};

let module_name = format!("${module_name}");
let module_name = eco_format!("${module_name}");
let path = self.import_path(package, module);
let unqualified_imports = unqualified.iter().map(|i| {
let alias = i.as_name.as_ref().map(|n| {
Expand Down Expand Up @@ -448,9 +448,9 @@ impl<'a> Generator<'a> {
},
};
if publicity.is_importable() {
imports.register_export(maybe_escape_identifier_string(name).to_string())
imports.register_export(maybe_escape_identifier_string(name))
}
imports.register_module(module.to_string(), [], [member]);
imports.register_module(EcoString::from(module), [], [member]);
}

fn module_constant(
Expand Down
22 changes: 11 additions & 11 deletions compiler-core/src/javascript/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ use crate::{
///
#[derive(Debug, Default)]
pub(crate) struct Imports<'a> {
imports: HashMap<String, Import<'a>>,
exports: HashSet<String>,
imports: HashMap<EcoString, Import<'a>>,
exports: HashSet<EcoString>,
}

impl<'a> Imports<'a> {
pub fn new() -> Self {
Self::default()
}

pub fn register_export(&mut self, export: String) {
pub fn register_export(&mut self, export: EcoString) {
let _ = self.exports.insert(export);
}

pub fn register_module(
&mut self,
path: String,
aliases: impl IntoIterator<Item = String>,
path: EcoString,
aliases: impl IntoIterator<Item = EcoString>,
unqualified_imports: impl IntoIterator<Item = Member<'a>>,
) {
let import = self
Expand All @@ -56,7 +56,7 @@ impl<'a> Imports<'a> {
self.exports
.into_iter()
.sorted()
.map(|string| EcoString::from(string).to_doc()),
.map(|string| string.to_doc()),
break_(",", ", "),
);
let names = docvec![
Expand All @@ -80,13 +80,13 @@ impl<'a> Imports<'a> {

#[derive(Debug)]
struct Import<'a> {
path: String,
aliases: HashSet<String>,
path: EcoString,
aliases: HashSet<EcoString>,
unqualified: Vec<Member<'a>>,
}

impl<'a> Import<'a> {
fn new(path: String) -> Self {
fn new(path: EcoString) -> Self {
Self {
path,
aliases: Default::default(),
Expand All @@ -95,7 +95,7 @@ impl<'a> Import<'a> {
}

pub fn into_doc(self, codegen_target: JavaScriptCodegenTarget) -> Document<'a> {
let path = EcoString::from(self.path).to_doc();
let path = self.path.to_doc();
let import_modifier = if codegen_target == JavaScriptCodegenTarget::TypeScriptDeclarations {
"type "
} else {
Expand All @@ -106,7 +106,7 @@ impl<'a> Import<'a> {
"import ",
import_modifier,
"* as ",
EcoString::from(alias),
alias,
" from \"",
path.clone(),
r#"";"#,
Expand Down
10 changes: 5 additions & 5 deletions compiler-core/src/javascript/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,28 +265,28 @@ impl<'a> TypeScriptGenerator<'a> {
///
fn register_import(&mut self, imports: &mut Imports<'a>, package: &'a str, module: &'a str) {
let path = self.import_path(package, module);
imports.register_module(path, [self.module_name(module).to_string()], []);
imports.register_module(path, [self.module_name(module)], []);
}

/// Calculates the path of where to import an external module from
///
fn import_path(&self, package: &'a str, module: &'a str) -> String {
fn import_path(&self, package: &'a str, module: &'a str) -> EcoString {
// DUPE: current_module_name_segments_count
// TODO: strip shared prefixed between current module and imported
// module to avoid descending and climbing back out again
if package == self.module.type_info.package || package.is_empty() {
// Same package
match self.current_module_name_segments_count {
1 => format!("./{module}.d.mts"),
1 => eco_format!("./{module}.d.mts"),
_ => {
let prefix = "../".repeat(self.current_module_name_segments_count - 1);
format!("{prefix}{module}.d.mts")
eco_format!("{prefix}{module}.d.mts")
}
}
} else {
// Different package
let prefix = "../".repeat(self.current_module_name_segments_count);
format!("{prefix}{package}/{module}.d.mts")
eco_format!("{prefix}{package}/{module}.d.mts")
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub struct Parser<T: Iterator<Item = LexResult>> {
tok0: Option<Spanned>,
tok1: Option<Spanned>,
extra: ModuleExtra,
doc_comments: VecDeque<(u32, String)>,
doc_comments: VecDeque<(u32, EcoString)>,
}
impl<T> Parser<T>
where
Expand Down
4 changes: 3 additions & 1 deletion compiler-core/src/parse/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ecow::EcoString;

use crate::ast::SrcSpan;
use crate::parse::error::{LexicalError, LexicalErrorType};
use crate::parse::token::Token;
Expand Down Expand Up @@ -705,7 +707,7 @@ where
}
_ => Kind::Comment,
};
let mut content = String::new();
let mut content = EcoString::new();
let start_pos = self.get_pos();
while Some('\n') != self.chr0 {
match self.chr0 {
Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum Token {
Int { value: EcoString },
Float { value: EcoString },
String { value: EcoString },
CommentDoc { content: String },
CommentDoc { content: EcoString },
// Groupings
LeftParen, // (
RightParen, // )
Expand Down
21 changes: 6 additions & 15 deletions compiler-core/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ pub enum Document<'a> {
/// Nests the given document to the current cursor position
Group(Box<Self>),

/// A string to render
String {
string: String,
/// A str to render
Str {
string: &'a str,
// The number of extended grapheme clusters in the string.
// This is what the pretty printer uses as the width of the string as it
// is closes to what a human would consider the "length" of a string.
Expand All @@ -185,9 +185,6 @@ pub enum Document<'a> {
graphemes: isize,
},

/// A str to render
Str { string: &'a str, graphemes: isize },

/// A string that is cheap to copy
EcoString { string: EcoString, graphemes: isize },
}
Expand Down Expand Up @@ -329,9 +326,9 @@ fn fits(

// When we run into a string we increase the current_width; looping
// back we will check if we've exceeded the maximum allowed width.
Document::Str { graphemes, .. }
| Document::String { graphemes, .. }
| Document::EcoString { graphemes, .. } => current_width += graphemes,
Document::Str { graphemes, .. } | Document::EcoString { graphemes, .. } => {
current_width += graphemes
}

// If we get to a break we need to first see if it has to be
// rendered as its unbroken or broken string, depending on the mode.
Expand Down Expand Up @@ -485,11 +482,6 @@ fn format(

// Strings are printed as they are and the current width is
// increased accordingly.
Document::String { string, graphemes } => {
width += graphemes;
writer.str_write(string)?;
}

Document::EcoString { string, graphemes } => {
width += graphemes;
writer.str_write(string)?;
Expand Down Expand Up @@ -674,7 +666,6 @@ impl<'a> Document<'a> {
match self {
Line(n) => *n == 0,
EcoString { string, .. } => string.is_empty(),
String { string, .. } => string.is_empty(),
Str { string, .. } => string.is_empty(),
// assuming `broken` and `unbroken` are equivalent
Break { broken, .. } => broken.is_empty(),
Expand Down

0 comments on commit e5a38f8

Please sign in to comment.