Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text-encoding-identifier-case: Ignore JSX meta[charset="utf-8"] #1817

Merged
merged 2 commits into from Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions rules/text-encoding-identifier-case.js
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
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
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" />',
],
});