mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 12:51:57 +00:00
feat(linter): add --format github for github check annotation (#3191)
closes #480
This commit is contained in:
parent
c6bd616456
commit
ed3fa399a6
6 changed files with 62 additions and 7 deletions
8
.github/workflows/ecosystem.yml
vendored
8
.github/workflows/ecosystem.yml
vendored
|
|
@ -6,17 +6,19 @@ on:
|
||||||
types: [opened, synchronize]
|
types: [opened, synchronize]
|
||||||
paths:
|
paths:
|
||||||
- 'crates/oxc_parser/**'
|
- 'crates/oxc_parser/**'
|
||||||
|
- 'crates/oxc_diagnostics/**'
|
||||||
- 'crates/oxc_cli/**'
|
- 'crates/oxc_cli/**'
|
||||||
- 'crates/oxc_linter/**'
|
- 'crates/oxc_linter/**'
|
||||||
- '!.github/workflows/ecosystem.yml'
|
- '.github/workflows/ecosystem.yml'
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
paths:
|
paths:
|
||||||
- 'crates/oxc_parser/**'
|
- 'crates/oxc_parser/**'
|
||||||
|
- 'crates/oxc_diagnostics/**'
|
||||||
- 'crates/oxc_cli/**'
|
- 'crates/oxc_cli/**'
|
||||||
- 'crates/oxc_linter/**'
|
- 'crates/oxc_linter/**'
|
||||||
- '!.github/workflows/ecosystem.yml'
|
- '.github/workflows/ecosystem.yml'
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
|
||||||
|
|
@ -59,7 +61,7 @@ jobs:
|
||||||
ref: master
|
ref: master
|
||||||
steps:
|
steps:
|
||||||
- name: Clone ${{ matrix.repository }}
|
- name: Clone ${{ matrix.repository }}
|
||||||
uses: taiki-e/checkout-action@v1
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
repository: ${{ matrix.repository }}
|
repository: ${{ matrix.repository }}
|
||||||
ref: ${{ matrix.ref }}
|
ref: ${{ matrix.ref }}
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,7 @@ pub struct WarningOptions {
|
||||||
/// Output
|
/// Output
|
||||||
#[derive(Debug, Clone, Bpaf)]
|
#[derive(Debug, Clone, Bpaf)]
|
||||||
pub struct OutputOptions {
|
pub struct OutputOptions {
|
||||||
/// Use a specific output format (default, json)
|
/// Use a specific output format (default, json, unix, checkstyle, github)
|
||||||
#[bpaf(long, short, fallback(OutputFormat::Default), hide_usage)]
|
#[bpaf(long, short, fallback(OutputFormat::Default), hide_usage)]
|
||||||
pub format: OutputFormat,
|
pub format: OutputFormat,
|
||||||
}
|
}
|
||||||
|
|
@ -143,6 +143,9 @@ pub struct OutputOptions {
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||||
pub enum OutputFormat {
|
pub enum OutputFormat {
|
||||||
Default,
|
Default,
|
||||||
|
/// GitHub Check Annotation
|
||||||
|
/// <https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-notice-message>
|
||||||
|
Github,
|
||||||
Json,
|
Json,
|
||||||
Unix,
|
Unix,
|
||||||
Checkstyle,
|
Checkstyle,
|
||||||
|
|
@ -156,6 +159,7 @@ impl FromStr for OutputFormat {
|
||||||
"default" => Ok(Self::Default),
|
"default" => Ok(Self::Default),
|
||||||
"unix" => Ok(Self::Unix),
|
"unix" => Ok(Self::Unix),
|
||||||
"checkstyle" => Ok(Self::Checkstyle),
|
"checkstyle" => Ok(Self::Checkstyle),
|
||||||
|
"github" => Ok(Self::Github),
|
||||||
_ => Err(format!("'{s}' is not a known format")),
|
_ => Err(format!("'{s}' is not a known format")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -165,6 +165,7 @@ impl LintRunner {
|
||||||
OutputFormat::Json => diagnostic_service.set_json_reporter(),
|
OutputFormat::Json => diagnostic_service.set_json_reporter(),
|
||||||
OutputFormat::Unix => diagnostic_service.set_unix_reporter(),
|
OutputFormat::Unix => diagnostic_service.set_unix_reporter(),
|
||||||
OutputFormat::Checkstyle => diagnostic_service.set_checkstyle_reporter(),
|
OutputFormat::Checkstyle => diagnostic_service.set_checkstyle_reporter(),
|
||||||
|
OutputFormat::Github => diagnostic_service.set_github_reporter(),
|
||||||
}
|
}
|
||||||
diagnostic_service
|
diagnostic_service
|
||||||
}
|
}
|
||||||
|
|
|
||||||
42
crates/oxc_diagnostics/src/reporter/github.rs
Normal file
42
crates/oxc_diagnostics/src/reporter/github.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
|
io::{BufWriter, Stdout, Write},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{miette::Error, Severity};
|
||||||
|
|
||||||
|
use super::{writer, DiagnosticReporter, Info};
|
||||||
|
|
||||||
|
pub struct GithubReporter {
|
||||||
|
writer: BufWriter<Stdout>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for GithubReporter {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { writer: writer() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DiagnosticReporter for GithubReporter {
|
||||||
|
fn finish(&mut self) {
|
||||||
|
self.writer.flush().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_diagnostics(&mut self, _s: &[u8]) {}
|
||||||
|
|
||||||
|
fn render_error(&mut self, error: Error) -> Option<String> {
|
||||||
|
let message = format_github(&error);
|
||||||
|
self.writer.write_all(message.as_bytes()).unwrap();
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_github(diagnostic: &Error) -> String {
|
||||||
|
let Info { line, column, filename, message, severity, rule_id } = Info::new(diagnostic);
|
||||||
|
let severity = match severity {
|
||||||
|
Severity::Error => "error",
|
||||||
|
Severity::Warning | miette::Severity::Advice => "warning",
|
||||||
|
};
|
||||||
|
let title = rule_id.map_or(Cow::Borrowed("oxlint"), Cow::Owned);
|
||||||
|
format!("::{severity} file={filename},line={line},endLine={line},col={column},endColumn={column},title={title}::{message}\n")
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
mod checkstyle;
|
mod checkstyle;
|
||||||
|
mod github;
|
||||||
mod graphical;
|
mod graphical;
|
||||||
mod json;
|
mod json;
|
||||||
mod unix;
|
mod unix;
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
checkstyle::CheckstyleReporter, graphical::GraphicalReporter, json::JsonReporter,
|
checkstyle::CheckstyleReporter, github::GithubReporter, graphical::GraphicalReporter,
|
||||||
unix::UnixReporter,
|
json::JsonReporter, unix::UnixReporter,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::io::{BufWriter, Stdout};
|
use std::io::{BufWriter, Stdout};
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,8 @@ use std::{
|
||||||
use crate::{
|
use crate::{
|
||||||
miette::NamedSource,
|
miette::NamedSource,
|
||||||
reporter::{
|
reporter::{
|
||||||
CheckstyleReporter, DiagnosticReporter, GraphicalReporter, JsonReporter, UnixReporter,
|
CheckstyleReporter, DiagnosticReporter, GithubReporter, GraphicalReporter, JsonReporter,
|
||||||
|
UnixReporter,
|
||||||
},
|
},
|
||||||
Error, MinifiedFileError, Severity,
|
Error, MinifiedFileError, Severity,
|
||||||
};
|
};
|
||||||
|
|
@ -64,6 +65,10 @@ impl DiagnosticService {
|
||||||
self.reporter = Box::<CheckstyleReporter>::default();
|
self.reporter = Box::<CheckstyleReporter>::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_github_reporter(&mut self) {
|
||||||
|
self.reporter = Box::<GithubReporter>::default();
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn with_quiet(mut self, yes: bool) -> Self {
|
pub fn with_quiet(mut self, yes: bool) -> Self {
|
||||||
self.quiet = yes;
|
self.quiet = yes;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue