Skip to content

Commit 8a1a843

Browse files
committed
test(linter): add test for Message::from_tsgo_lint_diagnostic
1 parent a1b52c8 commit 8a1a843

File tree

1 file changed

+92
-2
lines changed

1 file changed

+92
-2
lines changed

crates/oxc_linter/src/tsgolint.rs

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use serde::{Deserialize, Serialize};
1111
use oxc_diagnostics::{DiagnosticSender, DiagnosticService, OxcDiagnostic, Severity};
1212
use oxc_span::{SourceType, Span};
1313

14-
use crate::fixer::{CompositeFix, Message, PossibleFixes};
14+
use crate::fixer::Message;
15+
#[cfg(test)]
16+
use crate::fixer::{CompositeFix, PossibleFixes};
1517

1618
use super::{AllowWarnDeny, ConfigStore, ResolvedLinterState, read_to_string};
1719

@@ -368,7 +370,7 @@ impl From<TsGoLintDiagnostic> for OxcDiagnostic {
368370

369371
impl Message<'_> {
370372
/// Converts a `TsGoLintDiagnostic` into a `Message` with possible fixes.
371-
#[expect(dead_code)]
373+
#[cfg(test)]
372374
fn from_tsgo_lint_diagnostic(val: TsGoLintDiagnostic, source_text: &str) -> Self {
373375
let possible_fix = if val.fixes.is_empty() {
374376
PossibleFixes::None
@@ -506,3 +508,91 @@ pub fn try_find_tsgolint_executable(cwd: &Path) -> Option<PathBuf> {
506508

507509
None
508510
}
511+
512+
#[cfg(test)]
513+
mod test {
514+
use oxc_diagnostics::{LabeledSpan, OxcCode, Severity};
515+
use oxc_span::{GetSpan, Span};
516+
517+
use crate::{
518+
fixer::{Message, PossibleFixes},
519+
tsgolint::{Fix, MessageType, Range, RuleMessage, TsGoLintDiagnostic},
520+
};
521+
522+
impl PartialEq for PossibleFixes<'_> {
523+
fn eq(&self, other: &Self) -> bool {
524+
match (self, other) {
525+
(Self::None, Self::None) => true,
526+
(Self::Single(fix1), Self::Single(fix2)) => fix1 == fix2,
527+
(Self::Multiple(fixes1), Self::Multiple(fixes2)) => {
528+
fixes1.iter().zip(fixes2.iter()).all(|(f1, f2)| f1 == f2)
529+
}
530+
_ => false,
531+
}
532+
}
533+
}
534+
535+
#[test]
536+
fn test_message_from_tsgo_lint_diagnostic_basic() {
537+
let diagnostic = TsGoLintDiagnostic {
538+
r#type: MessageType::Diagnostic,
539+
range: Range { pos: 0, end: 10 },
540+
rule: "some_rule".into(),
541+
message: RuleMessage {
542+
id: "some_id".into(),
543+
description: "Some description".into(),
544+
help: Some("Some help".into()),
545+
},
546+
fixes: vec![],
547+
suggestions: vec![],
548+
file_path: "some/file/path".into(),
549+
};
550+
551+
let message = Message::from_tsgo_lint_diagnostic(diagnostic, "Some text over 10 bytes.");
552+
553+
assert_eq!(message.error.message, "Some description");
554+
assert_eq!(message.error.severity, Severity::Warning);
555+
assert_eq!(message.span(), Span::new(0, 10));
556+
assert_eq!(
557+
message.error.code,
558+
OxcCode { scope: Some("typescript-eslint".into()), number: Some("some_rule".into()) }
559+
);
560+
assert!(message.error.labels.as_ref().is_some());
561+
assert_eq!(message.error.labels.as_ref().unwrap().len(), 1);
562+
assert_eq!(message.error.labels.as_ref().unwrap()[0], LabeledSpan::new(None, 0, 10));
563+
assert_eq!(message.error.help, Some("Some help".into()));
564+
assert!(message.fixes.is_empty());
565+
}
566+
567+
#[test]
568+
fn test_message_from_tsgo_lint_diagnostic_with_fixes() {
569+
let diagnostic = TsGoLintDiagnostic {
570+
r#type: MessageType::Diagnostic,
571+
range: Range { pos: 0, end: 10 },
572+
rule: "some_rule".into(),
573+
message: RuleMessage {
574+
id: "some_id".into(),
575+
description: "Some description".into(),
576+
help: None,
577+
},
578+
fixes: vec![
579+
Fix { text: "fixed".into(), range: Range { pos: 0, end: 5 } },
580+
Fix { text: "hello".into(), range: Range { pos: 5, end: 10 } },
581+
],
582+
suggestions: vec![],
583+
file_path: "some/file/path".into(),
584+
};
585+
586+
let message = Message::from_tsgo_lint_diagnostic(diagnostic, "Some text over 10 bytes.");
587+
588+
assert_eq!(message.fixes.len(), 1);
589+
assert_eq!(
590+
message.fixes,
591+
PossibleFixes::Single(crate::fixer::Fix {
592+
content: "fixedhello".into(),
593+
span: Span::new(0, 10),
594+
message: None,
595+
})
596+
);
597+
}
598+
}

0 commit comments

Comments
 (0)