Skip to content

Commit a5c85d6

Browse files
Merge pull request #754 from bryceosterhaus/753
fix(eslint-plugin): dataset uses camelCased keys instead of kebab-case.
2 parents 53cf49c + 426a196 commit a5c85d6

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

projects/eslint-plugin/rules/general/lib/rules/no-get-data-attribute.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
* SPDX-License-Identifier: MIT
44
*/
55

6+
function camelize(val) {
7+
return val
8+
.split('-')
9+
.map((item, i) => {
10+
return i !== 0
11+
? item.charAt(0).toUpperCase() + item.slice(1)
12+
: item;
13+
})
14+
.join('');
15+
}
16+
617
module.exports = {
718
create(context) {
819
const sourceCode = context.getSourceCode();
@@ -21,17 +32,16 @@ module.exports = {
2132
) {
2233
const argumentValue = node.arguments[0].value;
2334

24-
const argumentName = argumentValue.replace('data-', '');
35+
const argumentName = camelize(
36+
argumentValue.replace('data-', '')
37+
);
2538

26-
const replacementString = argumentName.includes('-')
27-
? `['${argumentName}']`
28-
: '.' + argumentName;
2939
context.report({
3040
fix: (fixer) => {
3141
if (node.callee.object.type === 'Identifier') {
3242
return fixer.replaceText(
3343
node,
34-
`${node.callee.object.name}.dataset${replacementString}`
44+
`${node.callee.object.name}.dataset.${argumentName}`
3545
);
3646
}
3747
else if (
@@ -43,11 +53,11 @@ module.exports = {
4353

4454
return fixer.replaceText(
4555
node,
46-
`${memberExpressionString}.dataset${replacementString}`
56+
`${memberExpressionString}.dataset.${argumentName}`
4757
);
4858
}
4959
},
50-
message: `Use "dataset${replacementString}" instead of "getAttribute('${argumentValue}')"`,
60+
message: `Use "dataset.${argumentName}" instead of "getAttribute('${argumentValue}')"`,
5161
node: node.callee.property,
5262
});
5363
}

projects/eslint-plugin/rules/general/tests/lib/rules/no-get-data-attribute.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ ruleTester.run('no-get-data-attribute', rule, {
3535
type: 'Identifier',
3636
},
3737
{
38-
message: `Use "dataset['some-other-key']" instead of "getAttribute('data-some-other-key')"`,
38+
message: `Use "dataset.someOtherKey" instead of "getAttribute('data-some-other-key')"`,
3939
type: 'Identifier',
4040
},
4141
{
42-
message: `Use "dataset['some-other-key']" instead of "getAttribute('data-some-other-key')"`,
42+
message: `Use "dataset.someOtherKey" instead of "getAttribute('data-some-other-key')"`,
4343
type: 'Identifier',
4444
},
4545
],
4646
output: `
4747
el.querySelector('test').getAttribute('data-foo');
4848
el.dataset.test;
49-
el.dataset['some-other-key'];
50-
one.el.dataset['some-other-key'];
49+
el.dataset.someOtherKey;
50+
one.el.dataset.someOtherKey;
5151
`,
5252
},
5353
],

0 commit comments

Comments
 (0)