limit the ide-check error amount (#8875)

# Description

This PR limits the amount of errors returned with the --ide-check flag.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
Darren Schroeder 2023-04-13 12:53:18 -05:00 committed by GitHub
parent 3603610026
commit a33a7960bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 16 deletions

View File

@ -37,9 +37,8 @@ pub(crate) fn gather_commandline_args() -> (Vec<String>, String, Vec<String>) {
#[cfg(feature = "plugin")]
"--plugin-config" => args.next().map(|a| escape_quote_string(&a)),
"--log-level" | "--log-target" | "--testbin" | "--threads" | "-t"
| "--include-path" | "-I" | "--ide-goto-def" | "--ide-hover" | "--ide-complete" => {
args.next()
}
| "--include-path" | "-I" | "--ide-goto-def" | "--ide-hover" | "--ide-complete"
| "--ide-check" => args.next(),
_ => None,
};
@ -92,12 +91,6 @@ pub(crate) fn parse_commandline_args(
)) = pipeline.elements.get(0)
{
let redirect_stdin = call.get_named_arg("stdin");
let ide_goto_def: Option<Value> =
call.get_flag(engine_state, &mut stack, "ide-goto-def")?;
let ide_hover: Option<Value> = call.get_flag(engine_state, &mut stack, "ide-hover")?;
let ide_complete: Option<Value> =
call.get_flag(engine_state, &mut stack, "ide-complete")?;
let ide_check = call.get_named_arg("ide-check");
let login_shell = call.get_named_arg("login");
let interactive_shell = call.get_named_arg("interactive");
let commands: Option<Expression> = call.get_flag_expr("commands");
@ -115,6 +108,13 @@ pub(crate) fn parse_commandline_args(
let table_mode: Option<Value> =
call.get_flag(engine_state, &mut stack, "table-mode")?;
let ide_goto_def: Option<Value> =
call.get_flag(engine_state, &mut stack, "ide-goto-def")?;
let ide_hover: Option<Value> = call.get_flag(engine_state, &mut stack, "ide-hover")?;
let ide_complete: Option<Value> =
call.get_flag(engine_state, &mut stack, "ide-complete")?;
let ide_check: Option<Value> = call.get_flag(engine_state, &mut stack, "ide-check")?;
fn extract_contents(
expression: Option<Expression>,
) -> Result<Option<Spanned<String>>, ShellError> {
@ -229,7 +229,7 @@ pub(crate) struct NushellCliArgs {
pub(crate) ide_goto_def: Option<Value>,
pub(crate) ide_hover: Option<Value>,
pub(crate) ide_complete: Option<Value>,
pub(crate) ide_check: Option<Spanned<String>>,
pub(crate) ide_check: Option<Value>,
}
#[derive(Clone)]
@ -312,8 +312,9 @@ impl Command for Nu {
"list completions for the item at the given position",
None,
)
.switch(
.named(
"ide-check",
SyntaxShape::Int,
"run a diagnostic check on the given source",
None,
);

View File

@ -74,18 +74,28 @@ fn read_in_file<'a>(
(file, working_set)
}
pub fn check(engine_state: &mut EngineState, file_path: &String) {
pub fn check(engine_state: &mut EngineState, file_path: &String, max_errors: &Value) {
let cwd = std::env::current_dir().expect("Could not get current working directory.");
engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy()));
let mut working_set = StateWorkingSet::new(engine_state);
let file = std::fs::read(file_path);
let max_errors = if let Ok(max_errors) = max_errors.as_i64() {
max_errors as usize
} else {
100
};
if let Ok(contents) = file {
let offset = working_set.next_span_start();
let block = parse(&mut working_set, Some(file_path), &contents, false);
for err in &working_set.parse_errors {
for (idx, err) in working_set.parse_errors.iter().enumerate() {
if idx >= max_errors {
// eprintln!("Too many errors, stopping here. idx: {idx} max_errors: {max_errors}");
break;
}
let mut span = err.span();
span.start -= offset;
span.end -= offset;

View File

@ -167,8 +167,8 @@ fn main() -> Result<()> {
ide::complete(Arc::new(engine_state), &script_name, &ide_complete);
return Ok(());
} else if parsed_nu_cli_args.ide_check.is_some() {
ide::check(&mut engine_state, &script_name);
} else if let Some(max_errors) = parsed_nu_cli_args.ide_check {
ide::check(&mut engine_state, &script_name, &max_errors);
return Ok(());
}

View File

@ -4,7 +4,7 @@ use crate::tests::{test_ide_contains, TestResult};
fn parser_recovers() -> TestResult {
test_ide_contains(
"3 + \"bob\"\nlet x = \"fred\"\n",
&["--ide-check"],
&["--ide-check 5"],
"\"typename\":\"string\"",
)
}