Skip to content

Commit c67a70f

Browse files
authored
text-encoding-identifier-case: Ignore JSX meta[charset="utf-8"] (#1817)
1 parent 51d7e06 commit c67a70f

4 files changed

+91
-0
lines changed

rules/text-encoding-identifier-case.js

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ const create = () => ({
3939
return;
4040
}
4141

42+
if (
43+
node.value === 'utf-8'
44+
&& node.parent.type === 'JSXAttribute'
45+
&& node.parent.value === node
46+
&& node.parent.name.type === 'JSXIdentifier'
47+
&& node.parent.name.name.toLowerCase() === 'charset'
48+
&& node.parent.parent.type === 'JSXOpeningElement'
49+
&& node.parent.parent.attributes.includes(node.parent)
50+
&& node.parent.parent.name.type === 'JSXIdentifier'
51+
&& node.parent.parent.name.name.toLowerCase() === 'meta'
52+
) {
53+
return;
54+
}
55+
4256
const {raw} = node;
4357
const value = raw.slice(1, -1);
4458

test/snapshots/text-encoding-identifier-case.mjs.md

+56
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,59 @@ Generated by [AVA](https://avajs.dev).
279279
> 1 | whatever.readFile(file, "UTF-8",)␊
280280
| ^^^^^^^ Prefer \`utf8\` over \`UTF-8\`.␊
281281
`
282+
283+
## Invalid #1
284+
1 | <not-meta charset="utf-8" />
285+
286+
> Error 1/1
287+
288+
`␊
289+
> 1 | <not-meta charset="utf-8" />␊
290+
| ^^^^^^^ Prefer \`utf8\` over \`utf-8\`.␊
291+
292+
--------------------------------------------------------------------------------␊
293+
Suggestion 1/1: Replace \`utf-8\` with \`utf8\`.␊
294+
1 | <not-meta charset="utf8" />␊
295+
`
296+
297+
## Invalid #2
298+
1 | <meta not-charset="utf-8" />
299+
300+
> Error 1/1
301+
302+
`␊
303+
> 1 | <meta not-charset="utf-8" />␊
304+
| ^^^^^^^ Prefer \`utf8\` over \`utf-8\`.␊
305+
306+
--------------------------------------------------------------------------------␊
307+
Suggestion 1/1: Replace \`utf-8\` with \`utf8\`.␊
308+
1 | <meta not-charset="utf8" />␊
309+
`
310+
311+
## Invalid #3
312+
1 | <meta charset="ASCII" />
313+
314+
> Error 1/1
315+
316+
`␊
317+
> 1 | <meta charset="ASCII" />␊
318+
| ^^^^^^^ Prefer \`ascii\` over \`ASCII\`.␊
319+
320+
--------------------------------------------------------------------------------␊
321+
Suggestion 1/1: Replace \`ASCII\` with \`ascii\`.␊
322+
1 | <meta charset="ascii" />␊
323+
`
324+
325+
## Invalid #4
326+
1 | <META CHARSET="ASCII" />
327+
328+
> Error 1/1
329+
330+
`␊
331+
> 1 | <META CHARSET="ASCII" />␊
332+
| ^^^^^^^ Prefer \`ascii\` over \`ASCII\`.␊
333+
334+
--------------------------------------------------------------------------------␊
335+
Suggestion 1/1: Replace \`ASCII\` with \`ascii\`.␊
336+
1 | <META CHARSET="ascii" />␊
337+
`
Binary file not shown.

test/text-encoding-identifier-case.mjs

+21
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,24 @@ test.snapshot({
3535
'whatever.readFile(file, "UTF-8",)',
3636
],
3737
});
38+
39+
// JSX
40+
test.snapshot({
41+
testerOptions: {
42+
parserOptions: {
43+
ecmaFeatures: {
44+
jsx: true,
45+
},
46+
},
47+
},
48+
valid: [
49+
'<meta charset="utf-8" />',
50+
'<META CHARSET="utf-8" />',
51+
],
52+
invalid: [
53+
'<not-meta charset="utf-8" />',
54+
'<meta not-charset="utf-8" />',
55+
'<meta charset="ASCII" />',
56+
'<META CHARSET="ASCII" />',
57+
],
58+
});

0 commit comments

Comments
 (0)