+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

feat: suppress warnings flag for check and ci #4535

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/rome_cli/src/cli_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub struct CliOptions {
#[bpaf(long("max-diagnostics"), argument("NUMBER"), optional)]
pub max_diagnostics: Option<u16>,

/// Do not emit warning diagnostics
#[bpaf(long("suppress-warnings"), switch)]
pub suppress_warnings: bool,

/// Skip over files containing syntax errors instead of emitting an error diagnostic.
#[bpaf(long("skip-errors"), switch)]
pub skip_errors: bool,
Expand Down
14 changes: 13 additions & 1 deletion crates/rome_cli/src/execute/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub(crate) fn traverse(
errors: &mut errors,
report: &mut report,
verbose: cli_options.verbose,
suppress_warnings: cli_options.suppress_warnings,
});
})
.expect("failed to spawn console thread");
Expand Down Expand Up @@ -266,6 +267,8 @@ struct ProcessMessagesOptions<'ctx> {
report: &'ctx mut Report,
/// Whether the console thread should print diagnostics in verbose mode
verbose: bool,
/// Whether the console thread should skip printing warning diagnostics
suppress_warnings: bool,
}

/// This thread receives [Message]s from the workers through the `recv_msgs`
Expand All @@ -282,6 +285,7 @@ fn process_messages(options: ProcessMessagesOptions) {
errors,
report,
verbose,
suppress_warnings,
} = options;

let mut paths: HashSet<String> = HashSet::new();
Expand Down Expand Up @@ -422,6 +426,10 @@ fn process_messages(options: ProcessMessagesOptions) {
*errors += 1;
}

if diag.severity() == Severity::Warning && suppress_warnings {
continue;
}

let diag = diag.with_file_path(&name).with_file_source_code(&content);
console.error(markup! {
{if verbose { PrintDiagnostic::verbose(&diag) } else { PrintDiagnostic::simple(&diag) }}
Expand All @@ -434,7 +442,11 @@ fn process_messages(options: ProcessMessagesOptions) {
*errors += 1;
}

let should_print = printed_diagnostics < max_diagnostics;
let should_suppress = severity == Severity::Warning && suppress_warnings;

let should_print =
printed_diagnostics < max_diagnostics && !should_suppress;

if should_print {
printed_diagnostics += 1;
remaining_diagnostics.store(
Expand Down
62 changes: 62 additions & 0 deletions crates/rome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ for(;true;);for(;true;);for(;true;);for(;true;);for(;true;);for(;true;);
"#;

const NO_DEBUGGER: &str = "debugger;";

const NEW_SYMBOL: &str = "new Symbol(\"\");";

const FIX_BEFORE: &str = "
Expand Down Expand Up @@ -1328,6 +1329,67 @@ fn print_verbose() {
));
}

#[test]
fn suppress_warnings() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let rome_path = Path::new("rome.json");
fs.insert(
rome_path.into(),
CONFIG_LINTER_DOWNGRADE_DIAGNOSTIC.as_bytes(),
);

let file_path = Path::new("file.ts");

const DEBUG_AND_ANY: &str = "debugger; const a: any = 1;";

fs.insert(file_path.into(), DEBUG_AND_ANY.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(&[
("check"),
("--suppress-warnings"),
file_path.as_os_str().to_str().unwrap(),
]),
);
assert!(result.is_err(), "run_cli returned {result:?}");

let messages = &console.out_buffer;

assert_eq!(
messages
.iter()
.filter(|m| m.level == LogLevel::Error)
.filter(|m| {
let content = format!("{:#?}", m.content);
content.contains("suspicious/noExplicitAny")
})
.count(),
1
);

assert_eq!(
messages
.iter()
.filter(|m| {
let content = format!("{:#?}", m.content);
content.contains("suspicious/noDebugger")
})
.count(),
0
);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"suppress_warnings",
fs,
console,
result,
));
}
#[test]
fn unsupported_file() {
let mut fs = MemoryFileSystem::default();
Expand Down
69 changes: 67 additions & 2 deletions crates/rome_cli/tests/commands/ci.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::configs::{CONFIG_DISABLED_FORMATTER, CONFIG_FILE_SIZE_LIMIT, CONFIG_LINTER_DISABLED};
use crate::configs::{
CONFIG_DISABLED_FORMATTER, CONFIG_FILE_SIZE_LIMIT, CONFIG_LINTER_DISABLED,
CONFIG_LINTER_DOWNGRADE_DIAGNOSTIC,
};
use crate::snap_test::SnapshotPayload;
use crate::{
assert_cli_snapshot, run_cli, CUSTOM_FORMAT_BEFORE, FORMATTED, LINT_ERROR, PARSE_ERROR,
UNFORMATTED,
};
use bpaf::Args;
use rome_console::{BufferConsole, MarkupBuf};
use rome_console::{BufferConsole, LogLevel, MarkupBuf};
use rome_fs::{FileSystemExt, MemoryFileSystem};
use rome_service::DynRef;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -702,6 +705,68 @@ fn print_verbose() {
));
}

