nu-lint 1.1.0

Linter for Nu shell scripts that helpfully suggests improvements
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
use std::{collections::HashSet, ops::ControlFlow};

use nu_protocol::{
    BlockId, Span, Type, VarId,
    ast::{Block, Expr, Expression, Pipeline, PipelineElement, Traverse},
};

use super::call::CallExt;
use crate::{ast::expression::ExpressionExt, context::LintContext};

const MAX_TYPE_INFERENCE_DEPTH: usize = 100;

pub trait BlockExt {
    /// Checks if block is an empty list. Example: `{ [] }`
    fn is_empty_list_block(&self) -> bool;
    #[must_use]
    /// Checks if block contains a specific span. Example: function body
    /// contains statement span
    fn contains_span(&self, span: Span) -> bool;
    /// All pipeline elements: `{ ls | where size > 1kb }`
    fn all_elements(&self) -> Vec<&PipelineElement>;
    /// Collects all user function call block IDs in block. Returns the
    /// `BlockId` of each called custom command's body.
    fn collect_user_function_call_block_ids(&self, context: &LintContext) -> Vec<BlockId>;
    /// Finds all transitively called functions by `BlockId`. Example: main
    /// calls foo, foo calls bar - returns `BlockId`s of foo and bar
    fn find_transitively_called_functions(
        &self,
        context: &LintContext,
        available_functions: &HashSet<BlockId>,
    ) -> HashSet<BlockId>;
    /// Helper for recursive transitive function search with cycle detection
    fn find_transitively_called_functions_impl(
        &self,
        context: &LintContext,
        available_functions: &HashSet<BlockId>,
        visited: &mut HashSet<BlockId>,
    ) -> HashSet<BlockId>;
    /// Checks if block uses pipeline input variable. Example: `{ $in | length
    /// }`
    fn uses_pipeline_input(&self, context: &LintContext) -> bool;
    /// Checks if block produces output. Example: `{ ls }` produces output, `{
    /// print "x" }` doesn't
    fn produces_output(&self) -> bool;
    /// Finds pipeline input-like variables (includes `$in` and closure
    /// parameters) and their spans. Example: `{ $in | length }` returns
    /// `(var_id, span of $in)`
    fn find_pipeline_input(&self, context: &LintContext) -> Option<(VarId, Span)>;
    /// Finds the actual `$in` variable usage and its span. Example: `{ $in |
    /// length }` returns span of `$in`. Does not match closure parameters.
    fn find_dollar_in_usage(&self) -> Option<Span>;
    /// Infers the output type of a block. Example: `{ ls }` returns "table"
    fn infer_output_type(&self, context: &LintContext) -> Type;
    /// Helper for recursive type inference with depth limit
    fn infer_output_type_with_depth(&self, context: &LintContext, depth: usize) -> Type;
    /// Infers the input type expected by a block. Example: `{ $in | length }`
    /// expects "list"
    fn infer_input_type(&self, context: &LintContext) -> Type;
    /// Extracts variable IDs that are assigned to within a block. Example: `{
    /// $x = 5; $y += 1 }` returns [x, y]
    fn extract_assigned_vars(&self) -> Vec<VarId>;

    /// Finds spans of all usages of a variable in this block.
    /// Example: finding all usages of `$x`
    fn var_usages(&self, var_id: VarId, context: &LintContext) -> Vec<Span>;

    /// Finds spans of expressions matching a predicate. Example: finding all
    /// expressions that contain null checks for a variable
    fn find_expr_spans<F>(&self, context: &LintContext, predicate: F) -> Vec<Span>
    where
        F: Fn(&Expression, &LintContext) -> bool;

    /// Traverse block and all descendants with parent tracking.
    /// Calls the callback for each expression with its immediate parent.
    /// The callback returns `ControlFlow::Continue(())` to recurse into
    /// children, or `ControlFlow::Break(())` to skip this expression's
    /// children.
    fn traverse_with_parent<'a, F>(
        &'a self,
        context: &'a LintContext,
        parent: Option<&'a Expression>,
        callback: &mut F,
    ) where
        F: FnMut(&'a Expression, Option<&'a Expression>) -> ControlFlow<()>;

    /// Recursively detect violations in all pipelines of this block and nested
    /// blocks. This is a common pattern used by many lint rules.
    ///
    /// The `check_pipeline` function is called for each pipeline and should
    /// return violations found in that pipeline. The function automatically
    /// recurses into closures, blocks, and subexpressions.
    fn detect_in_pipelines<T>(
        &self,
        context: &LintContext,
        check_pipeline: impl Fn(&Pipeline, &LintContext) -> Vec<T> + Copy,
    ) -> Vec<T>;

    /// Checks if this block is a pipeline ending with `columns` command and
    /// returns the span of the record expression (everything before `columns`).
    /// Example: `($record | columns)` returns span of `$record`
    fn find_columns_record_span(&self, context: &LintContext) -> Option<Span>;

    /// Checks if the given span is inside a `try` block anywhere in this AST.
    fn is_span_inside_try_block(&self, context: &LintContext, span: Span) -> bool;
}

impl BlockExt for Block {
    fn is_empty_list_block(&self) -> bool {
        self.pipelines
            .first()
            .and_then(|pipeline| pipeline.elements.first())
            .is_some_and(|elem| elem.expr.is_empty_list())
    }

    fn contains_span(&self, span: Span) -> bool {
        if let Some(block_span) = self.span {
            return span.start >= block_span.start && span.end <= block_span.end;
        }
        false
    }

    fn all_elements(&self) -> Vec<&PipelineElement> {
        self.pipelines.iter().flat_map(|p| &p.elements).collect()
    }

    fn collect_user_function_call_block_ids(&self, context: &LintContext) -> Vec<BlockId> {
        let mut block_ids = Vec::new();

        self.flat_map(
            context.working_set,
            &|expr| {
                if let Expr::Call(call) = &expr.expr {
                    let decl = context.working_set.get_decl(call.decl_id);
                    decl.block_id().into_iter().collect()
                } else {
                    vec![]
                }
            },
            &mut block_ids,
        );

        block_ids
    }

    fn find_transitively_called_functions(
        &self,
        context: &LintContext,
        available_functions: &HashSet<BlockId>,
    ) -> HashSet<BlockId> {
        let mut visited = HashSet::new();
        self.find_transitively_called_functions_impl(context, available_functions, &mut visited)
    }

    fn find_transitively_called_functions_impl(
        &self,
        context: &LintContext,
        available_functions: &HashSet<BlockId>,
        visited: &mut HashSet<BlockId>,
    ) -> HashSet<BlockId> {
        let mut result = HashSet::new();

        for callee_block_id in self.collect_user_function_call_block_ids(context) {
            if !available_functions.contains(&callee_block_id) {
                continue;
            }

            if !visited.insert(callee_block_id) {
                log::trace!("Cycle detected in function calls");
                continue;
            }

            result.insert(callee_block_id);

            let callee_block = context.working_set.get_block(callee_block_id);
            let transitive = callee_block.find_transitively_called_functions_impl(
                context,
                available_functions,
                visited,
            );
            result.extend(transitive);
        }

        result
    }

    fn uses_pipeline_input(&self, context: &LintContext) -> bool {
        self.all_elements()
            .iter()
            .any(|elem| elem.expr.uses_pipeline_input(context))
    }

    fn produces_output(&self) -> bool {
        self.pipelines.last().is_some_and(|pipeline| {
            pipeline
                .elements
                .last()
                .is_some_and(|last_element| !matches!(&last_element.expr.expr, Expr::Nothing))
        })
    }

    fn find_pipeline_input(&self, context: &LintContext) -> Option<(VarId, Span)> {
        self.all_elements()
            .iter()
            .find_map(|element| element.expr.find_pipeline_input(context))
    }

    fn find_dollar_in_usage(&self) -> Option<Span> {
        self.all_elements()
            .iter()
            .find_map(|element| element.expr.find_dollar_in_usage())
    }

    fn infer_output_type(&self, context: &LintContext) -> Type {
        self.infer_output_type_with_depth(context, 0)
    }

    fn infer_output_type_with_depth(&self, context: &LintContext, depth: usize) -> Type {
        if depth >= MAX_TYPE_INFERENCE_DEPTH {
            log::warn!(
                "Type inference depth limit ({MAX_TYPE_INFERENCE_DEPTH}) reached, returning Any"
            );
            return Type::Any;
        }

        log::trace!("Inferring output type for block (depth={depth})");

        let Some(pipeline) = self.pipelines.last() else {
            return self.output_type();
        };

        let elements = self.all_elements();
        let block_input_type = elements
            .iter()
            .find_map(|element| element.expr.find_pipeline_input(context))
            .and_then(|(in_var, _)| {
                elements
                    .iter()
                    .find_map(|element| element.expr.infer_input_type(Some(in_var), context))
            })
            .unwrap_or(Type::Any);
        log::trace!("Block inferred input type: {block_input_type:?}");
        let mut current_type = Some(block_input_type);

        for (idx, element) in pipeline.elements.iter().enumerate() {
            log::trace!("Pipeline element {idx}: current_type before = {current_type:?}");

            if let Expr::Call(call) = &element.expr.expr {
                let output = call.get_output_type(context, current_type);
                log::trace!("Pipeline element {idx} (Call): output type = {output:?}");
                current_type = Some(output);
                continue;
            }

            let inferred = element.expr.infer_output_type(context);
            log::trace!("Pipeline element {idx} (Expression): inferred type = {inferred:?}");
            if inferred.is_some() {
                current_type = inferred;
            }
        }

        let final_type = current_type.unwrap_or_else(|| self.output_type());
        log::trace!("Block final output type: {final_type:?}");
        final_type
    }

