Skip to content

Commit

Permalink
bools bad!!
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Oct 25, 2024
1 parent 9d8c4ef commit 17adb24
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
16 changes: 13 additions & 3 deletions compiler-core/src/build/package_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ where
JavaScript::new(&self.out, typescript, prelude_location, self.target_support).render(
&self.io,
modules,
self.stdlib_is_a_dependency(),
self.stdlib_package(),
)?;

if self.copy_native_files {
Expand Down Expand Up @@ -431,11 +431,21 @@ where
Ok(())
}

fn stdlib_is_a_dependency(&self) -> bool {
self.config.dependencies.contains_key("gleam_stdlib")
fn stdlib_package(&self) -> StdlibPackage {
if self.config.dependencies.contains_key("gleam_stdlib") {
StdlibPackage::Present
} else {
StdlibPackage::Missing
}
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum StdlibPackage {
Present,
Missing,
}

fn analyse(
package_config: &PackageConfig,
target: Target,
Expand Down
10 changes: 5 additions & 5 deletions compiler-core/src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
analyse::TargetSupport,
build::{ErlangAppCodegenConfiguration, Module},
build::{package_compiler::StdlibPackage, ErlangAppCodegenConfiguration, Module},
config::PackageConfig,
erlang,
io::FileSystemWriter,
Expand Down Expand Up @@ -182,14 +182,14 @@ impl<'a> JavaScript<'a> {
&self,
writer: &impl FileSystemWriter,
modules: &[Module],
stdlib_is_a_dependency: bool,
stdlib_package: StdlibPackage,
) -> Result<()> {
for module in modules {
let js_name = module.name.clone();
if self.typescript == TypeScriptDeclarations::Emit {
self.ts_declaration(writer, module, &js_name)?;
}
self.js_module(writer, module, &js_name, stdlib_is_a_dependency)?
self.js_module(writer, module, &js_name, stdlib_package)?
}
self.write_prelude(writer)?;
Ok(())
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<'a> JavaScript<'a> {
writer: &impl FileSystemWriter,
module: &Module,
js_name: &str,
stdblib_is_a_dependency: bool,
stdlib_package: StdlibPackage,
) -> Result<()> {
let name = format!("{js_name}.mjs");
let path = self.output_directory.join(name);
Expand All @@ -252,7 +252,7 @@ impl<'a> JavaScript<'a> {
&module.code,
self.target_support,
self.typescript,
stdblib_is_a_dependency,
stdlib_package,
);
tracing::debug!(name = ?js_name, "Generated js module");
writer.write(&path, &output?)
Expand Down
13 changes: 7 additions & 6 deletions compiler-core/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod tests;
mod typescript;

use crate::analyse::TargetSupport;
use crate::build::package_compiler::StdlibPackage;
use crate::build::Target;
use crate::codegen::TypeScriptDeclarations;
use crate::type_::PRELUDE_MODULE_NAME;
Expand Down Expand Up @@ -45,7 +46,7 @@ pub struct Generator<'a> {
current_module_name_segments_count: usize,
target_support: TargetSupport,
typescript: TypeScriptDeclarations,
stdlib_is_a_dependency: bool,
stdlib_package: StdlibPackage,
}

impl<'a> Generator<'a> {
Expand All @@ -54,7 +55,7 @@ impl<'a> Generator<'a> {
module: &'a TypedModule,
target_support: TargetSupport,
typescript: TypeScriptDeclarations,
stdlib_is_a_dependency: bool,
stdlib_package: StdlibPackage,
) -> Self {
let current_module_name_segments_count = module.name.split('/').count();

Expand All @@ -66,7 +67,7 @@ impl<'a> Generator<'a> {
module_scope: Default::default(),
target_support,
typescript,
stdlib_is_a_dependency,
stdlib_package,
}
}

Expand Down Expand Up @@ -173,7 +174,7 @@ impl<'a> Generator<'a> {
};

let echo = if self.tracker.echo_used {
if self.stdlib_is_a_dependency {
if StdlibPackage::Present == self.stdlib_package {
self.register_import(
&mut imports,
"gleam_stdlib",
Expand Down Expand Up @@ -594,14 +595,14 @@ pub fn module(
src: &EcoString,
target_support: TargetSupport,
typescript: TypeScriptDeclarations,
stdlib_is_a_dependency: bool,
stdlib_package: StdlibPackage,
) -> Result<String, crate::Error> {
let document = Generator::new(
line_numbers,
module,
target_support,
typescript,
stdlib_is_a_dependency,
stdlib_package,
)
.compile()
.map_err(|error| crate::Error::JavaScript {
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/javascript/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ pub fn compile(src: &str, deps: Vec<(&str, &str, &str)>) -> TypedModule {
pub fn compile_js(src: &str, deps: Vec<(&str, &str, &str)>) -> Result<String, crate::Error> {
let ast = compile(src, deps);
let line_numbers = LineNumbers::new(src);
let stdlib_is_a_dependency = true;
let stdlib_package = StdlibPackage::Present;
let output = module(
&ast,
&line_numbers,
Utf8Path::new(""),
&"".into(),
TargetSupport::Enforced,
TypeScriptDeclarations::None,
stdlib_is_a_dependency,
stdlib_package,
)?;

Ok(output.replace(
Expand Down

0 comments on commit 17adb24

Please sign in to comment.