tishlang_lint 2.8.0

AST-based linter for Tish
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
//! AST-based lints for Tish. Rule IDs are stable for CI and editors.

use std::collections::HashSet;

use tishlang_ast::{Expr, ObjectProp, Program, Statement};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Severity {
    Warning,
    Error,
}

#[derive(Debug, Clone)]
pub struct LintDiagnostic {
    pub code: &'static str,
    pub message: String,
    /// 1-based line
    pub line: u32,
    /// 1-based column (UTF-16 offset preferred for LSP; we use byte col as approx)
    pub col: u32,
    pub severity: Severity,
}

/// Run all default rules on parsed program.
pub fn lint_program(program: &Program) -> Vec<LintDiagnostic> {
    let mut out = Vec::new();
    check_unreachable(&program.statements, &mut out);
    for s in &program.statements {
        lint_stmt(s, &mut out);
    }
    out
}

/// `tish-unreachable-code` (no-unreachable): in a statement sequence, the first statement after an
/// unconditional `return`/`throw`/`break`/`continue` sibling can never run. Flags only that first
/// statement (not the whole tail — less noise) and skips hoisted `fn` declarations, which are valid
/// after a terminator. Only a DIRECT-sibling terminator triggers it, so a conditional
/// `if (c) return` followed by more statements is correctly NOT flagged.
fn check_unreachable(stmts: &[Statement], out: &mut Vec<LintDiagnostic>) {
    let mut terminated = false;
    for st in stmts {
        if terminated {
            if matches!(st, Statement::FunDecl { .. }) {
                continue; // function declarations hoist — reachable regardless of position
            }
            let (line, col) = stmt_span(st);
            out.push(LintDiagnostic {
                code: "tish-unreachable-code",
                message: "Unreachable code after return/throw/break/continue.".into(),
                line,
                col,
                severity: Severity::Warning,
            });
            return;
        }
        if matches!(
            st,
            Statement::Return { .. }
                | Statement::Throw { .. }
                | Statement::Break { .. }
                | Statement::Continue { .. }
        ) {
            terminated = true;
        }
    }
}

/// Lint source: parse then lint. Parse errors are not reported here.
pub fn lint_source(source: &str) -> Result<Vec<LintDiagnostic>, String> {
    let program = tishlang_parser::parse(source)?;
    Ok(lint_program(&program))
}

fn lint_stmt(s: &Statement, out: &mut Vec<LintDiagnostic>) {
    match s {
        Statement::Try {
            body,
            catch_param,
            catch_body,
            finally_body,
            ..
        } => {
            lint_stmt(body, out);
            if let (Some(_), Some(cb)) = (catch_param, catch_body) {
                if is_empty_block_or_stmt(cb) {
                    let span = stmt_span(cb);
                    out.push(LintDiagnostic {
                        code: "tish-empty-catch",
                        message: "Empty catch block; handle or rethrow the error.".into(),
                        line: span.0,
                        col: span.1,
                        severity: Severity::Warning,
                    });
                }
                lint_stmt(cb, out);
            }
            if let Some(fb) = finally_body {
                lint_stmt(fb, out);
            }
        }
        // Block and the transparent comma-declarator group (#141: Multi was previously skipped).
        Statement::Block { statements, .. } | Statement::Multi { statements, .. } => {
            check_unreachable(statements, out);
            for st in statements {
                lint_stmt(st, out);
            }
        }
        // #140: control-flow CONDITION expressions are linted too, not just bodies.
        Statement::If {
            cond,
            then_branch,
            else_branch,
            ..
        } => {
            lint_expr(cond, out);
            lint_stmt(then_branch, out);
            if let Some(e) = else_branch {
                lint_stmt(e, out);
            }
        }
        Statement::While { cond, body, .. } => {
            lint_expr(cond, out);
            lint_stmt(body, out);
        }
        Statement::ForOf { iterable, body, .. } => {
            lint_expr(iterable, out);
            lint_stmt(body, out);
        }
        Statement::For {
            init,
            cond,
            update,
            body,
            ..
        } => {
            if let Some(i) = init {
                lint_stmt(i, out);
            }
            if let Some(c) = cond {
                lint_expr(c, out);
            }
            if let Some(u) = update {
                lint_expr(u, out);
            }
            lint_stmt(body, out);
        }
        Statement::FunDecl { body, .. } => lint_stmt(body, out),
        Statement::Switch {
            expr,
            cases,
            default_body,
            ..
        } => {
            lint_expr(expr, out);
            for (case_expr, stmts) in cases {
                if let Some(ce) = case_expr {
                    lint_expr(ce, out);
                }
                check_unreachable(stmts, out);
                for st in stmts {
                    lint_stmt(st, out);
                }
            }
            if let Some(def) = default_body {
                check_unreachable(def, out);
                for st in def {
                    lint_stmt(st, out);
                }
            }
        }
        Statement::DoWhile { body, cond, .. } => {
            lint_stmt(body, out);
            lint_expr(cond, out);
        }
        Statement::Export { declaration, .. } => match declaration.as_ref() {
            tishlang_ast::ExportDeclaration::Named(inner) => lint_stmt(inner, out),
            // Walk `export default <expr>` too — its subtree was previously never linted, so e.g.
            // `export default { a: 1, a: 2 }` produced no tish-duplicate-key warning (#151).
            tishlang_ast::ExportDeclaration::Default(expr) => lint_expr(expr, out),
        },
        Statement::ExprStmt { expr, .. } => lint_expr(expr, out),
        Statement::VarDecl { init: Some(e), .. } => lint_expr(e, out),
        Statement::VarDeclDestructure { init, .. } => lint_expr(init, out),
        Statement::Return { value: Some(e), .. } => lint_expr(e, out),
        Statement::Throw { value, .. } => lint_expr(value, out),
        _ => {}
    }
}

