Skip to content

Commit 951a14c

Browse files
committed
make filed check donot so strict
1 parent fb7942f commit 951a14c

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

crates/emmylua_code_analysis/src/diagnostic/checker/check_field.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashSet;
22

3-
use emmylua_parser::{LuaAst, LuaAstNode, LuaExpr, LuaIndexExpr, LuaVarExpr};
3+
use emmylua_parser::{LuaAst, LuaAstNode, LuaExpr, LuaIndexExpr, LuaIndexKey, LuaVarExpr};
44

55
use 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)]
102106
fn 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+
}

crates/emmylua_code_analysis/src/diagnostic/test/inject_field_test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,16 @@ mod test {
105105
"#
106106
));
107107
}
108+
109+
#[test]
110+
fn test_issue_264() {
111+
let mut ws = VirtualWorkspace::new();
112+
assert!(ws.check_code_for(
113+
DiagnosticCode::InjectField,
114+
r#"
115+
local a = { 'a' }
116+
a[#a + 1] = 'b'
117+
"#
118+
));
119+
}
108120
}

0 commit comments

Comments
 (0)