Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1555,4 +1555,36 @@ mod test {
"#,
));
}

#[test]
fn test_cast_bracket_index_narrows_type() {
let mut ws = VirtualWorkspace::new();

// @cast addresses[1] -nil should narrow addresses[1] from string|nil to string
assert!(ws.check_code_for(
DiagnosticCode::ParamTypeMismatch,
r#"
---@param addr string
local function connect(addr) end

---@type string[]
local addresses = { "127.0.0.1" }
---@cast addresses[1] -nil
connect(addresses[1])
"#,
));

// Without @cast, addresses[1] is string|nil, should report param-type-mismatch
assert!(!ws.check_code_for(
DiagnosticCode::ParamTypeMismatch,
r#"
---@param addr string
local function connect(addr) end

---@type string[]
local addresses = { "127.0.0.1" }
connect(addresses[1])
"#,
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -818,4 +818,45 @@ mod test {
"#
));
}

#[test]
fn test_intersection_array_index_access() {
let mut ws = VirtualWorkspace::new();

// Explicit intersection type annotation
assert!(ws.check_code_for(
DiagnosticCode::UndefinedField,
r#"
---@type integer[] & { n: integer }
local values
local e = values[1]
"#
));
}

#[test]
fn test_array_index_with_cast() {
let mut ws = VirtualWorkspace::new();

// Accessing [1] on a string[] should not report undefined-field
assert!(ws.check_code_for(
DiagnosticCode::UndefinedField,
r#"
---@type string[]
local addresses
local a = addresses[1]
"#
));

// Accessing [1] on a string[] with @cast should not report undefined-field
assert!(ws.check_code_for(
DiagnosticCode::UndefinedField,
r#"
---@type string[]
local addresses
---@cast addresses[1] -nil
local a = addresses[1]
"#
));
}
}
43 changes: 34 additions & 9 deletions crates/emmylua_parser/src/grammar/doc/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,16 +499,41 @@ fn parse_cast_expr(p: &mut LuaDocParser) -> DocParseResult {
let m = p.mark(LuaSyntaxKind::NameExpr);
p.bump();
let mut cm = m.complete(p);
// 处理多级字段访问
while p.current_token() == LuaTokenKind::TkDot {
let index_m = cm.precede(p, LuaSyntaxKind::IndexExpr);
p.bump();
if p.current_token() == LuaTokenKind::TkName {
p.bump();
} else {
// 找不到也不报错
// 处理多级字段访问(支持 `.` 和 `[]` 索引)
loop {
match p.current_token() {
LuaTokenKind::TkDot => {
let index_m = cm.precede(p, LuaSyntaxKind::IndexExpr);
p.bump();
if p.current_token() == LuaTokenKind::TkName {
p.bump();
}
cm = index_m.complete(p);
}
LuaTokenKind::TkLeftBracket => {
let index_m = cm.precede(p, LuaSyntaxKind::IndexExpr);
p.bump();
// Wrap the index value in a LiteralExpr node so that
// LuaIndexExpr::get_index_key() can find it (it expects
// a child Node, not a bare token).
if p.current_token() == LuaTokenKind::TkInt
|| p.current_token() == LuaTokenKind::TkString
{
let literal_m = p.mark(LuaSyntaxKind::LiteralExpr);
p.bump();
literal_m.complete(p);
} else if p.current_token() == LuaTokenKind::TkName {
let name_m = p.mark(LuaSyntaxKind::NameExpr);
p.bump();
name_m.complete(p);
}
if p.current_token() == LuaTokenKind::TkRightBracket {
p.bump();
}
cm = index_m.complete(p);
}
_ => break,
}
cm = index_m.complete(p);
}

Ok(cm)
Expand Down
13 changes: 13 additions & 0 deletions crates/emmylua_parser/src/lexer/lua_doc_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,24 @@ impl LuaDocLexer<'_> {
reader.bump();
LuaTokenKind::TkDot
}
'[' => {
reader.bump();
LuaTokenKind::TkLeftBracket
}
']' => {
reader.bump();
LuaTokenKind::TkRightBracket
}
ch if is_name_start(ch) => {
reader.bump();
reader.eat_while(is_name_continue);
LuaTokenKind::TkName
}
ch if ch.is_ascii_digit() => {
reader.bump();
reader.eat_while(|c| c.is_ascii_digit());
LuaTokenKind::TkInt
}
_ => self.lex_normal(),
}
}
Expand Down
Loading