fn is_empty_block_or_stmt(s: &Statement) -> bool {
    match s {
        Statement::Block { statements, .. } => statements.is_empty(),
        Statement::ExprStmt { .. } => false,
        _ => false,
    }
}

fn stmt_span(s: &Statement) -> (u32, u32) {
    // Use the AST's uniform accessor so every statement kind reports its real position (the old
    // local match only handled Block/Try and defaulted everything else to (1,1)).
    let sp = s.span();
    (sp.start.0 as u32, sp.start.1 as u32)
}

fn lint_expr(e: &Expr, out: &mut Vec<LintDiagnostic>) {
    match e {
        Expr::Object { props, .. } => {
            let mut seen: HashSet<&str> = HashSet::new();
            for p in props {
                if let ObjectProp::KeyValue(k, v, kspan) = p {
                    if !seen.insert(k.as_ref()) {
                        // Point at the duplicated KEY, not the enclosing `{` — so distinct dupes get
                        // distinct positions (#143).
                        out.push(LintDiagnostic {
                            code: "tish-duplicate-key",
                            message: format!("Duplicate object key `{}`", k),
                            line: kspan.start.0 as u32,
                            col: kspan.start.1 as u32,
                            severity: Severity::Warning,
                        });
                    }
                    lint_expr(v, out);
                } else if let ObjectProp::Spread(ex) = p {
                    lint_expr(ex, out);
                }
            }
        }
        Expr::Binary { left, right, .. } => {
            lint_expr(left, out);
            lint_expr(right, out);
        }
        Expr::Unary { operand, .. } => lint_expr(operand, out),
        Expr::Call { callee, args, .. } => {
            lint_expr(callee, out);
            for a in args {
                match a {
                    tishlang_ast::CallArg::Expr(x) => lint_expr(x, out),
                    tishlang_ast::CallArg::Spread(x) => lint_expr(x, out),
                }
            }
        }
        Expr::New { callee, args, .. } => {
            lint_expr(callee, out);
            for a in args {
                match a {
                    tishlang_ast::CallArg::Expr(x) => lint_expr(x, out),
                    tishlang_ast::CallArg::Spread(x) => lint_expr(x, out),
                }
            }
        }
        Expr::Member { object, .. } => {
            lint_expr(object, out);
        }
        Expr::Index { object, index, .. } => {
            lint_expr(object, out);
            lint_expr(index, out);
        }
        Expr::Conditional {
            cond,
            then_branch,
            else_branch,
            ..
        } => {
            lint_expr(cond, out);
            lint_expr(then_branch, out);
            lint_expr(else_branch, out);
        }
        Expr::NullishCoalesce { left, right, .. } => {
            lint_expr(left, out);
            lint_expr(right, out);
        }
        Expr::Array { elements, .. } => {
            for el in elements {
                match el {
                    tishlang_ast::ArrayElement::Expr(x) => lint_expr(x, out),
                    tishlang_ast::ArrayElement::Spread(x) => lint_expr(x, out),
                }
            }
        }
        Expr::Assign { value, .. }
        | Expr::CompoundAssign { value, .. }
        | Expr::LogicalAssign { value, .. } => lint_expr(value, out),
        Expr::MemberAssign { object, value, .. } => {
            lint_expr(object, out);
            lint_expr(value, out);
        }
        Expr::IndexAssign {
            object,
            index,
            value,
            ..
        } => {
            lint_expr(object, out);
            lint_expr(index, out);
            lint_expr(value, out);
        }
        Expr::ArrowFunction { body, .. } => match body {
            tishlang_ast::ArrowBody::Expr(x) => lint_expr(x, out),
            tishlang_ast::ArrowBody::Block(b) => lint_stmt(b, out),
        },
        Expr::TemplateLiteral { exprs, .. } => {
            for x in exprs {
                lint_expr(x, out);
            }
        }
        Expr::Await { operand, .. } => lint_expr(operand, out),
        Expr::TypeOf { operand, .. } => lint_expr(operand, out),
        // #142: descend into a delete target (`delete obj[expr]` / `delete (expr).prop`).
        Expr::Delete { target, .. } => lint_expr(target, out),
        Expr::JsxElement {
            props, children, ..
        } => {
            for pr in props {
                match pr {
                    tishlang_ast::JsxProp::Attr { value, .. } => {
                        if let tishlang_ast::JsxAttrValue::Expr(x) = value {
                            lint_expr(x, out);
                        }
                    }
                    tishlang_ast::JsxProp::Spread(x) => lint_expr(x, out),
                }
            }
            for ch in children {
                if let tishlang_ast::JsxChild::Expr(x) = ch {
                    lint_expr(x, out);
                }
            }
        }
        Expr::JsxFragment { children, .. } => {
            for ch in children {
                if let tishlang_ast::JsxChild::Expr(x) = ch {
                    lint_expr(x, out);
                }
            }
        }
        _ => {}
    }
}

