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_version::LuaVersion, operations::Exec, 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.lua_version().is_some_and(|v| v == &LuaVersion::Luau) {
53        utils::luau_analyze::run(
54            &config,
55            vec![target_path],
56            lint_args.args.unwrap_or_default(),
57        )
58        .await
59    } else {
60        let check_args: Vec<String> = match lint_args.args {
61            Some(args) => args,
62            None if lint_args.no_ignore => Vec::new(),
63            None => {
64                let ignored_files = workspace.iter().flat_map(|workspace| {
65                    top_level_ignored_files(workspace)
66                        .into_iter()
67                        .map(|file| file.to_slash_lossy().to_string())
68                });
69                std::iter::once("--exclude-files".into())
70                    .chain(ignored_files)
71                    .collect_vec()
72            }
73        };
74
75        Exec::new("luacheck", None, &config)
76            .arg(target_path)
77            .args(check_args)
78            .disable_loader(true)
79            .exec()
80            .await?;
81
82        Ok(())
83    }
84}