Skip to content

Commit d52d084

Browse files
GearsDatapackslpil
authored andcommitted
Favour public type aliases over internal types
1 parent b495dcf commit d52d084

13 files changed

+295
-29
lines changed

compiler-core/src/exhaustiveness/printer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ impl<'a> Printer<'a> {
5454
..
5555
} => {
5656
let (module, name) = match self.names.named_constructor(module, name) {
57-
NameContextInformation::Qualified(m, n) => (Some(m), n),
58-
NameContextInformation::Unqualified(n) => (None, n),
59-
NameContextInformation::Unimported(n) => {
60-
(Some(module.split('/').next_back().unwrap_or(module)), n)
57+
NameContextInformation::Qualified(module, name) => (Some(module), name),
58+
NameContextInformation::Unqualified(name) => (None, name),
59+
NameContextInformation::Unimported(module, name) => {
60+
(module.split('/').next_back(), name)
6161
}
6262
};
6363

compiler-core/src/language_server/code_action.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5014,7 +5014,7 @@ fn pretty_constructor_name(
50145014
.names
50155015
.named_constructor(constructor_module, constructor_name)
50165016
{
5017-
type_::printer::NameContextInformation::Unimported(_) => None,
5017+
type_::printer::NameContextInformation::Unimported(_, _) => None,
50185018
type_::printer::NameContextInformation::Unqualified(constructor_name) => {
50195019
Some(eco_format!("{constructor_name}"))
50205020
}

compiler-core/src/language_server/tests/action.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9796,3 +9796,101 @@ fn remove_unreachable_branches_does_not_pop_up_if_all_branches_are_reachable() {
97969796
find_position_of("Ok(n)").to_selection()
97979797
);
97989798
}
9799+
9800+
#[test]
9801+
fn add_type_annotations_public_alias_to_internal_type_aliased_module() {
9802+
let src = "
9803+
import package as pkg
9804+
9805+
pub fn main() {
9806+
pkg.make_wibble()
9807+
}
9808+
";
9809+
9810+
assert_code_action!(
9811+
ADD_ANNOTATION,
9812+
TestProject::for_source(src)
9813+
.add_package_module(
9814+
"package",
9815+
"package",
9816+
"
9817+
import package/internal
9818+
9819+
pub type Wibble = internal.Wibble
9820+
9821+
pub fn make_wibble() {
9822+
internal.Wibble
9823+
}
9824+
"
9825+
)
9826+
.add_package_module("package", "package/internal", "pub type Wibble { Wibble }"),
9827+
find_position_of("main").to_selection(),
9828+
);
9829+
}
9830+
9831+
// https://github.com/gleam-lang/gleam/issues/3898
9832+
#[test]
9833+
fn add_type_annotations_public_alias_to_internal_type() {
9834+
let src = "
9835+
import package
9836+
9837+
pub fn main() {
9838+
package.make_wibble()
9839+
}
9840+
";
9841+
9842+
assert_code_action!(
9843+
ADD_ANNOTATION,
9844+
TestProject::for_source(src)
9845+
.add_package_module(
9846+
"package",
9847+
"package",
9848+
"
9849+
import package/internal
9850+
9851+
pub type Wibble = internal.Wibble
9852+
9853+
pub fn make_wibble() {
9854+
internal.Wibble
9855+
}
9856+
"
9857+
)
9858+
.add_package_module("package", "package/internal", "pub type Wibble { Wibble }"),
9859+
find_position_of("main").to_selection(),
9860+
);
9861+
}
9862+
9863+
#[test]
9864+
fn add_type_annotations_public_alias_to_internal_generic_type() {
9865+
let src = "
9866+
import package
9867+
9868+
pub fn main() {
9869+
package.make_wibble(10)
9870+
}
9871+
";
9872+
9873+
assert_code_action!(
9874+
ADD_ANNOTATION,
9875+
TestProject::for_source(src)
9876+
.add_package_module(
9877+
"package",
9878+
"package",
9879+
"
9880+
import package/internal
9881+
9882+
pub type Wibble(a, b) = internal.Wibble(a, b)
9883+
9884+
pub fn make_wibble(x) {
9885+
internal.Wibble(x)
9886+
}
9887+
"
9888+
)
9889+
.add_package_module(
9890+
"package",
9891+
"package/internal",
9892+
"pub type Wibble(a, b) { Wibble(a) }"
9893+
),
9894+
find_position_of("main").to_selection(),
9895+
);
9896+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\nimport package\n\npub fn main() {\n package.make_wibble(10)\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
import package
8+
9+
pub fn main() {
10+
11+
package.make_wibble(10)
12+
}
13+
14+
15+
----- AFTER ACTION
16+
17+
import package
18+
19+
pub fn main() -> package.Wibble(Int, a) {
20+
package.make_wibble(10)
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\nimport package\n\npub fn main() {\n package.make_wibble()\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
import package
8+
9+
pub fn main() {
10+
11+
package.make_wibble()
12+
}
13+
14+
15+
----- AFTER ACTION
16+
17+
import package
18+
19+
pub fn main() -> package.Wibble {
20+
package.make_wibble()
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\nimport package as pkg\n\npub fn main() {\n pkg.make_wibble()\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
import package as pkg
8+
9+
pub fn main() {
10+
11+
pkg.make_wibble()
12+
}
13+
14+
15+
----- AFTER ACTION
16+
17+
import package as pkg
18+
19+
pub fn main() -> pkg.Wibble {
20+
pkg.make_wibble()
21+
}

compiler-core/src/parse/snapshots/gleam_core__parse__tests__const_string_concat.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Parsed {
100100
imported_modules: {},
101101
type_variables: {},
102102
local_value_constructors: {},
103+
reexport_aliases: {},
103104
},
104105
unused_definition_positions: {},
105106
},

compiler-core/src/parse/snapshots/gleam_core__parse__tests__deprecation_attribute_on_type_variant.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Parsed {
6969
imported_modules: {},
7070
type_variables: {},
7171
local_value_constructors: {},
72+
reexport_aliases: {},
7273
},
7374
unused_definition_positions: {},
7475
},

compiler-core/src/parse/snapshots/gleam_core__parse__tests__import_type.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Parsed {
6969
imported_modules: {},
7070
type_variables: {},
7171
local_value_constructors: {},
72+
reexport_aliases: {},
7273
},
7374
unused_definition_positions: {},
7475
},

compiler-core/src/parse/snapshots/gleam_core__parse__tests__private_opaque_type_is_parsed.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Parsed {
5353
imported_modules: {},
5454
type_variables: {},
5555
local_value_constructors: {},
56+
reexport_aliases: {},
5657
},
5758
unused_definition_positions: {},
5859
},

0 commit comments

Comments
 (0)