JS: jQuery usage efficiency false positive in inspector

I have this piece of pseudo-code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

function afterSomedynamicChanges() {
$('.test').html('this is at the run-time may be different due added elements!');
}

$('.test').html('this is initial');
doSomeDynamicChanges();
afterSomedynamicChanges();

</script>

The PHPstorm inspector alerts me "Javascript -> General -> jQuery usage efficiency -> Duplicated selector", the problem is that this is false positive, the jQuery selector must be executed twice, because the initial set of matched elements may be different because JS may add new contents dynamically with same class name, so the original selector no longer contains all matched nodes when I call the run-time function.

Please confirm if I understand this correctly, but I believe that the selector inspection in functions and methods should be ignore the global selectors. Even if not.... then there should be an option to suppress this warning for single statement (which does not seem to be present now).

 

 

3 comments

there is a request to not show this warning if there's an append() or insert*() with the same selector, https://youtrack.jetbrains.com/issue/WEB-33881. Please follow it for updates

For now, you can suppress the warning by hitting Alt+Enter and then Right and choosing the corresponding action from the submenu:

0

Thanks for the answer, but well, I don't have the option for single statement disable, what can be the issue - advise? I have newest version of PHP storm with all upgrades.

 

Just in case and not to bother you twice, can you paste the suppress inspection code in text so that I can just copy& paste if I will not be bale to resolve the missing item in GUI issues?

Or maybe the "Suppress for tag" is what should be used? Re-thinking it I start to suspect that suppress for tag works in HTML land and suppress for statement in JS land... correct?

0

>I don't have the option for single statement disable, what can be the issue - advise? 

No idea. What PHPStorm version do you use?

> can you paste the suppress inspection code in text so that I can just copy& paste if I will not be bale to resolve the missing item in GUI issues?

please try

// noinspection JSJQueryEfficiency

 

Suppress for tag will suppress the inspections for all statements within current <script> tag :

<!--suppress JSJQueryEfficiency -->
<script>
0

Please sign in to leave a comment.