File tree Expand file tree Collapse file tree
crates/emmylua_code_analysis/src Expand file tree Collapse file tree Original file line number Diff line number Diff 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,
Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments