Skip to main content

lux_cli/
lint.rs

1use std::path::PathBuf;
2
3use clap::Args;
4use itertools::Itertools;
5use lux_lib::{config::Config, lua, operations, workspace::Workspace};
6use path_slash::PathBufExt;
7
8use crate::utils;
9use crate::utils::path::{classify_path, PathTarget};
10use crate::workspace::top_level_ignored_files;
11use miette::{IntoDiagnostic, Result};
12
13#[derive(Args)]
14pub struct Lint {
15    /// Path to a workspace, directory, or Lua file to lint. Defaults to the current workspace.
16    #[arg(long)]
17    path: Option<PathBuf>,
18    /// Arguments to pass to the luacheck command.{n}
19    /// If you pass arguments to luacheck, Lux will not pass any default arguments.
20    args: Option<Vec<String>>,
21    /// By default, Lux will add top-level ignored files and directories{n}
22    /// (like those in .gitignore) to luacheck's exclude files.{n}
23    /// This flag disables that behaviour.{n}
24    #[arg(long)]
25    no_ignore: bool,
26}
27
28pub async fn lint(lint_args: Lint, config: Config) -> Result<()> {
29    let target = match lint_args.path.as_deref() {
30        Some(path) => classify_path(path)?,
31        None => match Workspace::current()? {
32            Some(workspace) => PathTarget::Workspace(Box::new(workspace)),
33            None => PathTarget::Directory(std::env::current_dir().into_diagnostic()?),
34        },
35    };
36
37    let (target_path, workspace) = match target {
38        PathTarget::Workspace(ws) => (ws.root().to_slash_lossy().to_string(), Some(*ws)),
39        PathTarget::Directory(dir) => {
40            let ws = Workspace::from(&dir)?;
41            (dir.to_slash_lossy().to_string(), ws)
42        }
43        PathTarget::File(file) => {
44            let ws = match file.parent() {
45                Some(parent) => Workspace::from(parent)?,
46                None => None,
47            };
48            (file.to_slash_lossy().to_string(), ws)
49        }
50    };
51
52    if config
53        .lua_version()
54        .is_some_and(|v| v == &lua::Version::Luau)
55    {
56        utils::luau_analyze::run(
57            &config,
58            vec![target_path],
59            lint_args.args.unwrap_or_default(),
60        )
61        .await
62    } else {
63        let check_args: Vec<String> = match lint_args.args {
64            Some(args) => args,
65            None if lint_args.no_ignore => Vec::new(),
66            None => {
67                let ignored_files = workspace.iter().flat_map(|workspace| {
68                    top_level_ignored_files(workspace)
69                        .into_iter()
70                        .map(|file| file.to_slash_lossy().to_string())
71                });
72                std::iter::once("--exclude-files".into())
73                    .chain(ignored_files)
74                    .collect_vec()
75            }
76        };
77
78        operations::Exec::new("luacheck", None, &config)
79            .arg(target_path)
80            .args(check_args)
81            .disable_loader(true)
82            .exec()
83            .await?;
84
85        Ok(())
86    }
87}