Skip to content

Commit

Permalink
Replace isValidVariableName with isIdentifierName from Babel (#1975)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 18, 2022
1 parent f64a4a8 commit 046cf90
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 35 deletions.
4 changes: 2 additions & 2 deletions rules/prefer-dom-node-dataset.js
@@ -1,5 +1,5 @@
'use strict';
const isValidVariableName = require('./utils/is-valid-variable-name.js');
const {isIdentifierName} = require('@babel/helper-validator-identifier');
const quoteString = require('./utils/quote-string.js');
const {methodCallSelector, matches} = require('./selectors/index.js');

Expand Down Expand Up @@ -44,7 +44,7 @@ const create = context => ({
case 'setAttribute':
case 'getAttribute':
case 'removeAttribute': {
text = isValidVariableName(name) ? `.${name}` : `[${quoteString(name, nameNode.raw.charAt(0))}]`;
text = isIdentifierName(name) ? `.${name}` : `[${quoteString(name, nameNode.raw.charAt(0))}]`;
text = `${datasetText}${text}`;
if (method === 'setAttribute') {
text += ` = ${sourceCode.getText(node.arguments[1])}`;
Expand Down
3 changes: 0 additions & 3 deletions rules/utils/is-valid-variable-name.js

This file was deleted.

4 changes: 4 additions & 0 deletions test/prefer-dom-node-dataset.mjs
Expand Up @@ -39,6 +39,7 @@ test.snapshot({
`,
'element.setAttribute(\'data-unicorn\', \'🦄\');',
'element.setAttribute(\'data-🦄\', \'🦄\');',
'element.setAttribute(\'data-ゆ\', \'ゆ\');',
'element.setAttribute(\'data-foo2\', \'🦄\');',
'element.setAttribute(\'data-foo:bar\', \'zaz\');',
'element.setAttribute("data-foo:bar", "zaz");',
Expand Down Expand Up @@ -91,6 +92,7 @@ test.snapshot({
'element.removeAttribute("data-unicorn");',
'element.removeAttribute("data-unicorn",);',
'element.removeAttribute("data-🦄");',
'element.removeAttribute("data-ゆ");',
'element.removeAttribute("data-foo2");',
'element.removeAttribute("data-foo:bar");',
'element.removeAttribute("data-foo:bar");',
Expand Down Expand Up @@ -142,6 +144,7 @@ test.snapshot({
'element.hasAttribute("data-unicorn");',
'element.hasAttribute("data-unicorn",);',
'element.hasAttribute("data-🦄");',
'element.hasAttribute("data-ゆ");',
'element.hasAttribute("data-foo2");',
'element.hasAttribute("data-foo:bar");',
'element.hasAttribute("data-foo:bar");',
Expand Down Expand Up @@ -188,6 +191,7 @@ test.snapshot({
'element.getAttribute("data-unicorn");',
'element.getAttribute("data-unicorn",);',
'element.getAttribute("data-🦄");',
'element.getAttribute("data-ゆ");',
'element.getAttribute("data-foo2");',
'element.getAttribute("data-foo:bar");',
'element.getAttribute("data-foo:bar");',
Expand Down

0 comments on commit 046cf90

Please sign in to comment.