#[test]
fn suppress_warnings() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let rome_path = Path::new("rome.json");
fs.insert(
rome_path.into(),
CONFIG_LINTER_DOWNGRADE_DIAGNOSTIC.as_bytes(),
);

let file_path = Path::new("file.ts");

const DEBUG_AND_ANY: &str = "debugger; const a: any = 1;";

fs.insert(file_path.into(), DEBUG_AND_ANY.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(&[
("ci"),
("--suppress-warnings"),
file_path.as_os_str().to_str().unwrap(),
]),
);
assert!(result.is_err(), "run_cli returned {result:?}");

let messages = &console.out_buffer;

assert_eq!(
messages
.iter()
.filter(|m| m.level == LogLevel::Error)
.filter(|m| {
let content = format!("{:#?}", m.content);
content.contains("suspicious/noExplicitAny")
})
.count(),
1
);

assert_eq!(
messages
.iter()
.filter(|m| {
let content = format!("{:#?}", m.content);
content.contains("suspicious/noDebugger")
})
.count(),
0
);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"suppress_warnings",
fs,
console,
result,
));
}

#[test]
fn ci_formatter_linter_organize_imports() {
let mut fs = MemoryFileSystem::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Available options:
--config-path <PATH> Set the filesystem path to the directory of the rome.json configuration
file
--max-diagnostics <NUMBER> Cap the amount of diagnostics displayed (default: 20)
--suppress-warnings Do not emit warning diagnostics
--skip-errors Skip over files containing syntax errors instead of emitting an error
diagnostic.
--json Reports information using the JSON format
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `rome.json`

```json
{
"linter": {
"rules": {
"recommended": true,
"suspicious": {
"noDebugger": "warn"
}
}
}
}
```

## `file.ts`

```ts
debugger; const a: any = 1;
```

# Termination Message

```block
internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Some errors were emitted while running checks



```

# Emitted Messages

```block
file.ts:1:20 lint/suspicious/noExplicitAny ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Unexpected any. Specify a different type.

> 1 │ debugger; const a: any = 1;
│ ^^^

i any disables many type checking rules. Its use should be avoided.


```

```block
The number of diagnostics exceeds the number allowed by Rome.
Diagnostics not shown: 1.
```

```block
Checked 1 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Available options:
--config-path <PATH> Set the filesystem path to the directory of the rome.json configuration
file
--max-diagnostics <NUMBER> Cap the amount of diagnostics displayed (default: 20)
--suppress-warnings Do not emit warning diagnostics
--skip-errors Skip over files containing syntax errors instead of emitting an error
diagnostic.
--json Reports information using the JSON format
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `rome.json`

```json
{
"linter": {
"rules": {
"recommended": true,
"suspicious": {
"noDebugger": "warn"
}
}
}
}
```

## `file.ts`

```ts
debugger; const a: any = 1;
```

# Termination Message

```block
internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Some errors were emitted while running checks



```

# Emitted Messages

```block
file.ts:1:20 lint/suspicious/noExplicitAny ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Unexpected any. Specify a different type.

> 1 │ debugger; const a: any = 1;
│ ^^^

i any disables many type checking rules. Its use should be avoided.


```

```block
Checked 1 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Available options:
--config-path <PATH> Set the filesystem path to the directory of the rome.json configuration
file
--max-diagnostics <NUMBER> Cap the amount of diagnostics displayed (default: 20)
--suppress-warnings Do not emit warning diagnostics
--skip-errors Skip over files containing syntax errors instead of emitting an error
diagnostic.
--json Reports information using the JSON format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ expression: content
Acts as a server for the Language Server Protocol over stdin/stdout

Usage: [--colors off|force] [--use-server] [--verbose] [--config-path PATH] [--max-diagnostics
NUMBER] [--skip-errors]
NUMBER] [--suppress-warnings] [--skip-errors]

Available options:
Global options applied to all commands
Expand All @@ -20,6 +20,7 @@ Available options:
--config-path <PATH> Set the filesystem path to the directory of the rome.json configuration
file
--max-diagnostics <NUMBER> Cap the amount of diagnostics displayed (default: 20)
--suppress-warnings Do not emit warning diagnostics
--skip-errors Skip over files containing syntax errors instead of emitting an error
diagnostic.
--json Reports information using the JSON format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Available options:
--config-path <PATH> Set the filesystem path to the directory of the rome.json configuration
file
--max-diagnostics <NUMBER> Cap the amount of diagnostics displayed (default: 20)
--suppress-warnings Do not emit warning diagnostics
--skip-errors Skip over files containing syntax errors instead of emitting an error
diagnostic.
--json Reports information using the JSON format
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载