Skip to content

Commit 27db769

Browse files
authoredAug 23, 2024··
feat(linter/unicorn): add fixer to text-encoding-identifier-case (#5154)
1 parent 7eb052e commit 27db769

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed
 

‎crates/oxc_linter/src/rules/unicorn/text_encoding_identifier_case.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ declare_oxc_lint!(
4646
/// ```
4747
TextEncodingIdentifierCase,
4848
style,
49-
pending
49+
fix
5050
);
5151

5252
impl Rule for TextEncodingIdentifierCase {
@@ -72,7 +72,10 @@ impl Rule for TextEncodingIdentifierCase {
7272
return;
7373
}
7474

75-
ctx.diagnostic(text_encoding_identifier_case_diagnostic(span, replacement, s));
75+
ctx.diagnostic_with_fix(
76+
text_encoding_identifier_case_diagnostic(span, replacement, s),
77+
|fixer| fixer.replace(Span::new(span.start + 1, span.end - 1), replacement),
78+
);
7679
}
7780
}
7881

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

172-
Tester::new(TextEncodingIdentifierCase::NAME, pass, fail).test_and_snapshot();
175+
let fix = vec![
176+
(r#""UTF-8""#, r#""utf8""#),
177+
(r#""utf-8""#, r#""utf8""#),
178+
(r"'utf-8'", r"'utf8'"),
179+
(r#""Utf8""#, r#""utf8""#),
180+
(r#""ASCII""#, r#""ascii""#),
181+
(r#"fs.readFile?.(file, "UTF-8")"#, r#"fs.readFile?.(file, "utf8")"#),
182+
(r#"fs?.readFile(file, "UTF-8")"#, r#"fs?.readFile(file, "utf8")"#),
183+
(r#"readFile(file, "UTF-8")"#, r#"readFile(file, "utf8")"#),
184+
(r#"fs.readFile(...file, "UTF-8")"#, r#"fs.readFile(...file, "utf8")"#),
185+
(r#"new fs.readFile(file, "UTF-8")"#, r#"new fs.readFile(file, "utf8")"#),
186+
(r#"fs.readFile(file, {encoding: "UTF-8"})"#, r#"fs.readFile(file, {encoding: "utf8"})"#),
187+
(r#"fs.readFile("UTF-8")"#, r#"fs.readFile("utf8")"#),
188+
(r#"fs.readFile(file, "UTF-8", () => {})"#, r#"fs.readFile(file, "utf8", () => {})"#),
189+
(r#"fs.readFileSync(file, "UTF-8")"#, r#"fs.readFileSync(file, "utf8")"#),
190+
(r#"fs[readFile](file, "UTF-8")"#, r#"fs[readFile](file, "utf8")"#),
191+
(r#"fs["readFile"](file, "UTF-8")"#, r#"fs["readFile"](file, "utf8")"#),
192+
(r#"await fs.readFile(file, "UTF-8",)"#, r#"await fs.readFile(file, "utf8",)"#),
193+
(r#"fs.promises.readFile(file, "UTF-8",)"#, r#"fs.promises.readFile(file, "utf8",)"#),
194+
(r#"whatever.readFile(file, "UTF-8",)"#, r#"whatever.readFile(file, "utf8",)"#),
195+
(r#"<not-meta charset="utf-8" />"#, r#"<not-meta charset="utf8" />"#),
196+
(r#"<meta not-charset="utf-8" />"#, r#"<meta not-charset="utf8" />"#),
197+
(r#"<meta charset="ASCII" />"#, r#"<meta charset="ascii" />"#),
198+
(r#"<META CHARSET="ASCII" />"#, r#"<META CHARSET="ascii" />"#),
199+
];
200+
201+
Tester::new(TextEncodingIdentifierCase::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
173202
}

‎crates/oxc_linter/src/snapshots/text_encoding_identifier_case.snap

+23
Original file line numberDiff line numberDiff line change
@@ -6,135 +6,158 @@ source: crates/oxc_linter/src/tester.rs
66
1"UTF-8"
77
· ───────
88
╰────
9+
help: Replace `UTF-8` with `utf8`.
910

1011
eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `utf-8`.
1112
╭─[text_encoding_identifier_case.tsx:1:1]
1213
1"utf-8"
1314
· ───────
1415
╰────
16+
help: Replace `utf-8` with `utf8`.
1517

1618
eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `utf-8`.
1719
╭─[text_encoding_identifier_case.tsx:1:1]
1820
1'utf-8'
1921
· ───────
2022
╰────
23+
help: Replace `utf-8` with `utf8`.
2124

2225
eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `utf8` over `Utf8`.
2326
╭─[text_encoding_identifier_case.tsx:1:1]
2427
1"Utf8"
2528
· ──────
2629
╰────
30+
help: Replace `Utf8` with `utf8`.
2731

2832
eslint-plugin-unicorn(text-encoding-identifier-case): Prefer `ascii` over `ASCII`.
2933
╭─[text_encoding_identifier_case.tsx:1:1]
3034
1"ASCII"
3135
· ───────
3236
╰────
37+
help: Replace `ASCII` with `ascii`.
3338

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.