Skip to content

Commit

Permalink
feat(linter/unicorn): add fixer to text-encoding-identifier-case (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 authored Aug 23, 2024
1 parent 7eb052e commit 27db769
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ declare_oxc_lint!(
/// ```
TextEncodingIdentifierCase,
style,
pending
fix
);

impl Rule for TextEncodingIdentifierCase {
Expand All @@ -72,7 +72,10 @@ impl Rule for TextEncodingIdentifierCase {
return;
}

ctx.diagnostic(text_encoding_identifier_case_diagnostic(span, replacement, s));
ctx.diagnostic_with_fix(
text_encoding_identifier_case_diagnostic(span, replacement, s),
|fixer| fixer.replace(Span::new(span.start + 1, span.end - 1), replacement),
);
}
}

Expand Down Expand Up @@ -169,5 +172,31 @@ fn test() {
r#"<META CHARSET="ASCII" />"#,
];

Tester::new(TextEncodingIdentifierCase::NAME, pass, fail).test_and_snapshot();
let fix = vec![
(r#""UTF-8""#, r#""utf8""#),
(r#""utf-8""#, r#""utf8""#),
(r"'utf-8'", r"'utf8'"),
(r#""Utf8""#, r#""utf8""#),
(r#""ASCII""#, r#""ascii""#),
(r#"fs.readFile?.(file, "UTF-8")"#, r#"fs.readFile?.(file, "utf8")"#),
(r#"fs?.readFile(file, "UTF-8")"#, r#"fs?.readFile(file, "utf8")"#),
(r#"readFile(file, "UTF-8")"#, r#"readFile(file, "utf8")"#),
(r#"fs.readFile(...file, "UTF-8")"#, r#"fs.readFile(...file, "utf8")"#),
(r#"new fs.readFile(file, "UTF-8")"#, r#"new fs.readFile(file, "utf8")"#),
(r#"fs.readFile(file, {encoding: "UTF-8"})"#, r#"fs.readFile(file, {encoding: "utf8"})"#),
(r#"fs.readFile("UTF-8")"#, r#"fs.readFile("utf8")"#),
(r#"fs.readFile(file, "UTF-8", () => {})"#, r#"fs.readFile(file, "utf8", () => {})"#),
(r#"fs.readFileSync(file, "UTF-8")"#, r#"fs.readFileSync(file, "utf8")"#),
(r#"fs[readFile](file, "UTF-8")"#, r#"fs[readFile](file, "utf8")"#),
(r#"fs["readFile"](file, "UTF-8")"#, r#"fs["readFile"](file, "utf8")"#),
(r#"await fs.readFile(file, "UTF-8",)"#, r#"await fs.readFile(file, "utf8",)"#),
(r#"fs.promises.readFile(file, "UTF-8",)"#, r#"fs.promises.readFile(file, "utf8",)"#),
(r#"whatever.readFile(file, "UTF-8",)"#, r#"whatever.readFile(file, "utf8",)"#),
(r#"<not-meta charset="utf-8" />"#, r#"<not-meta charset="utf8" />"#),
(r#"<meta not-charset="utf-8" />"#, r#"<meta not-charset="utf8" />"#),
(r#"<meta charset="ASCII" />"#, r#"<meta charset="ascii" />"#),
(r#"<META CHARSET="ASCII" />"#, r#"<META CHARSET="ascii" />"#),
];

Tester::new(TextEncodingIdentifierCase::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}
23 changes: 23 additions & 0 deletions crates/oxc_linter/src/snapshots/text_encoding_identifier_case.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,135 +6,158 @@ source: crates/oxc_linter/src/tester.rs
1"UTF-8"
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `utf-8`.
╭─[text_encoding_identifier_case.tsx:1:1]
1"utf-8"
· ───────
╰────
help: Replace `utf-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `utf-8`.
╭─[text_encoding_identifier_case.tsx:1:1]
1'utf-8'
· ───────
╰────
help: Replace `utf-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `Utf8`.
╭─[text_encoding_identifier_case.tsx:1:1]
1"Utf8"
· ──────
╰────
help: Replace `Utf8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `ascii` over `ASCII`.
╭─[text_encoding_identifier_case.tsx:1:1]
1"ASCII"
· ───────
╰────
help: Replace `ASCII` with `ascii`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:21]
1fs.readFile?.(file, "UTF-8")
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:20]
1fs?.readFile(file, "UTF-8")
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:16]
1readFile(file, "UTF-8")
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:22]
1fs.readFile(...file, "UTF-8")
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:23]
1new fs.readFile(file, "UTF-8")
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:30]
1fs.readFile(file, {encoding: "UTF-8"})
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:13]
1fs.readFile("UTF-8")
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:19]
1fs.readFile(file, "UTF-8", () => {})
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:23]
1fs.readFileSync(file, "UTF-8")
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:20]
1fs[readFile](file, "UTF-8")
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:22]
1fs["readFile"](file, "UTF-8")
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:25]
1await fs.readFile(file, "UTF-8",)
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:28]
1fs.promises.readFile(file, "UTF-8",)
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `UTF-8`.
╭─[text_encoding_identifier_case.tsx:1:25]
1whatever.readFile(file, "UTF-8",)
· ───────
╰────
help: Replace `UTF-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `utf-8`.
╭─[text_encoding_identifier_case.tsx:1:19]
1<not-meta charset="utf-8" />
· ───────
╰────
help: Replace `utf-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `utf-8`.
╭─[text_encoding_identifier_case.tsx:1:19]
1<meta not-charset="utf-8" />
· ───────
╰────
help: Replace `utf-8` with `utf8`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `ascii` over `ASCII`.
╭─[text_encoding_identifier_case.tsx:1:15]
1<meta charset="ASCII" />
· ───────
╰────
help: Replace `ASCII` with `ascii`.

eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `ascii` over `ASCII`.
╭─[text_encoding_identifier_case.tsx:1:15]
1<META CHARSET="ASCII" />
· ───────
╰────
help: Replace `ASCII` with `ascii`.

0 comments on commit 27db769

Please sign in to comment.