Skip to content

Commit 2a67d7b

Browse files
committed
Fix #373
1 parent d2ecc00 commit 2a67d7b

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,34 @@ end
590590
assert_eq!(b, b_expected);
591591
}
592592

593+
#[test]
594+
fn test_issue_373() {
595+
let mut ws = VirtualWorkspace::new();
596+
597+
ws.def(
598+
r#"
599+
--- @alias myalias string|string[]
600+
601+
--- @param x myalias
602+
function foo(x)
603+
if type(x) == 'string' then
604+
a = x
605+
elseif type(x) == 'table' then
606+
b = x
607+
end
608+
end
609+
"#,
610+
);
611+
612+
let a = ws.expr_ty("a");
613+
let a_expected = ws.ty("string");
614+
assert_eq!(a, a_expected);
615+
616+
let b = ws.expr_ty("b");
617+
let b_expected = ws.ty("string[]");
618+
assert_eq!(b, b_expected);
619+
}
620+
593621
#[test]
594622
fn test_issue_405() {
595623
let mut ws = VirtualWorkspace::new();

crates/emmylua_code_analysis/src/db_index/type/type_ops/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,16 @@ impl TypeOps {
4747
}
4848
}
4949
}
50+
51+
pub fn get_real_type<'a>(db: &'a DbIndex, compact_type: &'a LuaType) -> Option<&'a LuaType> {
52+
match compact_type {
53+
LuaType::Ref(type_decl_id) => {
54+
let type_decl = db.get_type_index().get_type_decl(type_decl_id)?;
55+
if type_decl.is_alias() {
56+
return get_real_type(db, type_decl.get_alias_ref()?);
57+
}
58+
Some(compact_type)
59+
}
60+
_ => Some(compact_type),
61+
}
62+
}

crates/emmylua_code_analysis/src/db_index/type/type_ops/narrow_type.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::{DbIndex, LuaType, LuaUnionType};
22

3+
use super::get_real_type;
4+
35
// need to be optimized
46
pub fn narrow_down_type(db: &DbIndex, source: LuaType, target: LuaType) -> Option<LuaType> {
57
if source == target {
@@ -45,9 +47,15 @@ pub fn narrow_down_type(db: &DbIndex, source: LuaType, target: LuaType) -> Optio
4547
LuaType::Ref(type_decl_id) | LuaType::Def(type_decl_id) => {
4648
let type_decl = db.get_type_index().get_type_decl(type_decl_id)?;
4749
// enum 在实际使用时实际上是 enum.field, 并不等于 table
48-
if !type_decl.is_enum() {
49-
return Some(source);
50+
if type_decl.is_enum() {
51+
return None;
52+
}
53+
if type_decl.is_alias() {
54+
if let Some(alias_ref) = get_real_type(db, &source) {
55+
return narrow_down_type(db, alias_ref.clone(), target);
56+
}
5057
}
58+
return Some(source);
5159
}
5260
_ => {}
5361
},

crates/emmylua_code_analysis/src/db_index/type/type_ops/remove_type.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::{DbIndex, LuaType, LuaUnionType};
22

3+
use super::get_real_type;
4+
35
pub fn remove_type(db: &DbIndex, source: LuaType, removed_type: LuaType) -> Option<LuaType> {
46
if source == removed_type {
57
match source {
@@ -163,16 +165,3 @@ pub fn remove_type(db: &DbIndex, source: LuaType, removed_type: LuaType) -> Opti
163165

164166
Some(source)
165167
}
166-
167-
fn get_real_type<'a>(db: &'a DbIndex, compact_type: &'a LuaType) -> Option<&'a LuaType> {
168-
match compact_type {
169-
LuaType::Ref(type_decl_id) => {
170-
let type_decl = db.get_type_index().get_type_decl(type_decl_id)?;
171-
if type_decl.is_alias() {
172-
return get_real_type(db, type_decl.get_alias_ref()?);
173-
}
174-
Some(compact_type)
175-
}
176-
_ => Some(compact_type),
177-
}
178-
}

0 commit comments

Comments
 (0)