Skip to content

Commit

Permalink
feat: optionally collect parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed Jan 3, 2025
1 parent 894873f commit ec6142e
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 13 deletions.
4 changes: 4 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ bench = false
name = "fix_return_code"
harness = false

[[test]]
name = "fix_parse_errors"
harness = false

[[test]]
name = "config_not_found"
harness = false
Expand Down
3 changes: 3 additions & 0 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub(crate) struct Cli {
/// Path to a configuration file.
#[arg(long, global = true)]
pub config: Option<String>,
/// Show parse errors.
#[arg(long, global = true, default_value = "false")]
pub parsing_errors: bool,
}

#[derive(Debug, Subcommand)]
Expand Down
11 changes: 8 additions & 3 deletions crates/cli/src/commands_fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ pub(crate) fn run_fix(
args: FixArgs,
config: FluffConfig,
ignorer: impl Fn(&Path) -> bool + Send + Sync,
collect_parse_errors: bool,
) -> i32 {
let FixArgs {
paths,
force,
format,
} = args;
let mut linter = linter(config, format);
let mut linter = linter(config, format, collect_parse_errors);
let result = linter.lint_paths(paths, true, &ignorer);

if result
Expand Down Expand Up @@ -68,10 +69,14 @@ pub(crate) fn run_fix(
}
}

pub(crate) fn run_fix_stdin(config: FluffConfig, format: Format) -> i32 {
pub(crate) fn run_fix_stdin(
config: FluffConfig,
format: Format,
collect_parse_errors: bool,
) -> i32 {
let read_in = crate::stdin::read_std_in().unwrap();

let linter = linter(config, format);
let linter = linter(config, format, collect_parse_errors);
let result = linter.lint_string(&read_in, None, true);

// print fixed to std out
Expand Down
12 changes: 9 additions & 3 deletions crates/cli/src/commands_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ pub(crate) fn run_lint(
args: LintArgs,
config: FluffConfig,
ignorer: impl Fn(&Path) -> bool + Send + Sync,
collect_parse_errors: bool,
) -> i32 {
let LintArgs { paths, format } = args;
let mut linter = linter(config, format);
let mut linter = linter(config, format, collect_parse_errors);

linter.lint_paths(paths, false, &ignorer);

linter.formatter().unwrap().completion_message();
Expand All @@ -20,10 +22,14 @@ pub(crate) fn run_lint(
}
}

pub(crate) fn run_lint_stdin(config: FluffConfig, format: Format) -> i32 {
pub(crate) fn run_lint_stdin(
config: FluffConfig,
format: Format,
collect_parse_errors: bool,
) -> i32 {
let read_in = crate::stdin::read_std_in().unwrap();

let linter = linter(config, format);
let linter = linter(config, format, collect_parse_errors);
linter.lint_string(&read_in, None, false);

linter.formatter().unwrap().completion_message();
Expand Down
13 changes: 7 additions & 6 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fn main() {
return codegen_docs();

let cli = Cli::parse();
let collect_parse_errors = cli.parsing_errors;

let config: FluffConfig = if let Some(config) = cli.config.as_ref() {
if !Path::new(config).is_file() {
Expand Down Expand Up @@ -77,16 +78,16 @@ fn main() {
eprintln!("{e}");
1
}
Ok(false) => commands_lint::run_lint(args, config, ignorer),
Ok(true) => commands_lint::run_lint_stdin(config, args.format),
Ok(false) => commands_lint::run_lint(args, config, ignorer, collect_parse_errors),
Ok(true) => commands_lint::run_lint_stdin(config, args.format, collect_parse_errors),
},
Commands::Fix(args) => match is_std_in_flag_input(&args.paths) {
Err(e) => {
eprintln!("{e}");
1
}
Ok(false) => commands_fix::run_fix(args, config, ignorer),
Ok(true) => commands_fix::run_fix_stdin(config, args.format),
Ok(false) => commands_fix::run_fix(args, config, ignorer, collect_parse_errors),
Ok(true) => commands_fix::run_fix_stdin(config, args.format, collect_parse_errors),
},
Commands::Lsp => {
sqruff_lsp::run();
Expand All @@ -97,7 +98,7 @@ fn main() {
std::process::exit(status_code);
}

pub(crate) fn linter(config: FluffConfig, format: Format) -> Linter {
pub(crate) fn linter(config: FluffConfig, format: Format, collect_parse_errors: bool) -> Linter {
let formatter: Arc<dyn Formatter> = match format {
Format::Human => {
let output_stream = std::io::stderr().into();
Expand All @@ -118,7 +119,7 @@ pub(crate) fn linter(config: FluffConfig, format: Format) -> Linter {
}
};

Linter::new(config, Some(formatter), None, false)
Linter::new(config, Some(formatter), None, collect_parse_errors)
}

fn check_user_input() -> Option<bool> {
Expand Down
40 changes: 40 additions & 0 deletions crates/cli/tests/fix_parse_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use core::str;
use std::path::{Path, PathBuf};

use assert_cmd::Command;

fn main() {
parse_errors();
}

fn parse_errors() {
let profile = if cfg!(debug_assertions) {
"debug"
} else {
"release"
};

let cargo_folder = Path::new(env!("CARGO_MANIFEST_DIR"));
let mut sqruff_path = PathBuf::from(cargo_folder);
sqruff_path.push(format!("../../target/{}/sqruff", profile));

// STDIN - do nothing
let mut cmd = Command::new(sqruff_path.clone());
cmd.env("HOME", PathBuf::from(env!("CARGO_MANIFEST_DIR")));
cmd.arg("fix")
.arg("-f")
.arg("human")
.arg("--parsing-errors")
.arg("-");
cmd.current_dir(cargo_folder);
cmd.write_stdin("SelEc");

let assert = cmd.assert();
let output = assert.get_output();

let stdout_str = str::from_utf8(&output.stdout).unwrap();
let stderr_str = str::from_utf8(&output.stderr).unwrap();
assert_eq!(stdout_str, "SelEc\n\n");
assert_eq!(stderr_str, "== [<string>] FAIL\nL: 1 | P: 1 | ???? | Unparsable section\nL: 1 | P: 1 | LT12 | Files must end with a single trailing newline.\n | [layout.end_of_file]\n");
assert_eq!(output.status.code().unwrap(), 1);
}
2 changes: 1 addition & 1 deletion crates/lib/src/core/linter/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ impl Linter {
);

violations.extend(unparsables.into_iter().map(|segment| SQLParseError {
description: "Found unparsable section".into(),
description: "Unparsable section".into(),
segment: segment.into(),
}));
}
Expand Down
3 changes: 3 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ sqruff is a sql formatter and linter
###### **Options:**

* `--config <CONFIG>` — Path to a configuration file
* `--parsing-errors` — Show parse errors

Default value: `false`



Expand Down

0 comments on commit ec6142e

Please sign in to comment.