Skip to content

Commit

Permalink
feat(check): improvements to default behavior (#59)
Browse files Browse the repository at this point in the history
* feat(check): improvements to default behavior

* revise: emit_diagnostics now takes a generic Iter
  • Loading branch information
a-frantz authored Jan 23, 2025
1 parent b128600 commit 5cde395
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
34 changes: 18 additions & 16 deletions src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ pub struct Common {
#[clap(long)]
pub deny_notes: bool,

/// Only display diagnostics for local files.
///
/// This is useful when you want to ignore diagnostics for remote imports.
/// If specified with a remote file, an error will be raised.
#[arg(long)]
pub local_only: bool,

/// Supress diagnostics from imported documents.
///
/// This will only display diagnostics for the document specified by `file`.
/// If specified with a directory, an error will be raised.
#[arg(long)]
pub single_document: bool,

/// Show diagnostics for remote documents.
///
/// By default, when checking a local document remote diagnostics are
/// suppressed. This flag will show diagnostics for remote documents.
/// This flag has no effect when checking a remote document.
#[arg(long)]
pub show_remote_diagnostics: bool,

/// Run the `shellcheck` program on command sections.
///
/// Requires linting to be enabled. This feature is experimental.
Expand Down Expand Up @@ -100,12 +101,7 @@ pub async fn check(args: CheckArgs) -> anyhow::Result<()> {
let shellcheck = args.common.shellcheck;

let file = args.common.file;
if args.common.local_only && Url::parse(&file).is_ok() {
bail!(
"`--local-only` was specified, but `{file}` is a remote URL",
file = file
);
}

if args.common.single_document
&& fs::metadata(&file)
.with_context(|| format!("failed to read metadata for file `{file}`"))
Expand All @@ -118,13 +114,17 @@ pub async fn check(args: CheckArgs) -> anyhow::Result<()> {
);
}

let remote_file = Url::parse(&file).is_ok();

let results = analyze(&file, exceptions, lint, shellcheck).await?;

let cwd = std::env::current_dir().ok();
let mut error_count = 0;
let mut warning_count = 0;
let mut note_count = 0;
for result in &results {
let mut suppress = false;

// Attempt to strip the CWD from the result path
let uri = result.document().uri();
if args.common.single_document && !uri.as_str().contains(&file) {
Expand All @@ -148,8 +148,8 @@ pub async fn check(args: CheckArgs) -> anyhow::Result<()> {
.to_string_lossy()
.to_string(),
_ => {
if args.common.local_only {
continue;
if !remote_file && !args.common.show_remote_diagnostics {
suppress = true;
}
uri.to_string()
}
Expand All @@ -162,7 +162,9 @@ pub async fn check(args: CheckArgs) -> anyhow::Result<()> {

if !diagnostics.is_empty() {
emit_diagnostics(
diagnostics,
diagnostics
.iter()
.filter(|d| !suppress || d.severity() == Severity::Error),
&uri,
&result.document().node().syntax().text().to_string(),
args.common.report_mode,
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ fn get_display_config(report_mode: Mode, no_color: bool) -> (Config, StandardStr
}

/// Emits the given diagnostics to the terminal.
fn emit_diagnostics(
diagnostics: &[Diagnostic],
fn emit_diagnostics<'a>(
diagnostics: impl IntoIterator<Item = &'a Diagnostic>,
file_name: &str,
source: &str,
report_mode: Mode,
Expand Down

0 comments on commit 5cde395

Please sign in to comment.