Skip to content
Merged
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
13 changes: 13 additions & 0 deletions crates/emmylua_code_analysis/src/compilation/test/generic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,4 +816,17 @@ mod test {
let result_ty = ws.expr_ty("result");
assert_eq!(ws.humanize_type(result_ty), "integer");
}

#[test]
fn test_extends_true() {
let mut ws = VirtualWorkspace::new();
assert!(ws.check_code_for(
DiagnosticCode::TypeNotFound,
r#"
---@alias TestA<T> T extends "test" and number or string
---@alias TestB<T> T extends true and number or string
---@alias TestC<T> T extends 111 and number or string
"#,
));
}
Comment on lines +821 to +831
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test name test_extends_true is a bit specific given it also tests string and number literals. To fully validate the changes in this PR, which aims to support true, false, and nil, it would be beneficial to expand this test to include cases for false and nil as well. Also, consider renaming the test to something more general like test_extends_literal.

Here's a suggested update:

#[test]
fn test_extends_literal() {
    let mut ws = VirtualWorkspace::new();
    assert!(ws.check_code_for(
        DiagnosticCode::TypeNotFound,
        r#"
        ---@alias TestA<T> T extends "test" and number or string
        ---@alias TestB<T> T extends true and number or string
        ---@alias TestC<T> T extends 111 and number or string
        ---@alias TestD<T> T extends false and number or string
        ---@alias TestE<T> T extends nil and number or string
        "#,
    ));
}

Note that for the nil case to pass, the parser will also need to be updated to recognize TkNil as a literal type, which seems to be missing from this PR.

    #[test]
    fn test_extends_literal() {
        let mut ws = VirtualWorkspace::new();
        assert!(ws.check_code_for(
            DiagnosticCode::TypeNotFound,
            r#"
            ---@alias TestA<T> T extends "test" and number or string
            ---@alias TestB<T> T extends true and number or string
            ---@alias TestC<T> T extends 111 and number or string
            ---@alias TestD<T> T extends false and number or string
            ---@alias TestE<T> T extends nil and number or string
            "#,
        ));
    }

}
3 changes: 3 additions & 0 deletions crates/emmylua_parser/src/lexer/lua_doc_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ impl LuaDocLexer<'_> {
ch if is_name_start(ch) => {
let (text, _) = read_doc_name(reader);
match text {
"true" => LuaTokenKind::TkTrue,
"false" => LuaTokenKind::TkFalse,
"nil" => LuaTokenKind::TkNil,
"new" => LuaTokenKind::TkDocNew,
_ => LuaTokenKind::TkName,
}
Expand Down