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

prefer-dom-node-dataset: Fix name conversion #1674

Merged
merged 2 commits into from
Dec 30, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions rules/prefer-dom-node-dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ const dashToCamelCase = string => string.replace(/-[a-z]/g, s => s[1].toUpperCas
const create = context => ({
[selector](node) {
const [nameNode] = node.arguments;
const attributeName = nameNode.value;
let attributeName = nameNode.value;

if (typeof attributeName !== 'string' || !attributeName.startsWith('data-') || attributeName === 'data-') {
if (typeof attributeName !== 'string') {
return;
}

attributeName = attributeName.toLowerCase();

if (!attributeName.startsWith('data-')) {
return;
}

Expand Down
18 changes: 10 additions & 8 deletions test/prefer-dom-node-dataset.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ test.snapshot({
'element.setAttribute(0, \'🦄\');',
// First Argument is not startsWith `data-`
'element.setAttribute(\'foo-unicorn\', \'🦄\');',
// First Argument is `data-`
'element.setAttribute(\'data-\', \'🦄\');',
'element.setAttribute(\'data\', \'🦄\');',
],
invalid: [
outdent`
Expand All @@ -47,6 +46,11 @@ test.snapshot({
'element.setAttribute(\'data-foo-bar\', \'zaz\');',
'element.setAttribute(\'data-foo\', /* comment */ \'bar\');',
'element.querySelector(\'#selector\').setAttribute(\'data-AllowAccess\', true);',
'element.setAttribute("data-", "🦄");',
'element.setAttribute("data--foo", "🦄");',
'element.setAttribute("DATA--FOO", "🦄");',
'element.setAttribute("DATA- ", "🦄");',
'element.setAttribute("DATA-Foo-bar", "🦄");',
],
});

Expand Down Expand Up @@ -75,8 +79,7 @@ test.snapshot({
'element.removeAttribute(0);',
// First Argument is not startsWith `data-`
'element.removeAttribute("foo-unicorn");',
// First Argument is `data-`
'element.removeAttribute("data-");',
'element.removeAttribute("data");',
],
invalid: [
outdent`
Expand All @@ -95,6 +98,7 @@ test.snapshot({
'element.removeAttribute("data-foo-bar");',
'element.removeAttribute("data-foo");',
'element.querySelector("#selector").removeAttribute("data-AllowAccess");',
'element.removeAttribute("data-");',
],
});

Expand Down Expand Up @@ -126,8 +130,7 @@ test.snapshot({
'element.hasAttribute(0);',
// First Argument is not startsWith `data-`
'element.hasAttribute("foo-unicorn");',
// First Argument is `data-`
'element.hasAttribute("data-");',
'element.hasAttribute("data");',
],
invalid: [
outdent`
Expand Down Expand Up @@ -173,8 +176,7 @@ test.snapshot({
'element.getAttribute(0);',
// First Argument is not startsWith `data-`
'element.getAttribute("foo-unicorn");',
// First Argument is `data-`
'element.getAttribute("data-");',
'element.getAttribute("data");',
],
invalid: [
outdent`
Expand Down
104 changes: 100 additions & 4 deletions test/snapshots/prefer-dom-node-dataset.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Generated by [AVA](https://avajs.dev).
> Output

`␊
1 | element.querySelector('#selector').dataset.AllowAccess = true;␊
1 | element.querySelector('#selector').dataset.allowaccess = true;␊
`

> Error 1/1
Expand All @@ -173,6 +173,86 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`.dataset\` over \`setAttribute(…)\`.␊
`

## Invalid #11
1 | element.setAttribute("data-", "🦄");

> Output

`␊
1 | element.dataset[""] = "🦄";␊
`

> Error 1/1

`␊
> 1 | element.setAttribute("data-", "🦄");␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`.dataset\` over \`setAttribute(…)\`.␊
`

## Invalid #12
1 | element.setAttribute("data--foo", "🦄");

> Output

`␊
1 | element.dataset.Foo = "🦄";␊
`

> Error 1/1

`␊
> 1 | element.setAttribute("data--foo", "🦄");␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`.dataset\` over \`setAttribute(…)\`.␊
`

## Invalid #13
1 | element.setAttribute("DATA--FOO", "🦄");

> Output

`␊
1 | element.dataset.Foo = "🦄";␊
`

> Error 1/1

`␊
> 1 | element.setAttribute("DATA--FOO", "🦄");␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`.dataset\` over \`setAttribute(…)\`.␊
`

## Invalid #14
1 | element.setAttribute("DATA- ", "🦄");

> Output

`␊
1 | element.dataset[" "] = "🦄";␊
`

> Error 1/1

`␊
> 1 | element.setAttribute("DATA- ", "🦄");␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`.dataset\` over \`setAttribute(…)\`.␊
`

## Invalid #15
1 | element.setAttribute("DATA-Foo-bar", "🦄");

> Output

`␊
1 | element.dataset.fooBar = "🦄";␊
`

> Error 1/1

`␊
> 1 | element.setAttribute("DATA-Foo-bar", "🦄");␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`.dataset\` over \`setAttribute(…)\`.␊
`

## Invalid #1
1 | element.removeAttribute(
2 | "data-foo", // comment
Expand Down Expand Up @@ -361,7 +441,7 @@ Generated by [AVA](https://avajs.dev).
> Output

`␊
1 | delete element.querySelector("#selector").dataset.AllowAccess;␊
1 | delete element.querySelector("#selector").dataset.allowaccess;␊
`

> Error 1/1
Expand All @@ -371,6 +451,22 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`.dataset\` over \`removeAttribute(…)\`.␊
`

## Invalid #13
1 | element.removeAttribute("data-");

> Output

`␊
1 | delete element.dataset[""];␊
`

> Error 1/1

`␊
> 1 | element.removeAttribute("data-");␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`.dataset\` over \`removeAttribute(…)\`.␊
`

## Invalid #1
1 | element.hasAttribute(
2 | "data-foo", // comment
Expand Down Expand Up @@ -559,7 +655,7 @@ Generated by [AVA](https://avajs.dev).
> Output

`␊
1 | Object.hasOwn(element.querySelector("#selector").dataset, "AllowAccess");␊
1 | Object.hasOwn(element.querySelector("#selector").dataset, "allowaccess");␊
`

> Error 1/1
Expand Down Expand Up @@ -757,7 +853,7 @@ Generated by [AVA](https://avajs.dev).
> Output

`␊
1 | element.querySelector("#selector").dataset.AllowAccess;␊
1 | element.querySelector("#selector").dataset.allowaccess;␊
`

> Error 1/1
Expand Down
Binary file modified test/snapshots/prefer-dom-node-dataset.mjs.snap
Binary file not shown.