/// Human-readable catalog for documentation.
pub const RULES: &[(&str, &str)] = &[
    (
        "tish-empty-catch",
        "Warns on catch blocks with no statements (likely mistake).",
    ),
    (
        "tish-duplicate-key",
        "Warns when an object literal repeats the same key.",
    ),
];

#[cfg(test)]
mod tests {
    use super::*;

    /// Count `tish-duplicate-key` diagnostics the linter emits for `src` — a probe for whether the
    /// linter actually descends into a given position (the dup-key rule fires on any object literal
    /// the walk reaches).
    fn dup_keys(src: &str) -> usize {
        lint_source(src)
            .expect("parse")
            .iter()
            .filter(|d| d.code == "tish-duplicate-key")
            .count()
    }

    #[test]
    fn baseline_dup_key_in_plain_var_is_linted() {
        assert_eq!(dup_keys("let x = { a: 1, a: 2 }\n"), 1);
    }

    // #141: comma-separated declarators lower to Statement::Multi, which the linter skipped.
    #[test]
    fn lints_inside_comma_declarators() {
        assert!(
            dup_keys("let x = { a: 1, a: 2 }, y = 3\n") >= 1,
            "dup key in a comma-declarator (Statement::Multi) must be linted"
        );
    }

    // #140: control-flow CONDITION expressions were never linted (only bodies were walked).
    #[test]
    fn lints_inside_control_flow_conditions() {
        assert!(dup_keys("if ({ a: 1, a: 2 }) {}\n") >= 1, "if condition");
        assert!(dup_keys("while ({ a: 1, a: 2 }) { break }\n") >= 1, "while condition");
        assert!(
            dup_keys("for (let i = 0; ({ a: 1, a: 2 }); i = i + 1) { break }\n") >= 1,
            "for condition"
        );
        assert!(dup_keys("do { break } while ({ a: 1, a: 2 })\n") >= 1, "do-while condition");
        assert!(
            dup_keys("switch ({ a: 1, a: 2 }) { case 1: break }\n") >= 1,
            "switch discriminant"
        );
    }

    // #142: the linter never descended into a delete target.
    #[test]
    fn lints_inside_delete_target() {
        assert!(dup_keys("delete ({ a: 1, a: 2 }).a\n") >= 1, "delete target");
    }

    // #151: the `export default <expr>` subtree was never walked (only `export <named>` was).
    #[test]
    fn lints_inside_export_default() {
        assert!(
            dup_keys("export default { a: 1, a: 2 }\n") >= 1,
            "dup key inside `export default` must be linted"
        );
    }

    // #152: no-unreachable.
    fn unreachable(src: &str) -> usize {
        lint_source(src)
            .expect("parse")
            .iter()
            .filter(|d| d.code == "tish-unreachable-code")
            .count()
    }

    #[test]
    fn flags_code_after_terminator() {
        assert_eq!(unreachable("fn f() {\n  return 1\n  let x = 2\n}\n"), 1, "after return");
        assert_eq!(unreachable("fn g() {\n  throw 1\n  g()\n}\n"), 1, "after throw");
        assert_eq!(
            unreachable("fn h() {\n  while (true) {\n    break\n    h()\n  }\n}\n"),
            1,
            "after break in loop body"
        );
    }

    #[test]
    fn does_not_flag_reachable_or_hoisted() {
        // Conditional return is NOT a direct-sibling terminator → following code is reachable.
        assert_eq!(unreachable("fn f(c) {\n  if (c) { return 1 }\n  let x = 2\n}\n"), 0, "conditional");
        // Function declarations hoist — valid after a return.
        assert_eq!(unreachable("fn g() {\n  return 1\n  fn helper() { return 2 }\n}\n"), 0, "hoisted fn");
        // Switch fall-through (no terminator) is intentional.
        assert_eq!(unreachable("switch (x) {\n  case 1:\n    a()\n  case 2:\n    b()\n}\n"), 0, "fallthrough");
    }
}

#[cfg(test)]
mod dupkey_position_tests {
    use super::*;
    #[test]
    fn dup_key_points_at_the_duplicated_key() {
        // `let x = { a: 1, a: 2 }` — the duplicate `a` is at 1-indexed col 17; the object `{` is col 9.
        let d: Vec<_> = lint_source("let x = { a: 1, a: 2 }\n")
            .expect("parse")
            .into_iter()
            .filter(|d| d.code == "tish-duplicate-key")
            .collect();
        assert_eq!(d.len(), 1, "exactly one dup-key");
        assert_eq!((d[0].line, d[0].col), (1, 17), "#143: must point at the duplicated key, not the `{{`");
    }
}