11use std:: collections:: HashSet ;
22
3- use emmylua_parser:: { LuaAst , LuaAstNode , LuaExpr , LuaIndexExpr , LuaVarExpr } ;
3+ use emmylua_parser:: { LuaAst , LuaAstNode , LuaExpr , LuaIndexExpr , LuaIndexKey , LuaVarExpr } ;
44
55use crate :: { DiagnosticCode , InferFailReason , LuaType , SemanticModel } ;
66
@@ -54,11 +54,6 @@ fn check_index_expr(
5454 code : DiagnosticCode ,
5555) -> Option < ( ) > {
5656 let db = context. db ;
57- let result = semantic_model. infer_expr ( LuaExpr :: IndexExpr ( index_expr. clone ( ) ) ) ;
58- match result {
59- Err ( InferFailReason :: FieldDotFound ) => { }
60- _ => return Some ( ( ) ) ,
61- }
6257
6358 let index_key = index_expr. get_index_key ( ) ?;
6459 let prefix_typ = semantic_model
@@ -69,6 +64,16 @@ fn check_index_expr(
6964 return Some ( ( ) ) ;
7065 }
7166
67+ if !is_valid_index_key ( & index_key) {
68+ return Some ( ( ) ) ;
69+ }
70+
71+ let result = semantic_model. infer_expr ( LuaExpr :: IndexExpr ( index_expr. clone ( ) ) ) ;
72+ match result {
73+ Err ( InferFailReason :: FieldDotFound ) => { }
74+ _ => return Some ( ( ) ) ,
75+ }
76+
7277 let index_name = index_key. get_path_part ( ) ;
7378 match code {
7479 DiagnosticCode :: InjectField => {
@@ -98,7 +103,6 @@ fn check_index_expr(
98103 Some ( ( ) )
99104}
100105
101- #[ allow( dead_code) ]
102106fn is_valid_prefix_type ( typ : & LuaType ) -> bool {
103107 let mut current_typ = typ;
104108 loop {
@@ -116,3 +120,10 @@ fn is_valid_prefix_type(typ: &LuaType) -> bool {
116120 }
117121 }
118122}
123+
124+ fn is_valid_index_key ( index_key : & LuaIndexKey ) -> bool {
125+ match index_key {
126+ LuaIndexKey :: String ( _) | LuaIndexKey :: Name ( _) | LuaIndexKey :: Integer ( _) => true ,
127+ _ => false ,
128+ }
129+ }
0 commit comments