Skip to content

Commit

Permalink
prefer-query-selector: Fix crash on unexpected selector (#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 19, 2021
1 parent 700f8bf commit 88018ca
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
2 changes: 1 addition & 1 deletion rules/prefer-query-selector.js
Expand Up @@ -67,7 +67,7 @@ function * getTemplateLiteralFix(fixer, node, identifierName) {

const canBeFixed = node => {
if (node.type === 'Literal') {
return node.value === null || Boolean(node.value.trim());
return node.raw === 'null' || (typeof node.value === 'string' && Boolean(node.value.trim()));
}

if (node.type === 'TemplateLiteral') {
Expand Down
4 changes: 3 additions & 1 deletion test/integration/projects.js
Expand Up @@ -158,7 +158,9 @@ module.exports = [
]
},
// #903
'https://github.com/mattermost/mattermost-webapp'
'https://github.com/mattermost/mattermost-webapp',
// #1030
'https://github.com/astrofox-io/astrofox'
].map(project => {
if (typeof project === 'string') {
project = {repository: project};
Expand Down
64 changes: 32 additions & 32 deletions test/prefer-query-selector.js
Expand Up @@ -2,7 +2,7 @@ import {outdent} from 'outdent';
import {test} from './utils/test.js';
import notDomNodeTypes from './utils/not-dom-node-types.js';

test({
test.snapshot({
valid: [
// Not `CallExpression`
'new document.getElementById(foo);',
Expand All @@ -29,35 +29,35 @@ test({
'document.querySelectorAll("li a");',
'document.querySelector("li").querySelectorAll("a");'
],
invalid: []
invalid: [
'document.getElementById("foo");',
'document.getElementsByClassName("foo");',
'document.getElementsByClassName("foo bar");',
'document.getElementsByTagName("foo");',
'document.getElementById("");',
'document.getElementById(\'foo\');',
'document.getElementsByClassName(\'foo\');',
'document.getElementsByClassName(\'foo bar\');',
'document.getElementsByTagName(\'foo\');',
'document.getElementsByClassName(\'\');',
'document.getElementById(`foo`);',
'document.getElementsByClassName(`foo`);',
'document.getElementsByClassName(`foo bar`);',
'document.getElementsByTagName(`foo`);',
'document.getElementsByTagName(``);',
'document.getElementsByClassName(`${fn()}`);', // eslint-disable-line no-template-curly-in-string
'document.getElementsByClassName(`foo ${undefined}`);', // eslint-disable-line no-template-curly-in-string
'document.getElementsByClassName(null);',
'document.getElementsByTagName(null);',
'document.getElementsByClassName(fn());',
'document.getElementsByClassName("foo" + fn());',
'document.getElementsByClassName(foo + "bar");',
outdent`
for (const div of document.body.getElementById("id").getElementsByClassName("class")) {
console.log(div.getElementsByTagName("div"));
}
`,
// #1030
'e.getElementById(3)'
]
});

test.snapshot([
'document.getElementById("foo");',
'document.getElementsByClassName("foo");',
'document.getElementsByClassName("foo bar");',
'document.getElementsByTagName("foo");',
'document.getElementById("");',
'document.getElementById(\'foo\');',
'document.getElementsByClassName(\'foo\');',
'document.getElementsByClassName(\'foo bar\');',
'document.getElementsByTagName(\'foo\');',
'document.getElementsByClassName(\'\');',
'document.getElementById(`foo`);',
'document.getElementsByClassName(`foo`);',
'document.getElementsByClassName(`foo bar`);',
'document.getElementsByTagName(`foo`);',
'document.getElementsByTagName(``);',
'document.getElementsByClassName(`${fn()}`);', // eslint-disable-line no-template-curly-in-string
'document.getElementsByClassName(`foo ${undefined}`);', // eslint-disable-line no-template-curly-in-string
'document.getElementsByClassName(null);',
'document.getElementsByTagName(null);',
'document.getElementsByClassName(fn());',
'document.getElementsByClassName("foo" + fn());',
'document.getElementsByClassName(foo + "bar");',
outdent`
for (const div of document.body.getElementById("id").getElementsByClassName("class")) {
console.log(div.getElementsByTagName("div"));
}
`
]);
10 changes: 10 additions & 0 deletions test/snapshots/prefer-query-selector.js.md
Expand Up @@ -347,3 +347,13 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^ Prefer `.querySelectorAll()` over `.getElementsByTagName()`.␊
3 | }␊
`

## Invalid #24
1 | e.getElementById(3)

> Error 1/1
`␊
> 1 | e.getElementById(3)␊
| ^^^^^^^^^^^^^^ Prefer `.querySelector()` over `.getElementById()`.␊
`
Binary file modified test/snapshots/prefer-query-selector.js.snap
Binary file not shown.

0 comments on commit 88018ca

Please sign in to comment.