emmylua_code_analysis 0.24.0

A library for analyzing lua code.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#[cfg(test)]
mod test {
    use crate::{DiagnosticCode, LuaType, VirtualWorkspace};

    const STACKED_PCALL_ALIAS_GUARDS: usize = 180;

    #[test]
    fn test_issue_263() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@alias aaa fun(a: string, b: integer): integer

        ---@type aaa
        local a

        d, b = pcall(a, "", 1)
        "#,
        );

        let aaa_ty = ws.expr_ty("b");
        let expected = ws.ty("integer|string");
        assert_eq!(aaa_ty, expected);
    }

    #[test]
    fn test_issue_280() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        assert!(ws.has_no_diagnostic(
            DiagnosticCode::ParamTypeMismatch,
            r#"
        ---@class D11.AAA
        local AAA = {}

        ---@param a string
        ---@param b number
        function AAA:name(a, b)
        end

        ---@param a string
        ---@param b number
        function AAA:t(a, b)
            local ok, err = pcall(self.name, self, a, b)
        end
        "#
        ));
    }

    #[test]
    fn test_nested_pcall_higher_order_return_shape() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@return integer
        local function f()
            return 1
        end

        ok, status, payload = pcall(pcall, f)
        "#,
        );

        assert_eq!(ws.expr_ty("status"), ws.ty("true|false|string"));
        assert_eq!(ws.expr_ty("payload"), ws.ty("string|integer|nil"));
    }

    #[test]
    fn test_pcall_return_overload_narrow_after_error_guard() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
            ---@return integer
            local function foo()
                return 2
            end

            local ok, result = pcall(foo)

            if not ok then
                error(result)
            end

            a = result
            "#,
        );

        assert_eq!(ws.expr_ty("a"), ws.ty("integer"));
    }

    #[test]
    fn test_pcall_array_return_narrow_after_error_guard() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@class Runner

        ---@class File

        ---@param specs string[]
        ---@param runner Runner
        ---@return File[] files
        local function startTests(specs, runner)
            local ok, result = pcall(function ()
                ---@type File[]
                local files

                return files
            end)

            outside = result
            if not ok then
                error(result)
            end
            ---@cast result - string

            narrowed = result
            return result
        end
        "#,
        );

        assert_eq!(ws.expr_ty("outside"), ws.ty("File[]|string"));
        assert_eq!(ws.expr_ty("narrowed"), ws.ty("File[]"));
    }

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

        ws.def(
            r#"
        ---@generic T, R
        ---@param f fun(...: T...): R...
        ---@param ... T...
        ---@return boolean, R...
        local function safe_call(f, ...)
            return true, f(...)
        end

        ---@return integer
        local function produce()
            return 1
        end

        ok, status, payload = safe_call(safe_call, produce)
        "#,
        );

        assert_eq!(ws.expr_ty("status"), ws.ty("boolean"));
        assert_eq!(ws.expr_ty("payload"), ws.ty("integer"));
    }

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

        ws.def(
            r#"
        ---@generic T, R, R1
        ---@param f sync fun(...: T...): R1, R...
        ---@param ... T...
        ---@return boolean, R1|string, R...
        local function pcall_like(f, ...) end

        ---@return integer
        local function produce()
            return 1
        end

        ok, status, payload = pcall_like(pcall_like, produce)
        "#,
        );

        assert_eq!(ws.expr_ty("ok"), ws.ty("boolean"));
        assert_eq!(ws.expr_ty("status"), ws.ty("boolean|string"));
        assert_eq!(ws.expr_ty("payload"), ws.ty("integer|string"));
    }

    #[test]
    fn test_pcall_stacked_alias_guards_build_semantic_model() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();
        let repeated_guards =
            "if failed then error(result) end\n".repeat(STACKED_PCALL_ALIAS_GUARDS);
        let block = format!(
            r#"
        ---@return integer
        local function foo()
            return 1
        end

        local ok, result = pcall(foo)
        local failed = ok == false

        {repeated_guards}
        narrowed = result
        "#,
        );

        let file_id = ws.def(&block);

        assert!(
            ws.analysis
                .compilation
                .get_semantic_model(file_id)
                .is_some(),
            "expected semantic model for stacked pcall alias guard repro"
        );
        assert_eq!(ws.expr_ty("narrowed"), ws.ty("integer"));
    }

    #[test]
    fn test_pcall_alias_callee_narrows_return_after_error_guard() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@return integer
        local function foo()
            return 1
        end

        local runner = pcall
        local ok, result = runner(foo)

        if not ok then
            error(result)
        end

        narrowed = result
        "#,
        );

        assert_eq!(ws.expr_ty("narrowed"), ws.ty("integer"));
    }

    #[test]
    fn test_pcall_narrows_type_guarded_callable_return_after_error_guard() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@param a string|fun(): integer
        local function foo(a)
            if type(a) == "string" then
                return
            end

            local ok, result = pcall(a)
            if not ok then
                return
            end

            narrowed = result
        end
        "#,
        );

        let narrowed = ws.expr_ty("narrowed");
        assert_eq!(ws.humanize_type(narrowed), "integer");
    }

    #[test]
    fn test_pcall_narrows_type_guarded_callable_return_with_forwarded_arg() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@param a string|fun(value: integer): string
        local function foo(a)
            if type(a) == "string" then
                return
            end

            local ok, result = pcall(a, 1)
            if not ok then
                return
            end

            narrowed = result
        end
        "#,
        );

        let narrowed = ws.expr_ty("narrowed");
        assert_eq!(ws.humanize_type(narrowed), "string");
    }

    #[test]
    fn test_pcall_narrows_type_guarded_callable_return_with_table_arg() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@param cb string|fun(a: {}): integer
        local function foo(cb)
            if type(cb) == "string" then
                return
            end

            local ok, result = pcall(cb, {})
            if not ok then
                return
            end

            narrowed = result
        end
        "#,
        );

        let narrowed = ws.expr_ty("narrowed");
        assert_eq!(ws.humanize_type(narrowed), "integer");
    }

    #[test]
    fn test_pcall_any_callable_splits_success_unknown_and_failure_string() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@type any
        local x

        local ok, result = pcall(x)
        outside = result
        if ok then
            success = result
        else
            failure = result
        end
        "#,
        );

        assert_eq!(ws.expr_ty("outside"), ws.ty("unknown|string"));
        assert_eq!(ws.expr_ty("success"), ws.ty("unknown"));
        assert_eq!(ws.expr_ty("failure"), ws.ty("string"));
    }

    #[test]
    fn test_issue_1020_pcall_preserves_pairs_value_function_return() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        local t = {
            { id = 1, func = function() return a > 0 end },
            { id = 2, func = function() return a > 0 end },
            { id = 3, func = function() return a > 0 end },
        }

        for _, v in pairs(t) do
            local f = v.func
            captured_f = f
            local success, result = pcall(f)
            outside = result
            if success then
                success_result = result
            else
                failure_result = result
            end
        end
        "#,
        );

        let captured_f = ws.expr_ty("captured_f");
        let outside = ws.expr_ty("outside");
        let success_result = ws.expr_ty("success_result");
        let failure_result = ws.expr_ty("failure_result");
        assert_eq!(ws.humanize_type(captured_f), "fun() -> boolean");
        assert_eq!(ws.humanize_type(outside), "(boolean|string)");
        assert_eq!(ws.humanize_type(success_result), "boolean");
        assert_eq!(ws.humanize_type(failure_result), "string");
    }

    #[test]
    fn test_return_overload_unions_callable_union_members_with_same_domain() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@alias FnA fun(x: integer): integer
        ---@alias FnB fun(x: integer): boolean

        ---@type FnA | FnB
        local run

        _, a = pcall(run, 1)
        "#,
        );

        assert_eq!(
            ws.expr_ty("a"),
            LuaType::from_vec(vec![LuaType::Integer, LuaType::Boolean, LuaType::String])
        );
    }

    #[test]
    fn test_return_overload_callable_union_is_not_order_dependent() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@alias FnA fun(x: integer): integer
        ---@alias FnB fun(x: integer): boolean

        ---@type FnB | FnA
        local run

        _, a = pcall(run, 1)
        "#,
        );

        assert_eq!(
            ws.expr_ty("a"),
            LuaType::from_vec(vec![LuaType::Integer, LuaType::Boolean, LuaType::String])
        );
    }

    #[test]
    fn test_return_overload_unions_callable_intersection_members_with_same_domain() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@alias FnA fun(x: integer): integer
        ---@alias FnB fun(x: integer): boolean

        ---@type FnA & FnB
        local run

        _, a = pcall(run, 1)
        "#,
        );

        assert_eq!(
            ws.expr_ty("a"),
            LuaType::from_vec(vec![LuaType::Integer, LuaType::Boolean, LuaType::String])
        );
    }

    #[test]
    fn test_return_overload_narrows_callable_union_member_by_arg_shape() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@alias FnA fun(x: integer): integer
        ---@alias FnB fun(x: string): integer

        ---@type FnA | FnB
        local run

        _, a = pcall(run, 1)
        "#,
        );

        assert_eq!(ws.expr_ty("a"), ws.ty("integer|string"));
    }

    #[test]
    fn test_return_overload_narrows_callable_intersection_member_by_arg_shape() {
        let mut ws = VirtualWorkspace::new_with_init_std_lib();

        ws.def(
            r#"
        ---@alias FnA fun(x: integer): integer
        ---@alias FnB fun(x: string): boolean

        ---@type FnA & FnB
        local run

        _, a = pcall(run, 1)
        "#,
        );

        assert_eq!(ws.expr_ty("a"), ws.ty("integer|string"));
    }
}