Something went wrong. Try again.
Lints and suggestions for the Nix programming language forked from oppi.li/statix
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362use std::{ default::Default, fmt, fs, path::{Path, PathBuf}, str::FromStr,};
use crate::{LintMap, dirs, err::ConfigErr, utils};
use clap::Parser;use lib::LINTS;use serde::{Deserialize, Serialize};use vfs::ReadOnlyVfs;
#[derive(Parser, Debug)]#[clap(author, about)]pub struct Opts { #[clap(subcommand)] pub cmd: SubCommand,}
#[derive(Parser, Debug)]pub enum SubCommand { /// Lints and suggestions for the nix programming language Check(Check), /// Find and fix issues raised by statix-check Fix(Fix), /// Fix exactly one issue at provided position Single(Single), /// Print detailed explanation for a lint warning Explain(Explain), /// Dump a sample config to stdout Dump(Dump), /// List all available lints List(List),}
#[derive(Parser, Debug)]pub struct Check { /// File or directory to run check on #[clap(default_value = ".")] target: PathBuf,
/// Globs of file patterns to skip #[clap(short, long)] ignore: Vec<String>,
/// Don't respect .gitignore files #[clap(short, long)] unrestricted: bool,
/// Output format. /// Supported values: stderr, errfmt, json #[clap(short = 'o', long, default_value_t)] pub format: OutFormat,
/// Path to statix.toml or its parent directory #[clap(short = 'c', long = "config", default_value = ".")] pub conf_path: PathBuf,
/// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout #[clap(short, long = "stdin")] pub streaming: bool,}
impl Check { pub fn vfs(&self, extra_ignores: &[String]) -> Result<ReadOnlyVfs, ConfigErr> { if self.streaming { use std::io::{self, BufRead}; let src = io::stdin() .lock() .lines() .map(|l| l.unwrap()) .collect::<Vec<String>>() .join("\n"); Ok(ReadOnlyVfs::singleton("<stdin>", src.as_bytes())) } else { let all_ignores = [self.ignore.as_slice(), extra_ignores].concat(); dirs::check_path_exists(&self.target)?; let files = dirs::walk_nix_files(&self.target, &all_ignores, self.unrestricted)?; Ok(vfs(&files.collect::<Vec<_>>())) } }}
#[derive(Parser, Debug)]pub struct Fix { /// File or directory to run fix on #[clap(default_value = ".")] target: PathBuf,
/// Globs of file patterns to skip #[clap(short, long)] ignore: Vec<String>,
/// Don't respect .gitignore files #[clap(short, long)] unrestricted: bool,
/// Do not fix files in place, display a diff instead #[clap(short, long = "dry-run")] pub diff_only: bool,
/// Path to statix.toml or its parent directory #[clap(short = 'c', long = "config", default_value = ".")] pub conf_path: PathBuf,
/// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout #[clap(short, long = "stdin")] pub streaming: bool,}
pub enum FixOut { Diff, Stream, Write,}
impl Fix { pub fn vfs(&self, extra_ignores: &[String]) -> Result<ReadOnlyVfs, ConfigErr> { if self.streaming { use std::io::{self, BufRead}; let src = io::stdin() .lock() .lines() .map(|l| l.unwrap()) .collect::<Vec<String>>() .join("\n"); Ok(ReadOnlyVfs::singleton("<stdin>", src.as_bytes())) } else { let all_ignores = [self.ignore.as_slice(), extra_ignores].concat(); dirs::check_path_exists(&self.target)?; let files = dirs::walk_nix_files(&self.target, &all_ignores, self.unrestricted)?; Ok(vfs(&files.collect::<Vec<_>>())) } }
// i need this ugly helper because clap's data model // does not reflect what i have in mind #[must_use] pub fn out(&self) -> FixOut { if self.diff_only { FixOut::Diff } else if self.streaming { FixOut::Stream } else { FixOut::Write } }}
#[derive(Parser, Debug)]pub struct Single { /// File to run single-fix on pub target: Option<PathBuf>,
/// Position to attempt a fix at #[clap(short, long)] pub position: Position,
/// Do not fix files in place, display a diff instead #[clap(short, long = "dry-run")] pub diff_only: bool,
/// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout #[clap(short, long = "stdin")] pub streaming: bool,
/// Path to statix.toml or its parent directory #[clap(short = 'c', long = "config", default_value = ".")] pub conf_path: PathBuf,}
impl Single { pub fn vfs(&self) -> Result<ReadOnlyVfs, ConfigErr> { if self.streaming { use std::io::{self, BufRead}; let src = io::stdin() .lock() .lines() .map(|l| l.unwrap()) .collect::<Vec<String>>() .join("\n"); Ok(ReadOnlyVfs::singleton("<stdin>", src.as_bytes())) } else { let src = std::fs::read_to_string(self.target.as_ref().unwrap()) .map_err(ConfigErr::InvalidPath)?; Ok(ReadOnlyVfs::singleton("<stdin>", src.as_bytes())) } } #[must_use] pub fn out(&self) -> FixOut { if self.diff_only { FixOut::Diff } else if self.streaming { FixOut::Stream } else { FixOut::Write } }}
#[derive(Parser, Debug)]pub struct Explain { /// Warning code to explain pub target: WarningCode,}
#[derive(Parser, Debug)]pub struct Dump {}
#[derive(Parser, Debug)]pub struct List {}
#[derive(Debug, Copy, Clone, Default)]pub enum OutFormat { Json, Errfmt, #[default] StdErr,}
impl fmt::Display for OutFormat { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "{}", match self { Self::Json => "json", Self::Errfmt => "errfmt", Self::StdErr => "stderr", } ) }}
impl FromStr for OutFormat { type Err = &'static str;
fn from_str(value: &str) -> Result<Self, Self::Err> { match value.to_ascii_lowercase().as_str() { "json" => Ok(Self::Json), "errfmt" => Ok(Self::Errfmt), "stderr" => Ok(Self::StdErr), _ => Err("unknown output format, try: json, errfmt"), } }}
#[derive(Serialize, Deserialize, Debug, Default)]pub struct ConfFile { #[serde(default = "Vec::new")] disabled: Vec<String>,
#[serde(default = "Vec::new")] pub ignore: Vec<String>,}
impl ConfFile { pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, ConfigErr> { let path = path.as_ref(); let config_file = fs::read_to_string(path).map_err(ConfigErr::InvalidPath)?; toml::de::from_str(&config_file).map_err(ConfigErr::ConfFileParse) } pub fn discover<P: AsRef<Path>>(path: P) -> Result<Self, ConfigErr> { let cannonical_path = fs::canonicalize(path.as_ref()).map_err(ConfigErr::InvalidPath)?; for p in cannonical_path.ancestors() { let statix_toml_path = if p.is_dir() { p.join("statix.toml") } else { p.to_path_buf() }; if statix_toml_path.exists() { return Self::from_path(statix_toml_path); } } Ok(Self::default()) } #[must_use] pub fn dump(&self) -> String { let ideal_config = { let disabled = vec![]; let ignore = vec![".direnv".into()]; Self { disabled, ignore } }; toml::ser::to_string_pretty(&ideal_config).unwrap() } #[must_use] pub fn lints(&self) -> LintMap { utils::lint_map_of( (*LINTS) .iter() .filter(|l| !self.disabled.iter().any(|check| check == l.name())) .copied() .collect::<Vec<_>>() .as_slice(), ) }}
#[derive(Debug, Clone, Copy)]pub struct Position { pub line: usize, pub col: usize,}
impl FromStr for Position { type Err = ConfigErr;
fn from_str(src: &str) -> Result<Self, Self::Err> { let parts = src.split(','); match parts.collect::<Vec<_>>().as_slice() { [line, col] => { let do_parse = |val: &str| { val.parse::<usize>() .map_err(|_| ConfigErr::InvalidPosition(src.to_owned())) }; let l = do_parse(line)?; let c = do_parse(col)?; Ok(Self { line: l, col: c }) } _ => Err(ConfigErr::InvalidPosition(src.to_owned())), } }}
#[derive(Debug, Clone, Copy)]pub struct WarningCode(pub u32);
impl FromStr for WarningCode { type Err = ConfigErr;
fn from_str(src: &str) -> Result<Self, Self::Err> { let mut char_stream = src.chars(); let severity = char_stream .next() .ok_or_else(|| ConfigErr::InvalidWarningCode(src.to_owned()))? .to_ascii_lowercase(); match severity { 'w' => char_stream .collect::<String>() .parse::<u32>() .map(Self) .map_err(|_| ConfigErr::InvalidWarningCode(src.to_owned())), _ => Ok(Self(0)), } }}
fn vfs(files: &[PathBuf]) -> vfs::ReadOnlyVfs { let mut vfs = ReadOnlyVfs::default(); for file in files { if let Ok(data) = fs::read_to_string(file) { let _id = vfs.alloc_file_id(file); vfs.set_file_contents(file, data.as_bytes()); } else { println!("`{}` contains non-utf8 content", file.display()); } } vfs}