Skip to content

Commit 7a9d355

Browse files
committed
Add test:
Fix #240 Fix #241
1 parent 3b9094f commit 7a9d355

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

crates/emmylua_code_analysis/src/compilation/test/closure_generic.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,65 @@ mod test {
1616
"#,
1717
);
1818
}
19+
20+
#[test]
21+
fn test_issue_240() {
22+
let mut ws = VirtualWorkspace::new();
23+
24+
ws.def(
25+
r#"
26+
--- @generic T
27+
--- @param fn fun(...:T...)
28+
--- @return fun(...:T...)
29+
local function wrap(fn)
30+
return fn
31+
end
32+
33+
--- @param a integer
34+
foo = wrap(function(a)
35+
end) -- type unknown
36+
37+
--- @param a integer
38+
bar = wrap(function(a)
39+
_ = a
40+
end) -- type fun(a: integer)
41+
"#,
42+
);
43+
44+
let foo = ws.expr_ty("foo");
45+
let foo_desc = ws.humanize_type(foo);
46+
assert_eq!(foo_desc, "fun(a: integer)");
47+
48+
let bar = ws.expr_ty("bar");
49+
let bar_desc = ws.humanize_type(bar);
50+
assert_eq!(bar_desc, "fun(a: integer)");
51+
}
52+
53+
#[test]
54+
fn test_issue_241() {
55+
let mut ws = VirtualWorkspace::new();
56+
57+
ws.def(
58+
r#"
59+
--- @generic T
60+
--- @param fn fun(...:T...)
61+
--- @return fun(...:T...)
62+
local function wrap(fn) return fn end
63+
64+
E = {}
65+
66+
--- @param a integer
67+
E.foo = function(a) _ = a end -- type fun(a: integer) - correct
68+
69+
--- @param a integer
70+
E.foo_wrapped = wrap(function(a) _ = a end) -- type fun(a: integer) - correct
71+
E.foo_wrapped2_a = wrap(wrap(E.foo)) -- type fun(a: integer) - correct
72+
E.foo_wrapped2_b = wrap(E.foo_wrapped) -- type fun() - wrong
73+
"#,
74+
);
75+
76+
let foo_wrapper2_b = ws.expr_ty("E.foo_wrapped2_b");
77+
let desc = ws.humanize_type(foo_wrapper2_b);
78+
assert_eq!(desc, "fun(a: integer)");
79+
}
1980
}

crates/emmylua_code_analysis/src/test_lib/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use lsp_types::NumberOrString;
55
use tokio_util::sync::CancellationToken;
66

77
use crate::{
8-
check_type_compact, DiagnosticCode, EmmyLuaAnalysis, Emmyrc, FileId, LuaType,
9-
VirtualUrlGenerator,
8+
check_type_compact, humanize_type, DiagnosticCode, EmmyLuaAnalysis, Emmyrc, FileId, LuaType,
9+
RenderLevel, VirtualUrlGenerator,
1010
};
1111

1212
/// A virtual workspace for testing.
@@ -177,6 +177,12 @@ impl VirtualWorkspace {
177177
emmyrc.diagnostics.enables = enables;
178178
self.analysis.diagnostic.update_config(Arc::new(emmyrc));
179179
}
180+
181+
pub fn humanize_type(&self, ty: LuaType) -> String {
182+
let db = &self.analysis.compilation.get_db();
183+
let level = 0;
184+
humanize_type(db, &ty, RenderLevel::Brief)
185+
}
180186
}
181187

182188
#[cfg(test)]

0 commit comments

Comments
 (0)