Skip to content

Commit

Permalink
Use original quote when fixing (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jul 12, 2021
1 parent 3b22917 commit bb81582
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rules/better-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const create = context => {
},
fix: fixer => fixer.replaceText(
patternNode,
quoteString(newPattern)
quoteString(newPattern, patternNode.raw.charAt(0))
)
};
}
Expand Down
8 changes: 4 additions & 4 deletions rules/prefer-dom-node-dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ const parseNodeText = (context, argument) => context.getSourceCode().getText(arg
const dashToCamelCase = string => string.replace(/-[a-z]/g, s => s[1].toUpperCase());

const fix = (context, node, fixer) => {
let [name, value] = node.arguments;
const [nameNode, valueNode] = node.arguments;
const calleeObject = parseNodeText(context, node.callee.object);

name = dashToCamelCase(name.value.slice(5));
value = parseNodeText(context, value);
const name = dashToCamelCase(nameNode.value.slice(5));
const value = parseNodeText(context, valueNode);

const replacement = `${calleeObject}.dataset${
isValidVariableName(name) ?
`.${name}` :
`[${quoteString(name)}]`
`[${quoteString(name, nameNode.raw.charAt(0))}]`
} = ${value}`;

return fixer.replaceText(node, replacement);
Expand Down
9 changes: 7 additions & 2 deletions test/better-regex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,18 @@ test({
{
code: 'const foo = new RegExp("[0-9]")',
errors: createError('[0-9]', '\\d'),
output: 'const foo = new RegExp(\'\\\\d\')'
output: 'const foo = new RegExp("\\\\d")'
},
{
code: 'const foo = new RegExp("\'[0-9]\'")',
code: 'const foo = new RegExp(\'\\\'[0-9]\\\'\')',
errors: createError('\'[0-9]\'', '\'\\d\''),
output: 'const foo = new RegExp(\'\\\'\\\\d\\\'\')'
},
{
code: 'const foo = new RegExp("\'[0-9]\'")',
errors: createError('\'[0-9]\'', '\'\\d\''),
output: 'const foo = new RegExp("\'\\\\d\'")'
},
{
code: 'const foo = new RegExp(\'[0-9]\', \'ig\')',
errors: createError('[0-9]', '\\d'),
Expand Down
5 changes: 5 additions & 0 deletions test/prefer-dom-node-dataset.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ test({
errors,
output: 'element.dataset[\'foo:bar\'] = \'zaz\';'
},
{
code: 'element.setAttribute("data-foo:bar", "zaz");',
errors,
output: 'element.dataset["foo:bar"] = "zaz";'
},
{
code: 'element.setAttribute(\'data-foo.bar\', \'zaz\');',
errors,
Expand Down

0 comments on commit bb81582

Please sign in to comment.