From a189c5393e1b106687736635fea0b8aeca283826 Mon Sep 17 00:00:00 2001 From: Brenley Dueck Date: Tue, 25 Oct 2022 07:21:20 -0500 Subject: [PATCH] feat(lint): add a report lint config setting (#16045) Builds off this PR to add a "report" setting to deno.json which can be "pretty", "compact", or "json". --- cli/args/config_file.rs | 3 ++ cli/schemas/config-file.v1.json | 9 ++++ cli/tests/integration/lint_tests.rs | 12 ++++++ .../testdata/lint/Deno.compact.format.jsonc | 13 ++++++ .../testdata/lint/with_malformed_config.out | 2 +- .../lint/with_report_config_compact.out | 4 ++ .../lint/with_report_config_override.out | 41 +++++++++++++++++++ cli/tools/lint.rs | 28 ++++++++++--- 8 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 cli/tests/testdata/lint/Deno.compact.format.jsonc create mode 100644 cli/tests/testdata/lint/with_report_config_compact.out create mode 100644 cli/tests/testdata/lint/with_report_config_override.out diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs index b0a5b355b9..cf88e3999c 100644 --- a/cli/args/config_file.rs +++ b/cli/args/config_file.rs @@ -342,6 +342,7 @@ impl FilesConfig { struct SerializedLintConfig { pub rules: LintRulesConfig, pub files: SerializedFilesConfig, + pub report: Option, } impl SerializedLintConfig { @@ -352,6 +353,7 @@ impl SerializedLintConfig { Ok(LintConfig { rules: self.rules, files: self.files.into_resolved(config_file_specifier)?, + report: self.report, }) } } @@ -360,6 +362,7 @@ impl SerializedLintConfig { pub struct LintConfig { pub rules: LintRulesConfig, pub files: FilesConfig, + pub report: Option, } #[derive(Clone, Copy, Debug, Serialize, Deserialize)] diff --git a/cli/schemas/config-file.v1.json b/cli/schemas/config-file.v1.json index a8f80dd361..26cd3ab77d 100644 --- a/cli/schemas/config-file.v1.json +++ b/cli/schemas/config-file.v1.json @@ -249,6 +249,15 @@ "uniqueItems": true } } + }, + "report": { + "default": "pretty", + "enum": [ + "pretty", + "json", + "compact" + ], + "description": "The default report format to use when linting" } } }, diff --git a/cli/tests/integration/lint_tests.rs b/cli/tests/integration/lint_tests.rs index afcdc6e40f..70a3cbbd47 100644 --- a/cli/tests/integration/lint_tests.rs +++ b/cli/tests/integration/lint_tests.rs @@ -95,7 +95,19 @@ itest!(lint_with_config { exit_code: 1, }); +itest!(lint_with_report_config { + args: "lint --config lint/Deno.compact.format.jsonc lint/with_config/", + output: "lint/with_report_config_compact.out", + exit_code: 1, +}); + // Check if CLI flags take precedence +itest!(lint_with_report_config_override { + args: "lint --config lint/Deno.compact.format.jsonc lint/with_config/ --json", + output: "lint/with_report_config_override.out", + exit_code: 1, +}); + itest!(lint_with_config_and_flags { args: "lint --config lint/Deno.jsonc --ignore=lint/with_config/a.ts", output: "lint/with_config_and_flags.out", diff --git a/cli/tests/testdata/lint/Deno.compact.format.jsonc b/cli/tests/testdata/lint/Deno.compact.format.jsonc new file mode 100644 index 0000000000..24b159ca6b --- /dev/null +++ b/cli/tests/testdata/lint/Deno.compact.format.jsonc @@ -0,0 +1,13 @@ +{ + "lint": { + "files": { + "include": ["with_config/"], + "exclude": ["with_config/b.ts"] + }, + "rules": { + "tags": ["recommended"], + "include": ["ban-untagged-todo"] + }, + "report": "compact" + } +} diff --git a/cli/tests/testdata/lint/with_malformed_config.out b/cli/tests/testdata/lint/with_malformed_config.out index 88fb8c4575..3aa4910653 100644 --- a/cli/tests/testdata/lint/with_malformed_config.out +++ b/cli/tests/testdata/lint/with_malformed_config.out @@ -1,4 +1,4 @@ error: Failed to parse "lint" configuration Caused by: - unknown field `dont_know_this_field`, expected `rules` or `files` + unknown field `dont_know_this_field`, expected one of `rules`, `files`, `report` diff --git a/cli/tests/testdata/lint/with_report_config_compact.out b/cli/tests/testdata/lint/with_report_config_compact.out new file mode 100644 index 0000000000..fe12412647 --- /dev/null +++ b/cli/tests/testdata/lint/with_report_config_compact.out @@ -0,0 +1,4 @@ +[WILDCARD]a.ts: line 1, col 1 - TODO should be tagged with (@username) or (#issue) (ban-untagged-todo) +[WILDCARD]a.ts: line 2, col 10 - `add` is never used (no-unused-vars) +Found 2 problems +Checked 1 file diff --git a/cli/tests/testdata/lint/with_report_config_override.out b/cli/tests/testdata/lint/with_report_config_override.out new file mode 100644 index 0000000000..ac633d911d --- /dev/null +++ b/cli/tests/testdata/lint/with_report_config_override.out @@ -0,0 +1,41 @@ +{ + "diagnostics": [ + { + "range": { + "start": { + "line": 1, + "col": 0, + "bytePos": 0 + }, + "end": { + "line": 1, + "col": 12, + "bytePos": 12 + } + }, + "filename": "[WILDCARD]a.ts", + "message": "TODO should be tagged with (@username) or (#issue)", + "code": "ban-untagged-todo", + "hint": "Add a user tag or issue reference to the TODO comment, e.g. TODO(@djones), TODO(djones), TODO(#123)" + }, + { + "range": { + "start": { + "line": 2, + "col": 9, + "bytePos": 22 + }, + "end": { + "line": 2, + "col": 12, + "bytePos": 25 + } + }, + "filename": "[WILDCARD]a.ts", + "message": "`add` is never used", + "code": "no-unused-vars", + "hint": "If this is intentional, prefix it with an underscore like `_add`" + } + ], + "errors": [] +} diff --git a/cli/tools/lint.rs b/cli/tools/lint.rs index 9afc9cd46b..bfbff8005e 100644 --- a/cli/tools/lint.rs +++ b/cli/tools/lint.rs @@ -77,6 +77,13 @@ pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> { // and `--ignore` CLI flag, only the flag value is taken into account. let mut include_files = args.clone(); let mut exclude_files = ignore.clone(); + let mut maybe_reporter_kind = if json { + Some(LintReporterKind::Json) + } else if compact { + Some(LintReporterKind::Compact) + } else { + None + }; let ps = ProcState::build(flags).await?; let maybe_lint_config = ps.options.to_lint_config()?; @@ -99,18 +106,27 @@ pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> { .filter_map(|s| specifier_to_file_path(s).ok()) .collect::>(); } + + if maybe_reporter_kind.is_none() { + maybe_reporter_kind = match lint_config.report.as_deref() { + Some("json") => Some(LintReporterKind::Json), + Some("compact") => Some(LintReporterKind::Compact), + Some("pretty") => Some(LintReporterKind::Pretty), + Some(_) => { + return Err(anyhow!("Invalid lint report type in config file")) + } + None => Some(LintReporterKind::Pretty), + } + } } if include_files.is_empty() { include_files = [std::env::current_dir()?].to_vec(); } - let reporter_kind = if json { - LintReporterKind::Json - } else if compact { - LintReporterKind::Compact - } else { - LintReporterKind::Pretty + let reporter_kind = match maybe_reporter_kind { + Some(report) => report, + None => LintReporterKind::Pretty, }; let has_error = Arc::new(AtomicBool::new(false));