-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(linter): add output formatter (#8436)
I want to start grouping all the different Formats for action X, Y and Z into own place. This is the first step and probably not the best one to be honest :) ~~I prefer that the `OutputFormatter` is a part of `oxlint` and not `oxc_linter`~~ ~~but all `use create::rules::RULES` is not public from outside.~~ EDIT: I pushed a commit with this changes can easily be reverted and move back to `oxc_linter` Also their is a crate `oxc_diagnostics` which has already the concept too but only for the lint part of `oxlint` and not for the other parts. The next goal would be splitting the `DiagnosticService` with its reporters to `oxlint`.
- Loading branch information
Showing
10 changed files
with
128 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod command; | ||
mod lint; | ||
mod output_formatter; | ||
mod result; | ||
mod runner; | ||
mod walk; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::io::Write; | ||
|
||
use oxc_linter::table::RuleTable; | ||
|
||
pub struct DefaultOutputFormatter; | ||
|
||
impl DefaultOutputFormatter { | ||
pub fn all_rules<T: Write>(writer: &mut T) { | ||
let table = RuleTable::new(); | ||
for section in table.sections { | ||
writeln!(writer, "{}", section.render_markdown_table(None)).unwrap(); | ||
} | ||
writeln!(writer, "Default: {}", table.turned_on_by_default_count).unwrap(); | ||
writeln!(writer, "Total: {}", table.total).unwrap(); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::output_formatter::default::DefaultOutputFormatter; | ||
|
||
#[test] | ||
fn all_rules() { | ||
let mut writer = Vec::new(); | ||
|
||
DefaultOutputFormatter::all_rules(&mut writer); | ||
assert!(!writer.is_empty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use oxc_linter::rules::RULES; | ||
use oxc_linter::RuleCategory; | ||
use std::io::Write; | ||
|
||
#[derive(Debug)] | ||
pub struct JsonOutputFormatter; | ||
|
||
impl JsonOutputFormatter { | ||
pub fn all_rules<T: Write>(writer: &mut T) { | ||
#[derive(Debug, serde::Serialize)] | ||
struct RuleInfoJson<'a> { | ||
scope: &'a str, | ||
value: &'a str, | ||
category: RuleCategory, | ||
} | ||
|
||
let rules_info = RULES.iter().map(|rule| RuleInfoJson { | ||
scope: rule.plugin_name(), | ||
value: rule.name(), | ||
category: rule.category(), | ||
}); | ||
|
||
writer | ||
.write_all( | ||
serde_json::to_string_pretty(&rules_info.collect::<Vec<_>>()) | ||
.expect("Failed to serialize") | ||
.as_bytes(), | ||
) | ||
.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
mod default; | ||
mod json; | ||
|
||
use std::io::Write; | ||
use std::str::FromStr; | ||
|
||
use crate::output_formatter::{default::DefaultOutputFormatter, json::JsonOutputFormatter}; | ||
|
||
pub struct OutputFormatter { | ||
format: OutputFormat, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
pub enum OutputFormat { | ||
Default, | ||
/// GitHub Check Annotation | ||
/// <https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-notice-message> | ||
Github, | ||
Json, | ||
Unix, | ||
Checkstyle, | ||
} | ||
|
||
impl FromStr for OutputFormat { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"json" => Ok(Self::Json), | ||
"default" => Ok(Self::Default), | ||
"unix" => Ok(Self::Unix), | ||
"checkstyle" => Ok(Self::Checkstyle), | ||
"github" => Ok(Self::Github), | ||
_ => Err(format!("'{s}' is not a known format")), | ||
} | ||
} | ||
} | ||
|
||
impl OutputFormatter { | ||
pub fn new(format: OutputFormat) -> Self { | ||
Self { format } | ||
} | ||
// print all rules which are currently supported by oxlint | ||
pub fn all_rules<T: Write>(&self, writer: &mut T) { | ||
match self.format { | ||
OutputFormat::Json => JsonOutputFormatter::all_rules(writer), | ||
_ => DefaultOutputFormatter::all_rules(writer), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters