Skip to content

Commit 9dabcb2

Browse files
committed
Fix #277
1 parent a6fbcf0 commit 9dabcb2

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

crates/emmylua_code_analysis/src/compilation/analyzer/flow/var_analyze.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,12 @@ fn infer_lua_type_assert(
420420
) -> Option<()> {
421421
let binary_expr = call_expr.get_parent::<LuaBinaryExpr>()?;
422422
let op = binary_expr.get_op_token()?;
423+
let mut is_eq = true;
423424
match op.get_op() {
424425
BinaryOperator::OpEq => {}
426+
BinaryOperator::OpNe => {
427+
is_eq = false;
428+
}
425429
_ => return None,
426430
};
427431

@@ -439,7 +443,7 @@ fn infer_lua_type_assert(
439443
_ => return None,
440444
};
441445

442-
let type_assert = match type_literal.as_str() {
446+
let mut type_assert = match type_literal.as_str() {
443447
"number" => TypeAssertion::Narrow(LuaType::Number),
444448
"string" => TypeAssertion::Narrow(LuaType::String),
445449
"boolean" => TypeAssertion::Narrow(LuaType::Boolean),
@@ -452,6 +456,10 @@ fn infer_lua_type_assert(
452456
str => TypeAssertion::Narrow(LuaType::Ref(LuaTypeDeclId::new(str))),
453457
};
454458

459+
if !is_eq {
460+
type_assert = type_assert.get_negation()?;
461+
}
462+
455463
broadcast_up(
456464
db,
457465
flow_chain,

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,26 @@ end
321321
let d_desc = ws.humanize_type(d);
322322
assert_eq!(d_desc, "string");
323323
}
324+
325+
#[test]
326+
fn test_issue_277() {
327+
let mut ws = VirtualWorkspace::new();
328+
329+
ws.def(
330+
r#"
331+
---@param t? table
332+
function myfun3(t)
333+
if type(t) ~= 'table' then
334+
return
335+
end
336+
337+
a = t
338+
end
339+
"#,
340+
);
341+
342+
let a = ws.expr_ty("a");
343+
let a_desc = ws.humanize_type(a);
344+
assert_eq!(a_desc, "table");
345+
}
324346
}

crates/emmylua_code_analysis/src/db_index/type/type_assert.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl TypeAssertion {
2020
TypeAssertion::Exist => Some(TypeAssertion::NotExist),
2121
TypeAssertion::NotExist => Some(TypeAssertion::Exist),
2222
TypeAssertion::Narrow(t) => Some(TypeAssertion::Remove(t.clone())),
23+
TypeAssertion::Remove(t) => Some(TypeAssertion::Narrow(t.clone())),
2324
_ => None,
2425
}
2526
}

0 commit comments

Comments
 (0)