From b86ef7d2dd39bb00e14470a01b3ad885117681a7 Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 12 May 2024 13:01:07 +0800 Subject: [PATCH] fix(diagnostics): need to escape strings for --format github closes #3212 --- crates/oxc_diagnostics/src/reporter/github.rs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/crates/oxc_diagnostics/src/reporter/github.rs b/crates/oxc_diagnostics/src/reporter/github.rs index 16db57940..9503f4438 100644 --- a/crates/oxc_diagnostics/src/reporter/github.rs +++ b/crates/oxc_diagnostics/src/reporter/github.rs @@ -38,5 +38,41 @@ fn format_github(diagnostic: &Error) -> String { Severity::Warning | miette::Severity::Advice => "warning", }; let title = rule_id.map_or(Cow::Borrowed("oxlint"), Cow::Owned); + let filename = escape_property(&filename); + let message = escape_data(&message); format!("::{severity} file={filename},line={line},endLine={line},col={column},endColumn={column},title={title}::{message}\n") } + +fn escape_data(value: &str) -> String { + // Refs: + // - https://github.com/actions/runner/blob/a4c57f27477077e57545af79851551ff7f5632bd/src/Runner.Common/ActionCommand.cs#L18-L22 + // - https://github.com/actions/toolkit/blob/fe3e7ce9a7f995d29d1fcfd226a32bca407f9dc8/packages/core/src/command.ts#L80-L94 + let mut result = String::with_capacity(value.len()); + for c in value.chars() { + match c { + '\r' => result.push_str("%0D"), + '\n' => result.push_str("%0A"), + '%' => result.push_str("%25"), + _ => result.push(c), + } + } + result +} + +fn escape_property(value: &str) -> String { + // Refs: + // - https://github.com/actions/runner/blob/a4c57f27477077e57545af79851551ff7f5632bd/src/Runner.Common/ActionCommand.cs#L25-L32 + // - https://github.com/actions/toolkit/blob/fe3e7ce9a7f995d29d1fcfd226a32bca407f9dc8/packages/core/src/command.ts#L80-L94 + let mut result = String::with_capacity(value.len()); + for c in value.chars() { + match c { + '\r' => result.push_str("%0D"), + '\n' => result.push_str("%0A"), + ':' => result.push_str("%3A"), + ',' => result.push_str("%2C"), + '%' => result.push_str("%25"), + _ => result.push(c), + } + } + result +}