Skip to content

Commit

Permalink
text-encoding-identifier-case: Ignore JSX meta[charset="utf-8"] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jun 4, 2022
1 parent 51d7e06 commit c67a70f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
14 changes: 14 additions & 0 deletions rules/text-encoding-identifier-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ const create = () => ({
return;
}

if (
node.value === 'utf-8'
&& node.parent.type === 'JSXAttribute'
&& node.parent.value === node
&& node.parent.name.type === 'JSXIdentifier'
&& node.parent.name.name.toLowerCase() === 'charset'
&& node.parent.parent.type === 'JSXOpeningElement'
&& node.parent.parent.attributes.includes(node.parent)
&& node.parent.parent.name.type === 'JSXIdentifier'
&& node.parent.parent.name.name.toLowerCase() === 'meta'
) {
return;
}

const {raw} = node;
const value = raw.slice(1, -1);

Expand Down
56 changes: 56 additions & 0 deletions test/snapshots/text-encoding-identifier-case.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,59 @@ Generated by [AVA](https://avajs.dev).
> 1 | whatever.readFile(file, "UTF-8",)␊
| ^^^^^^^ Prefer \`utf8\` over \`UTF-8\`.␊
`

## Invalid #1
1 | <not-meta charset="utf-8" />

> Error 1/1
`␊
> 1 | <not-meta charset="utf-8" />␊
| ^^^^^^^ Prefer \`utf8\` over \`utf-8\`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace \`utf-8\` with \`utf8\`.␊
1 | <not-meta charset="utf8" />␊
`

## Invalid #2
1 | <meta not-charset="utf-8" />

> Error 1/1
`␊
> 1 | <meta not-charset="utf-8" />␊
| ^^^^^^^ Prefer \`utf8\` over \`utf-8\`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace \`utf-8\` with \`utf8\`.␊
1 | <meta not-charset="utf8" />␊
`

## Invalid #3
1 | <meta charset="ASCII" />

> Error 1/1
`␊
> 1 | <meta charset="ASCII" />␊
| ^^^^^^^ Prefer \`ascii\` over \`ASCII\`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace \`ASCII\` with \`ascii\`.␊
1 | <meta charset="ascii" />␊
`

## Invalid #4
1 | <META CHARSET="ASCII" />

> Error 1/1
`␊
> 1 | <META CHARSET="ASCII" />␊
| ^^^^^^^ Prefer \`ascii\` over \`ASCII\`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace \`ASCII\` with \`ascii\`.␊
1 | <META CHARSET="ascii" />␊
`
Binary file modified test/snapshots/text-encoding-identifier-case.mjs.snap
Binary file not shown.
21 changes: 21 additions & 0 deletions test/text-encoding-identifier-case.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,24 @@ test.snapshot({
'whatever.readFile(file, "UTF-8",)',
],
});

// JSX
test.snapshot({
testerOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
valid: [
'<meta charset="utf-8" />',
'<META CHARSET="utf-8" />',
],
invalid: [
'<not-meta charset="utf-8" />',
'<meta not-charset="utf-8" />',
'<meta charset="ASCII" />',
'<META CHARSET="ASCII" />',
],
});

0 comments on commit c67a70f

Please sign in to comment.