mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(linter/eslint): Implement fixer for unicode-bom rule (#3259)
This commit is contained in:
parent
f6daf0b7ae
commit
3644400aed
1 changed files with 14 additions and 4 deletions
|
|
@ -2,7 +2,7 @@ use oxc_diagnostics::OxcDiagnostic;
|
|||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
|
||||
use crate::{context::LintContext, rule::Rule};
|
||||
use crate::{context::LintContext, rule::Rule, Fix};
|
||||
|
||||
fn unexpected_unicode_bom_diagnostic(span0: Span) -> OxcDiagnostic {
|
||||
OxcDiagnostic::warning("eslint(unicode-bom): Unexpected Unicode BOM (Byte Order Mark)")
|
||||
|
|
@ -59,11 +59,15 @@ impl Rule for UnicodeBom {
|
|||
let has_bomb = source.starts_with('');
|
||||
|
||||
if has_bomb && matches!(self.bom_option, BomOptionType::Never) {
|
||||
ctx.diagnostic(unexpected_unicode_bom_diagnostic(Span::new(0, 0)));
|
||||
ctx.diagnostic_with_fix(unexpected_unicode_bom_diagnostic(Span::new(0, 0)), || {
|
||||
return Fix::delete(Span::new(0, 3));
|
||||
});
|
||||
}
|
||||
|
||||
if !has_bomb && matches!(self.bom_option, BomOptionType::Always) {
|
||||
ctx.diagnostic(expected_unicode_bom_diagnostic(Span::new(0, 0)));
|
||||
ctx.diagnostic_with_fix(expected_unicode_bom_diagnostic(Span::new(0, 0)), || {
|
||||
return Fix::new("", Span::new(0, 0));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -105,5 +109,11 @@ fn test() {
|
|||
(" var a = 123;", Some(serde_json::json!(["never"]))),
|
||||
];
|
||||
|
||||
Tester::new(UnicodeBom::NAME, pass, fail).test_and_snapshot();
|
||||
let fix = vec![
|
||||
("var a = 123;", "var a = 123;", Some(serde_json::json!(["never"]))),
|
||||
("var a = 123;", "var a = 123;", None),
|
||||
("var a = 123;", "var a = 123;", Some(serde_json::json!(["always"]))),
|
||||
];
|
||||
|
||||
Tester::new(UnicodeBom::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue