Skip to content

Commit

Permalink
🎨 - Only export non-exotic module name in a namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrolich committed Jul 21, 2023
1 parent aa1f955 commit de6b525
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ fn filter_ppx_flags(ppx_flags: &Option<Vec<OneOrMore<String>>>) -> Option<Vec<On
.iter()
.filter(|flag| match (flag, filter) {
(bsconfig::OneOrMore::Single(str), Some(filter)) => !str.contains(filter),
(bsconfig::OneOrMore::Multiple(str), Some(filter)) => !str.first().unwrap().contains(filter),
_ => true
(bsconfig::OneOrMore::Multiple(str), Some(filter)) => {
!str.first().unwrap().contains(filter)
}
_ => true,
})
.map(|x| x.to_owned())
.collect::<Vec<OneOrMore<String>>>(),
Expand Down Expand Up @@ -297,7 +299,7 @@ fn get_dep_modules(
fn gen_mlmap(
package: &package_tree::Package,
namespace: &str,
depending_modules: AHashSet<String>,
depending_modules: &AHashSet<String>,
root_path: &str,
) -> String {
let build_path_abs = helpers::get_build_path(root_path, &package.name);
Expand All @@ -314,6 +316,10 @@ fn gen_mlmap(
let mut modules = Vec::from_iter(depending_modules.to_owned());
modules.sort();
for module in modules {
// check if the module names is referencible in code (no exotic module names)
// (only contains A-Z a-z 0-9 and _ and only starts with a capital letter)
// if not, it does not make sense to export as part of the name space
// this helps compile times of exotic modules such as MyModule.test
file.write_all(module.as_bytes()).unwrap();
file.write_all(b"\n").unwrap();
}
Expand Down Expand Up @@ -638,20 +644,26 @@ pub fn parse_packages(build_state: &mut BuildState) {
true
}
})
.filter(|module_name| helpers::is_non_exotic_module_name(module_name))
.collect::<AHashSet<String>>();

let mlmap = gen_mlmap(
&package,
namespace,
depending_modules,
&depending_modules,
&build_state.project_root,
);

// mlmap will be compiled in the AST generation step
// compile_mlmap(&package, namespace, &project_root);

let deps = source_files
.iter()
.filter(|path| {
helpers::is_non_exotic_module_name(&helpers::file_path_to_module_name(
&path,
&package_tree::Namespace::NoNamespace,
))
})
.map(|path| helpers::file_path_to_module_name(&path, &package.namespace))
.filter(|module_name| {
if let Some(entry) = entry {
Expand All @@ -669,7 +681,7 @@ pub fn parse_packages(build_state: &mut BuildState) {
),
Module {
source_type: SourceType::MlMap(MlMap { dirty: false }),
deps: deps,
deps,
reverse_deps: AHashSet::new(),
package_name: package.name.to_owned(),
compile_dirty: false,
Expand Down
10 changes: 10 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,13 @@ pub fn is_implementation_file(extension: &str) -> bool {
pub fn is_source_file(extension: &str) -> bool {
is_interface_file(extension) || is_implementation_file(extension)
}

pub fn is_non_exotic_module_name(module_name: &str) -> bool {
let mut chars = module_name.chars();
if chars.next().unwrap().is_ascii_uppercase()
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
{
return true;
}
return false;
}

0 comments on commit de6b525

Please sign in to comment.