Skip to content

Commit 5feb8b3

Browse files
committed
support doc function nullable param
1 parent 2340fe8 commit 5feb8b3

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/doc/infer_type.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,14 @@ fn infer_func_type(analyzer: &mut DocAnalyzer, func: LuaDocFuncType) -> LuaType
342342
continue;
343343
};
344344

345+
let nullable = param.is_nullable();
346+
345347
let type_ref = if let Some(type_ref) = param.get_type() {
346-
Some(infer_type(analyzer, type_ref))
348+
let mut typ = infer_type(analyzer, type_ref);
349+
if nullable && !typ.is_optional() {
350+
typ = TypeOps::Union.apply(&typ, &LuaType::Nil);
351+
}
352+
Some(typ)
347353
} else {
348354
None
349355
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[cfg(test)]
2+
mod test {
3+
use crate::{DiagnosticCode, VirtualWorkspace};
4+
5+
#[test]
6+
fn test_issue_98() {
7+
let mut ws = VirtualWorkspace::new();
8+
9+
assert!(ws.check_code_for(
10+
DiagnosticCode::MissingParameter,
11+
r#"
12+
---@param callback fun(i?: integer)
13+
function foo(callback)
14+
callback()
15+
callback(1123)
16+
end
17+
"#
18+
));
19+
}
20+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mod await_in_sync_test;
2-
mod param_type_check_test;
2+
mod param_type_check_test;
3+
mod missing_parameter_test;

crates/emmylua_parser/src/grammar/doc/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ fn parse_typed_param(p: &mut LuaDocParser) -> ParseResult {
264264
}
265265
LuaTokenKind::TkDots => {
266266
p.bump();
267+
if_token_bump(p, LuaTokenKind::TkDocQuestion);
267268
}
268269
_ => {
269270
return Err(LuaParseError::from_source_range(

crates/emmylua_parser/src/syntax/node/doc/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ impl LuaDocTypeParam {
260260
pub fn get_type(&self) -> Option<LuaDocType> {
261261
self.child()
262262
}
263+
264+
pub fn is_nullable(&self) -> bool {
265+
self.token_by_kind(LuaTokenKind::TkDocQuestion).is_some()
266+
}
263267
}
264268

265269
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)