Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use crate::{
use super::type_eval::{eval_conditionals, expand_alias_generic, expand_index_call, expand_mapped};
use super::{SemanticModel, infer};

#[cfg(test)]
mod tests;

// ─── RenderLevel ────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -317,7 +320,9 @@ impl<'a> TypeHumanizer<'a> {

/// Resolves default generic arguments of a type definition, supporting forward/backward references and iterating to a fixpoint.
fn resolve_generic_defaults(&self, def: &TypeDef, provided: &[LuaType]) -> Vec<LuaType> {
let n = def.generic_params.len();
// Preserve provided arguments even when the definition declares fewer
// parameters (e.g. standard-library table or a class with excess arguments).
let n = def.generic_params.len().max(provided.len());
let mut resolved: Vec<Option<LuaType>> = vec![None; n];
for (i, ty) in provided.iter().enumerate() {
if i < n {
Expand Down
111 changes: 111 additions & 0 deletions crates/emmylua_code_analysis/src/semantic_model/render/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use googletest::prelude::*;

use crate::{DiagnosticCode, VirtualWorkspace};

#[gtest]
fn explicit_table_arguments_survive_standard_library_definition() {
let mut ws = VirtualWorkspace::new_with_init_std_lib();
ws.def("---@class Train\n---@field id integer");

for annotation in [
"table<integer,Train>",
"table<integer>",
"table<unknown,Train>",
"table<string,table<integer,Train>>",
] {
let ty = ws.ty(annotation);
assert_that!(
ws.humanize_type(ty.clone()),
eq(annotation),
"annotation: {annotation}"
);
assert_that!(
ws.humanize_type_detailed(ty),
eq(annotation),
"annotation: {annotation}"
);
}
}

#[gtest]
fn rendering_preserves_arguments_beyond_declared_parameters() {
let mut ws = VirtualWorkspace::new();
ws.def("---@class Plain\n---@class Box<T>");

for annotation in [
"Plain<string>",
"Box<string,integer>",
"Missing<string,integer>",
] {
let ty = ws.ty(annotation);
assert_that!(
ws.humanize_type(ty.clone()),
eq(annotation),
"annotation: {annotation}"
);
assert_that!(
ws.humanize_type_detailed(ty),
eq(annotation),
"annotation: {annotation}"
);
}
}

#[gtest]
fn rendering_still_fills_defaults_and_missing_parameters() {
let mut ws = VirtualWorkspace::new();
ws.def("---@class Pair<T, U = T[]>\n---@class Required<T, U>");

for (annotation, expected) in [
("Pair<string>", "Pair<string,string[]>"),
("Pair<string,integer>", "Pair<string,integer>"),
("Required<string>", "Required<string,unknown>"),
] {
let ty = ws.ty(annotation);
assert_that!(
ws.humanize_type(ty.clone()),
eq(expected),
"annotation: {annotation}"
);
assert_that!(
ws.humanize_type_detailed(ty),
eq(expected),
"annotation: {annotation}"
);
}
}

#[gtest]
fn rendering_excess_arguments_keeps_alias_expansion() {
let mut ws = VirtualWorkspace::new();
ws.def("---@alias List<T> T[]\n---@alias Value<T> T['value']");
for (annotation, detailed) in [
("List<string,integer>", "List<string,integer> = string[]"),
(
"Value<{value:integer},string>",
"Value<{ value: integer },string> = integer",
),
] {
let ty = ws.ty(annotation);
assert_that!(
ws.humanize_type_detailed(ty),
eq(detailed),
"annotation: {annotation}"
);
}
}

#[gtest]
fn rendering_does_not_change_missing_argument_diagnostics() {
let mut ws = VirtualWorkspace::new();
ws.def("---@class Required<T, U>");
let ty = ws.ty("Required<string>");
assert_that!(ws.humanize_type(ty), eq("Required<string,unknown>"));
assert_that!(
ws.has_no_diagnostic(
DiagnosticCode::MissingTypeArgument,
"---@type Required<string>\nlocal value"
),
eq(false)
);
}
Loading