    fn infer_input_type(&self, context: &LintContext) -> Type {
        let Some((in_var, _)) = self.find_pipeline_input(context) else {
            return Type::Any;
        };

        self.all_elements()
            .iter()
            .find_map(|element| element.expr.infer_input_type(Some(in_var), context))
            .unwrap_or(Type::Any)
    }

    fn extract_assigned_vars(&self) -> Vec<VarId> {
        self.all_elements()
            .iter()
            .filter_map(|elem| elem.expr.extract_assigned_variable())
            .collect()
    }

    fn var_usages(&self, var_id: VarId, context: &LintContext) -> Vec<Span> {
        let mut results = Vec::new();
        self.flat_map(
            context.working_set,
            &|expr: &Expression| {
                // Only match Expr::Var directly - Traverse will visit it inside
                // FullCellPath too, so we avoid duplicates by not matching FullCellPath
                if let Expr::Var(id) = &expr.expr
                    && *id == var_id
                {
                    vec![expr.span]
                } else {
                    vec![]
                }
            },
            &mut results,
        );
        results
    }

    fn find_expr_spans<F>(&self, context: &LintContext, predicate: F) -> Vec<Span>
    where
        F: Fn(&Expression, &LintContext) -> bool,
    {
        use nu_protocol::ast::Expression;

        let mut matching_spans = Vec::new();
        self.flat_map(
            context.working_set,
            &|expr: &Expression| {
                if predicate(expr, context) {
                    vec![expr.span]
                } else {
                    vec![]
                }
            },
            &mut matching_spans,
        );
        matching_spans
    }

    fn traverse_with_parent<'a, F>(
        &'a self,
        context: &'a LintContext,
        parent: Option<&'a Expression>,
        callback: &mut F,
    ) where
        F: FnMut(&'a Expression, Option<&'a Expression>) -> ControlFlow<()>,
    {
        use crate::ast::expression::ExpressionExt;

        // For each pipeline element, parent is the block/closure/subexpression
        // expression
        for pipeline in &self.pipelines {
            for element in &pipeline.elements {
                element.expr.traverse_with_parent(context, parent, callback);
            }
        }
    }

    fn detect_in_pipelines<T>(
        &self,
        context: &LintContext,
        check_pipeline: impl Fn(&Pipeline, &LintContext) -> Vec<T> + Copy,
    ) -> Vec<T> {
        let mut results: Vec<T> = self
            .pipelines
            .iter()
            .flat_map(|p| check_pipeline(p, context))
            .collect();

        // Collect block_ids from nested expressions (closures, blocks,
        // subexpressions) without recursing into their contents.
        let mut child_block_ids = Vec::new();
        let mut collect_block_id =
            |expr: &Expression, _parent: Option<&Expression>| match expr.extract_block_id() {
                Some(block_id) => {
                    child_block_ids.push(block_id);
                    ControlFlow::Break(())
                }
                None => ControlFlow::Continue(()),
            };
        for pipeline in &self.pipelines {
            for element in &pipeline.elements {
                element
                    .expr
                    .traverse_with_parent(context, None, &mut collect_block_id);
            }
        }

        for block_id in child_block_ids {
            let block = context.working_set.get_block(block_id);
            results.extend(block.detect_in_pipelines(context, check_pipeline));
        }

        results
    }

    fn find_columns_record_span(&self, context: &LintContext) -> Option<Span> {
        let pipeline = self.pipelines.first()?;

        if pipeline.elements.len() < 2 {
            return None;
        }

        let last_elem = pipeline.elements.last()?;
        let Expr::Call(call) = &last_elem.expr.expr else {
            return None;
        };

        let decl = context.working_set.get_decl(call.decl_id);
        if decl.name() != "columns" {
            return None;
        }

        let elements_before_columns = &pipeline.elements[..pipeline.elements.len() - 1];
        if elements_before_columns.is_empty() {
            return None;
        }

        let start = elements_before_columns.first()?.expr.span.start;
        let end = elements_before_columns.last()?.expr.span.end;
        Some(Span::new(start, end))
    }

    fn is_span_inside_try_block(&self, context: &LintContext, span: Span) -> bool {
        use nu_protocol::ast::FindMapResult;

        self.find_map(context.working_set, &|expr| {
            let Expr::Call(call) = &expr.expr else {
                return FindMapResult::Continue;
            };
            if call.is_call_to_command("try", context)
                && expr.span.start <= span.start
                && expr.span.end >= span.end
            {
                return FindMapResult::Found(());
            }
            FindMapResult::Continue
        })
        .is_some()
    }
}