1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 00:59:24 -05:00

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".
This commit is contained in:
Brenley Dueck 2022-10-25 07:21:20 -05:00 committed by GitHub
parent ac5fcf626a
commit a189c5393e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 105 additions and 7 deletions

View file

@ -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)]

View file

@ -249,6 +249,15 @@
"uniqueItems": true
}
}
},
"report": {
"default": "pretty",
"enum": [
"pretty",
"json",
"compact"
],
"description": "The default report format to use when linting"
}
}
},

View file

@ -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",

View file

@ -0,0 +1,13 @@
{
"lint": {
"files": {
"include": ["with_config/"],
"exclude": ["with_config/b.ts"]
},
"rules": {
"tags": ["recommended"],
"include": ["ban-untagged-todo"]
},
"report": "compact"
}
}

View file

@ -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`

View file

@ -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

View file

@ -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": []
}

View file

@ -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));