Skip to content

Commit ccb4c74

Browse files
refactor: improve empty module validation and warnings
1 parent aad2133 commit ccb4c74

12 files changed

+15
-42
lines changed

compiler-core/src/build/package_compiler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,9 @@ fn analyse(
578578
.filter(|def| def.is_public())
579579
.count();
580580

581-
if public_definitions == 0 {
581+
// Only emit the empty module warning if the module has no definitions at all.
582+
// Modules with only private definitions already emit their own warnings.
583+
if public_definitions == 0 && module.ast.definitions.is_empty() {
582584
warnings.emit(crate::warning::Warning::EmptyModule {
583585
path: module.input_path.clone(),
584586
name: module.name.clone(),

compiler-core/src/error.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,13 +1052,12 @@ Please make sure internal types do not appear in public functions and try again.
10521052

10531053
Error::CannotPublishEmptyModules { unfinished } => vec![Diagnostic {
10541054
title: "Cannot publish empty modules".into(),
1055-
text: format!(
1055+
text: wrap_format!(
10561056
"These modules contain no public definitions and cannot be published:
10571057
10581058
{}
10591059
1060-
Please add public functions, types, or constants to these modules, or remove them and try again.
1061-
",
1060+
Please add public functions, types, or constants to these modules, or remove them and try again.",
10621061
unfinished
10631062
.iter()
10641063
.map(|name| format!(" - {}", name.as_str()))

compiler-core/src/warning.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,20 +1396,12 @@ The imported value could not be used in this module anyway."
13961396
}
13971397
}
13981398

1399-
Warning::EmptyModule { path, name } => Diagnostic {
1399+
Warning::EmptyModule { path: _, name } => Diagnostic {
14001400
title: "Empty module".into(),
14011401
text: format!("Module '{name}' contains no public definitions."),
14021402
hint: Some("Consider adding public functions, types, or constants, or removing this module.".into()),
14031403
level: diagnostic::Level::Warning,
1404-
location: Some(Location {
1405-
label: diagnostic::Label {
1406-
text: Some("This module is empty".into()),
1407-
span: SrcSpan { start: 0, end: 0 },
1408-
},
1409-
path: path.clone(),
1410-
src: EcoString::from(""),
1411-
extra_labels: vec![],
1412-
}),
1404+
location: None,
14131405
},
14141406
}
14151407
}

test-package-compiler/cases/empty_module_publish_validation/gleam.toml renamed to test-package-compiler/cases/empty_module_warning/gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name = "empty_module_publish_validation"
1+
name = "empty_module_warning"
22
version = "1.0.0"
33
description = "Test package with empty modules"
44
licences = ["Apache-2.0"]
File renamed without changes.
File renamed without changes.

test-package-compiler/src/generated_tests.rs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: test-package-compiler/src/generated_tests.rs
3-
expression: "./cases/empty_module_publish_validation"
3+
expression: "./cases/empty_module_warning"
44
snapshot_kind: text
55
---
66
//// /out/lib/the_package/_gleam_artefacts/empty.cache
@@ -31,8 +31,8 @@ main() ->
3131
<<"This module has public definitions"/utf8>>.
3232

3333

34-
//// /out/lib/the_package/ebin/empty_module_publish_validation.app
35-
{application, empty_module_publish_validation, [
34+
//// /out/lib/the_package/ebin/empty_module_warning.app
35+
{application, empty_module_warning, [
3636
{vsn, "1.0.0"},
3737
{applications, [gleam_stdlib]},
3838
{description, "Test package with empty modules"},
@@ -44,10 +44,6 @@ main() ->
4444

4545
//// Warning
4646
warning: Empty module
47-
┌─ src/empty.gleam:1:1
48-
49-
1
50-
^ This module is empty
5147

5248
Module 'empty' contains no public definitions.
5349
Hint: Consider adding public functions, types, or constants, or removing this module.

test-package-compiler/src/snapshots/test_package_compiler__generated_tests__erlang_app_generation.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ snapshot_kind: text
3434

3535
//// Warning
3636
warning: Empty module
37-
┌─ src/main.gleam:1:1
38-
39-
1
40-
^ This module is empty
4137

4238
Module 'main' contains no public definitions.
4339
Hint: Consider adding public functions, types, or constants, or removing this module.

test-package-compiler/src/snapshots/test_package_compiler__generated_tests__erlang_empty.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ snapshot_kind: text
2828

2929
//// Warning
3030
warning: Empty module
31-
┌─ src/empty.gleam:1:1
32-
33-
1
34-
^ This module is empty
3531

3632
Module 'empty' contains no public definitions.
3733
Hint: Consider adding public functions, types, or constants, or removing this module.

0 commit comments

Comments
 (0)