Skip to content

Commit 3ba54e9

Browse files
amedorasindresorhus
authored andcommitted
unicorn/prefer-dataset: Fix querySelector fixer (#383)
1 parent c28c770 commit 3ba54e9

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

rules/prefer-dataset.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ const getDataAttributeName = arg => {
1212
return '';
1313
};
1414

15-
const parseValueArgument = (context, arg) => context.getSourceCode().getText(arg);
15+
const parseNodeText = (context, arg) => context.getSourceCode().getText(arg);
1616

1717
const dashToCamelCase = string => string.replace(/-([a-z])/g, s => s[1].toUpperCase());
1818

1919
const getReplacement = (context, node, memberExpression, propertyName) => {
20-
const objectName = memberExpression.object.name;
21-
const value = parseValueArgument(context, node.arguments[1]);
20+
const calleeObject = parseNodeText(context, memberExpression.object);
21+
const value = parseNodeText(context, node.arguments[1]);
2222

2323
propertyName = dashToCamelCase(propertyName);
2424

2525
if (!isValidVariableName(propertyName)) {
26-
return `${objectName}.dataset['${propertyName}'] = ${value}`;
26+
return `${calleeObject}.dataset['${propertyName}'] = ${value}`;
2727
}
2828

29-
return `${objectName}.dataset.${propertyName} = ${value}`;
29+
return `${calleeObject}.dataset.${propertyName} = ${value}`;
3030
};
3131

3232
const isBracketNotation = (context, callee) => {

test/prefer-dataset.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ ruleTester.run('prefer-dataset', rule, {
7070
`,
7171
errors,
7272
output: 'element.dataset.foo = \'bar\';'
73+
},
74+
{
75+
code: 'element.querySelector(\'#selector\').setAttribute(\'data-AllowAccess\', true);',
76+
errors,
77+
output: 'element.querySelector(\'#selector\').dataset.AllowAccess = true;'
7378
}
7479
]
7580
});

0 commit comments

Comments
 (0)