summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorBrenley Dueck <brenleydueck@gmail.com>2022-10-25 07:21:20 -0500
committerGitHub <noreply@github.com>2022-10-25 14:21:20 +0200
commita189c5393e1b106687736635fea0b8aeca283826 (patch)
tree6c0119266f77611e1ba8146ee394f8e10cf8b3c4 /cli
parentac5fcf626a77db7795f7ab2b4f15e4ecb3270171 (diff)
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".
Diffstat (limited to 'cli')
-rw-r--r--cli/args/config_file.rs3
-rw-r--r--cli/schemas/config-file.v1.json9
-rw-r--r--cli/tests/integration/lint_tests.rs12
-rw-r--r--cli/tests/testdata/lint/Deno.compact.format.jsonc13
-rw-r--r--cli/tests/testdata/lint/with_malformed_config.out2
-rw-r--r--cli/tests/testdata/lint/with_report_config_compact.out4
-rw-r--r--cli/tests/testdata/lint/with_report_config_override.out41
-rw-r--r--cli/tools/lint.rs28
8 files changed, 105 insertions, 7 deletions
diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs
index b0a5b355b..cf88e3999 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<String>,
}
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<String>,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
diff --git a/cli/schemas/config-file.v1.json b/cli/schemas/config-file.v1.json
index a8f80dd36..26cd3ab77 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 afcdc6e40..70a3cbbd4 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 000000000..24b159ca6
--- /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 88fb8c457..3aa491065 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 000000000..fe1241264
--- /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 000000000..ac633d911
--- /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 9afc9cd46..bfbff8005 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::<Vec<_>>();
}
+
+ 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));