Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module.exports = {
create(context) {
const sourceCode = context.getSourceCode();

return {
CallExpression(node) {
if (
Expand All @@ -21,16 +23,31 @@ module.exports = {

const argumentName = argumentValue.replace('data-', '');

const replacementString = argumentName.includes('-')
? `['${argumentName}']`
: '.' + argumentName;
context.report({
fix: (fixer) => {
if (node.callee.object.type === 'Identifier') {
return fixer.replaceText(
node,
`${node.callee.object.name}.dataset.${argumentName}`
`${node.callee.object.name}.dataset${replacementString}`
);
}
else if (
node.callee.object.type === 'MemberExpression'
) {
const memberExpressionString = sourceCode.getText(
node.callee.object
);

return fixer.replaceText(
node,
`${memberExpressionString}.dataset${replacementString}`
);
}
},
message: `Use "dataset.${argumentName}" instead of "getAttribute('${argumentValue}')"`,
message: `Use "dataset${replacementString}" instead of "getAttribute('${argumentValue}')"`,
node: node.callee.property,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ ruleTester.run('no-get-data-attribute', rule, {
code: `
el.querySelector('test').getAttribute('data-foo');
el.getAttribute('data-test');
el.getAttribute('data-some-other-key');
one.el.getAttribute('data-some-other-key');
`,
errors: [
{
Expand All @@ -32,10 +34,20 @@ ruleTester.run('no-get-data-attribute', rule, {
message: `Use "dataset.test" instead of "getAttribute('data-test')"`,
type: 'Identifier',
},
{
message: `Use "dataset['some-other-key']" instead of "getAttribute('data-some-other-key')"`,
type: 'Identifier',
},
{
message: `Use "dataset['some-other-key']" instead of "getAttribute('data-some-other-key')"`,
type: 'Identifier',
},
],
output: `
el.querySelector('test').getAttribute('data-foo');
el.dataset.test;
el.dataset['some-other-key'];
one.el.dataset['some-other-key'];
`,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ module.exports = {
const filename = path.basename(filePath);

if (
filename.includes('webpack') ||
filename.includes('jest') ||
filePath.includes('test')
filePath.includes('dev') ||
filePath.includes('test') ||
filePath.includes('webpack')
) {
return {};
}
Expand All @@ -25,8 +26,8 @@ module.exports = {
if (
node &&
node.value &&
typeof node.value.includes === 'function' &&
node.value.includes('localhost')
typeof node.value === 'string' &&
node.value.indexOf('localhost') !== -1
) {
context.report({
message: DESCRIPTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const path = require('path');

const CHECK_AND_FIX_GLOBS = [
'/*.{js,ts}',
'/{src,test}/**/*.{js,scss,ts,tsx}',
'/{dev,src,test}/**/*.{js,scss,ts,tsx}',
'/src/**/*.{jsp,jspf}',
];

Expand Down